Skip to content

Commit 7b91ca3

Browse files
committed
controllers/helpers/pagination: Add PaginationOptions::is_explicit() fn
This also remove the obsolete `Paginated::is_explicit_page()`.
1 parent 6bb6793 commit 7b91ca3

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

src/controllers/helpers/pagination.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ impl PaginationOptions {
6060
pub(crate) fn is_backward(&self) -> bool {
6161
matches!(self.page, Page::SeekBackward(_))
6262
}
63+
64+
pub(crate) fn is_explicit(&self) -> bool {
65+
matches!(self.page, Page::Numeric(_))
66+
}
6367
}
6468

6569
#[derive(Debug, Deserialize, FromRequestParts, utoipa::IntoParams)]
@@ -251,7 +255,8 @@ impl<T> Paginated<T> {
251255
S: Serialize,
252256
{
253257
// TODO: handle this more properly when seek backward comes into play.
254-
if self.is_explicit_page() || self.records_and_total.len() < self.options.per_page as usize
258+
if self.options.is_explicit()
259+
|| self.records_and_total.len() < self.options.per_page as usize
255260
{
256261
return Ok(None);
257262
}
@@ -267,10 +272,6 @@ impl<T> Paginated<T> {
267272
Ok(Some(opts))
268273
}
269274

270-
fn is_explicit_page(&self) -> bool {
271-
matches!(&self.options.page, Page::Numeric(_))
272-
}
273-
274275
pub(crate) fn iter(&self) -> impl Iterator<Item = &T> {
275276
self.records_and_total.iter().map(|row| &row.record)
276277
}

src/controllers/krate/search.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::schema::*;
2121
use crate::util::errors::{AppResult, bad_request};
2222
use crate::views::EncodableCrate;
2323

24-
use crate::controllers::helpers::pagination::{Page, PaginationOptions, PaginationQueryParams};
24+
use crate::controllers::helpers::pagination::{PaginationOptions, PaginationQueryParams};
2525
use crate::models::krate::ALL_COLUMNS;
2626
use crate::util::RequestUtils;
2727
use crate::util::string_excl_null::StringExclNull;
@@ -219,12 +219,10 @@ pub async fn list_crates(
219219
};
220220
}
221221

222-
let explicit_page = matches!(pagination.page, Page::Numeric(_));
223-
224222
// To avoid breaking existing users, seek-based pagination is only used if an explicit page has
225223
// not been provided. This way clients relying on meta.next_page will use the faster seek-based
226224
// paginations, while client hardcoding pages handling will use the slower offset-based code.
227-
let (total, next_page, prev_page, data) = if !explicit_page && seek.is_some() {
225+
let (total, next_page, prev_page, data) = if !pagination.is_explicit() && seek.is_some() {
228226
let seek = seek.unwrap();
229227
if let Some(condition) = seek
230228
.decode(&pagination.page)?

src/controllers/krate/versions.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,10 @@ async fn list_by_date(
155155
let mut query = make_base_query();
156156

157157
if let Some(options) = options {
158-
assert!(
159-
!matches!(&options.page, Page::Numeric(_)),
160-
"?page= is not supported"
161-
);
162-
if let Some(SeekPayload::Date(Date { created_at, id })) = Seek::Date.decode(&options.page)? {
158+
assert!(!options.is_explicit(), "?page= is not supported");
159+
if let Some(SeekPayload::Date(Date { created_at, id })) =
160+
Seek::Date.decode(&options.page)?
161+
{
163162
query = query.filter(
164163
versions::created_at
165164
.eq(created_at)
@@ -282,10 +281,7 @@ async fn list_by_semver(
282281
sorted_versions
283282
.sort_unstable_by(|_, (semver_a, _, _), _, (semver_b, _, _)| semver_b.cmp(semver_a));
284283

285-
assert!(
286-
!matches!(&options.page, Page::Numeric(_)),
287-
"?page= is not supported"
288-
);
284+
assert!(!options.is_explicit(), "?page= is not supported");
289285

290286
let release_tracks = include.release_tracks.then(|| {
291287
ReleaseTracks::from_sorted_semver_iter(
@@ -422,7 +418,7 @@ where
422418
F: Fn(&T) -> S,
423419
S: serde::Serialize,
424420
{
425-
if matches!(options.page, Page::Numeric(_)) || records.len() < options.per_page as usize {
421+
if options.is_explicit() || records.len() < options.per_page as usize {
426422
return Ok(None);
427423
}
428424

0 commit comments

Comments
 (0)