Skip to content

Instantly share code, notes, and snippets.

@goyox86
Created March 10, 2017 16:14
Show Gist options
  • Save goyox86/896947c0c3c621e8381bc1f298f2cf47 to your computer and use it in GitHub Desktop.
Save goyox86/896947c0c3c621e8381bc1f298f2cf47 to your computer and use it in GitHub Desktop.
use std::default::Default;
const DEFAULT_PER_PAGE: i64 = 10;
const DEFAULT_PAGE: i64 = 1;
#[derive(FromForm)]
pub struct Pagination {
per_page: Option<i64>,
page: Option<i64>
}
impl Default for Pagination {
fn default() -> Self {
Self {
per_page: Some(DEFAULT_PER_PAGE),
page: Some(DEFAULT_PAGE)
}
}
}
impl Pagination {
pub fn get_per_page(&self) -> i64 {
self.per_page.unwrap_or(DEFAULT_PER_PAGE)
}
pub fn get_page(&self) -> i64 {
self.page.unwrap_or(DEFAULT_PAGE)
}
}
#[get("/posts?<pagination>", format = "application/json")]
fn index_paginated(db: State<Db>, pagination: Pagination) -> EndpointResult<JSON<Value>> {
let results = published_posts(&*db, Some(&pagination))?;
Ok(JSON(json!(results)))
}
// I would like to do:
#[get("/posts?<pagination>", format = "application/json")]
fn index_paginated(db: State<Db>, pagination: &Pagination) -> EndpointResult<JSON<Value>> {
let results = published_posts(&*db, Some(pagination))?;
Ok(JSON(json!(results)))
}
// But rust does not like it
error[E0277]: the trait bound `&endpoints::pagination::Pagination: rocket::request::FromForm<'_>` is not satisfied
--> src/endpoints/api_v1/posts.rs:29:1
|
29 | #[get("/posts?<pagination>", format = "application/json")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `rocket::request::FromForm<'_>` is not implemented for `&endpoints::pagination::Pagination`
|
= help: the following implementations were found:
<endpoints::pagination::Pagination as rocket::request::FromForm<'rocket>>
= note: required by `rocket::request::FromForm::from_form_items`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment