|
| 1 | +use async_graphql::{FieldResult, OutputType, SimpleObject}; |
| 2 | +use std::convert::TryFrom; |
| 3 | + |
| 4 | +#[derive(SimpleObject)] |
| 5 | +pub struct ConnectionFields<C: OutputType + Send + Sync> { |
| 6 | + pub total_count: C, |
| 7 | +} |
| 8 | + |
| 9 | +pub struct ValidatedPaginationArguments<I> { |
| 10 | + pub first: Option<usize>, |
| 11 | + pub last: Option<usize>, |
| 12 | + pub before: Option<I>, |
| 13 | + pub after: Option<I>, |
| 14 | +} |
| 15 | + |
| 16 | +pub struct PageMeta { |
| 17 | + pub has_next_page: bool, |
| 18 | + pub has_previous_page: bool, |
| 19 | + pub total_count: u64, |
| 20 | +} |
| 21 | + |
| 22 | +fn compute_range_boundaries( |
| 23 | + total_elements: InclusivePaginationInterval<u64>, |
| 24 | + pagination_arguments: ValidatedPaginationArguments<u64>, |
| 25 | +) -> PaginationInterval<u64> |
| 26 | +where |
| 27 | +{ |
| 28 | + use std::cmp::{max, min}; |
| 29 | + |
| 30 | + let InclusivePaginationInterval { |
| 31 | + upper_bound, |
| 32 | + lower_bound, |
| 33 | + } = total_elements; |
| 34 | + |
| 35 | + // Compute the required range of blocks in two variables: [from, to] |
| 36 | + // Both ends are inclusive |
| 37 | + let mut from: u64 = match pagination_arguments.after { |
| 38 | + Some(cursor) => max(cursor + 1, lower_bound), |
| 39 | + // If `after` is not set, start from the beginning |
| 40 | + None => lower_bound, |
| 41 | + }; |
| 42 | + |
| 43 | + let mut to: u64 = match pagination_arguments.before { |
| 44 | + Some(cursor) => { |
| 45 | + if cursor == 0 { |
| 46 | + return PaginationInterval::Empty; |
| 47 | + } |
| 48 | + min(cursor - 1, upper_bound) |
| 49 | + } |
| 50 | + // If `before` is not set, start from the beginning |
| 51 | + None => upper_bound, |
| 52 | + }; |
| 53 | + |
| 54 | + // Move `to` enough values to make the result have `first` blocks |
| 55 | + if let Some(first) = pagination_arguments.first { |
| 56 | + to = min( |
| 57 | + from.checked_add(u64::try_from(first).unwrap()) |
| 58 | + .and_then(|n| n.checked_sub(1)) |
| 59 | + .unwrap_or(to), |
| 60 | + to, |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + // Move `from` enough values to make the result have `last` blocks |
| 65 | + if let Some(last) = pagination_arguments.last { |
| 66 | + from = max( |
| 67 | + to.checked_sub(u64::try_from(last).unwrap()) |
| 68 | + .and_then(|n| n.checked_add(1)) |
| 69 | + .unwrap_or(from), |
| 70 | + from, |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + PaginationInterval::Inclusive(InclusivePaginationInterval { |
| 75 | + lower_bound: from, |
| 76 | + upper_bound: to, |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +pub fn compute_interval<I>( |
| 81 | + bounds: PaginationInterval<I>, |
| 82 | + pagination_arguments: ValidatedPaginationArguments<I>, |
| 83 | +) -> FieldResult<(PaginationInterval<I>, PageMeta)> |
| 84 | +where |
| 85 | + I: TryFrom<u64> + Clone, |
| 86 | + u64: From<I>, |
| 87 | +{ |
| 88 | + let pagination_arguments = pagination_arguments.cursors_into::<u64>(); |
| 89 | + let bounds = bounds.bounds_into::<u64>(); |
| 90 | + |
| 91 | + let (page_interval, has_next_page, has_previous_page, total_count) = match bounds { |
| 92 | + PaginationInterval::Empty => (PaginationInterval::Empty, false, false, 0u64), |
| 93 | + PaginationInterval::Inclusive(total_elements) => { |
| 94 | + let InclusivePaginationInterval { |
| 95 | + upper_bound, |
| 96 | + lower_bound, |
| 97 | + } = total_elements; |
| 98 | + |
| 99 | + let page = compute_range_boundaries(total_elements, pagination_arguments); |
| 100 | + |
| 101 | + let (has_previous_page, has_next_page) = match &page { |
| 102 | + PaginationInterval::Empty => (false, false), |
| 103 | + PaginationInterval::Inclusive(page) => ( |
| 104 | + page.lower_bound > lower_bound, |
| 105 | + page.upper_bound < upper_bound, |
| 106 | + ), |
| 107 | + }; |
| 108 | + |
| 109 | + let total_count = upper_bound |
| 110 | + .checked_add(1) |
| 111 | + .unwrap() |
| 112 | + .checked_sub(lower_bound) |
| 113 | + .expect("upper_bound should be >= than lower_bound"); |
| 114 | + (page, has_next_page, has_previous_page, total_count) |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + Ok(page_interval |
| 119 | + .bounds_try_into::<I>() |
| 120 | + .map(|interval| { |
| 121 | + ( |
| 122 | + interval, |
| 123 | + PageMeta { |
| 124 | + has_next_page, |
| 125 | + has_previous_page, |
| 126 | + total_count, |
| 127 | + }, |
| 128 | + ) |
| 129 | + }) |
| 130 | + .map_err(|_| "computed page interval is outside pagination boundaries") |
| 131 | + .unwrap()) |
| 132 | +} |
| 133 | + |
| 134 | +impl<I> ValidatedPaginationArguments<I> { |
| 135 | + fn cursors_into<T>(self) -> ValidatedPaginationArguments<T> |
| 136 | + where |
| 137 | + T: From<I>, |
| 138 | + { |
| 139 | + ValidatedPaginationArguments { |
| 140 | + after: self.after.map(T::from), |
| 141 | + before: self.before.map(T::from), |
| 142 | + first: self.first, |
| 143 | + last: self.last, |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +pub enum PaginationInterval<I> { |
| 149 | + Empty, |
| 150 | + Inclusive(InclusivePaginationInterval<I>), |
| 151 | +} |
| 152 | + |
| 153 | +pub struct InclusivePaginationInterval<I> { |
| 154 | + pub lower_bound: I, |
| 155 | + pub upper_bound: I, |
| 156 | +} |
| 157 | + |
| 158 | +impl<I> PaginationInterval<I> { |
| 159 | + fn bounds_into<T>(self) -> PaginationInterval<T> |
| 160 | + where |
| 161 | + T: From<I>, |
| 162 | + { |
| 163 | + match self { |
| 164 | + Self::Empty => PaginationInterval::<T>::Empty, |
| 165 | + Self::Inclusive(interval) => { |
| 166 | + PaginationInterval::<T>::Inclusive(InclusivePaginationInterval::<T> { |
| 167 | + lower_bound: T::from(interval.lower_bound), |
| 168 | + upper_bound: T::from(interval.upper_bound), |
| 169 | + }) |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + fn bounds_try_into<T>(self) -> Result<PaginationInterval<T>, <T as TryFrom<I>>::Error> |
| 175 | + where |
| 176 | + T: TryFrom<I>, |
| 177 | + { |
| 178 | + match self { |
| 179 | + Self::Empty => Ok(PaginationInterval::<T>::Empty), |
| 180 | + Self::Inclusive(interval) => Ok(PaginationInterval::<T>::Inclusive( |
| 181 | + InclusivePaginationInterval::<T> { |
| 182 | + lower_bound: T::try_from(interval.lower_bound)?, |
| 183 | + upper_bound: T::try_from(interval.upper_bound)?, |
| 184 | + }, |
| 185 | + )), |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments