Make it an error to specify both before and after for pagination
This commit is contained in:
parent
0ae50da9b7
commit
29c4180496
1 changed files with 25 additions and 7 deletions
|
@ -1,6 +1,23 @@
|
||||||
|
use std::{error, fmt};
|
||||||
|
|
||||||
use serde;
|
use serde;
|
||||||
use serde_urlencoded;
|
use serde_urlencoded;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Error;
|
||||||
|
|
||||||
|
impl fmt::Display for Error {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||||
|
write!(f, "{}", (self as &error::Error).description())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl error::Error for Error {
|
||||||
|
fn description(&self) -> &str {
|
||||||
|
"`after` and `before` are mutually exclusive"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct PaginationStruct<T> {
|
struct PaginationStruct<T> {
|
||||||
after: Option<T>,
|
after: Option<T>,
|
||||||
|
@ -14,16 +31,17 @@ pub enum Pagination<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> PaginationStruct<T> {
|
impl<T> PaginationStruct<T> {
|
||||||
fn into_enum(self) -> Pagination<T> {
|
fn into_enum(self) -> Result<Pagination<T>, Error> {
|
||||||
match (self.after, self.before) {
|
match (self.after, self.before) {
|
||||||
(Some(x), _) => Pagination::After(x),
|
(Some(x), None) => Ok(Pagination::After(x)),
|
||||||
(None, Some(x)) => Pagination::Before(x),
|
(None, Some(x)) => Ok(Pagination::Before(x)),
|
||||||
_ => Pagination::None,
|
(None, None) => Ok(Pagination::None),
|
||||||
|
_ => Err(Error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_str<'a, T: serde::Deserialize<'a>>(s: &'a str) -> Result<Pagination<T>, serde::de::value::Error> {
|
pub fn from_str<'a, T: serde::Deserialize<'a>>(s: &'a str) -> Result<Pagination<T>, Error> {
|
||||||
let pagination: PaginationStruct<T> = serde_urlencoded::from_str(s)?;
|
let pagination: PaginationStruct<T> = serde_urlencoded::from_str(s).map_err(|_| Error)?; // TODO Proper error reporting
|
||||||
Ok(pagination.into_enum())
|
Ok(pagination.into_enum()?)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue