Skip to content

Add backward seek-based pagination support for the search endpoint #10793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
controllers/helpers/pagination: Add an option to enable seeking backward
  • Loading branch information
eth3lbert committed Mar 10, 2025
commit d1dc40bb84e7930ef05b22495a246e92d11ef49c
24 changes: 20 additions & 4 deletions src/controllers/helpers/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl PaginationOptions {
PaginationOptionsBuilder {
limit_page_numbers: false,
enable_seek: false,
enable_seek_backward: false,
enable_pages: true,
}
}
Expand Down Expand Up @@ -85,6 +86,7 @@ pub(crate) struct PaginationOptionsBuilder {
limit_page_numbers: bool,
enable_pages: bool,
enable_seek: bool,
enable_seek_backward: bool,
}

impl PaginationOptionsBuilder {
Expand All @@ -103,6 +105,12 @@ impl PaginationOptionsBuilder {
self
}

#[allow(dead_code)]
pub(crate) fn enable_seek_backward(mut self, enable: bool) -> Self {
self.enable_seek_backward = enable;
self
}

pub(crate) fn gather(self, parts: &Parts) -> AppResult<PaginationOptions> {
use axum::extract::Query;

Expand Down Expand Up @@ -143,11 +151,19 @@ impl PaginationOptionsBuilder {

Page::Numeric(numeric_page)
} else if let Some(s) = params.seek {
if !self.enable_seek {
return Err(bad_request("?seek= is not supported for this request"));
match s.starts_with('-') {
true if !self.enable_seek_backward => {
return Err(bad_request(
"seek backward ?seek=- is not supported for this request",
));
}
// TODO: add a varaint for seek backward
true => unimplemented!("seek backward is not yet implemented"),
false if !self.enable_seek => {
return Err(bad_request("?seek= is not supported for this request"));
}
false => Page::Seek(RawSeekPayload(s)),
}

Page::Seek(RawSeekPayload(s))
} else {
Page::Unspecified
};
Expand Down