diff --git a/src/resources/pagination.rs b/src/resources/pagination.rs index 6d78c20..de56967 100644 --- a/src/resources/pagination.rs +++ b/src/resources/pagination.rs @@ -1,6 +1,23 @@ +use std::{error, fmt}; + use serde; 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)] struct PaginationStruct { after: Option, @@ -14,16 +31,17 @@ pub enum Pagination { } impl PaginationStruct { - fn into_enum(self) -> Pagination { + fn into_enum(self) -> Result, Error> { match (self.after, self.before) { - (Some(x), _) => Pagination::After(x), - (None, Some(x)) => Pagination::Before(x), - _ => Pagination::None, + (Some(x), None) => Ok(Pagination::After(x)), + (None, Some(x)) => Ok(Pagination::Before(x)), + (None, None) => Ok(Pagination::None), + _ => Err(Error) } } } -pub fn from_str<'a, T: serde::Deserialize<'a>>(s: &'a str) -> Result, serde::de::value::Error> { - let pagination: PaginationStruct = serde_urlencoded::from_str(s)?; - Ok(pagination.into_enum()) +pub fn from_str<'a, T: serde::Deserialize<'a>>(s: &'a str) -> Result, Error> { + let pagination: PaginationStruct = serde_urlencoded::from_str(s).map_err(|_| Error)?; // TODO Proper error reporting + Ok(pagination.into_enum()?) }