Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Pagination<'a, T> {
- offset: i64,
- limit: i64,
- total: i64,
- values: Vec<T>,
- }
- impl<T> Deserialize for Pagination<T>
- where T: Deserialize
- {
- fn deserialize<D>(de: &mut D) -> Result<Self, D::Error>
- where D: Deserializer,
- {
- let mut object: BTreeMap<String, serde_json::Value> =
- try!(Deserialize::deserialize(de));
- let offset = match object.pop("offset") {
- Some(offset) => {
- match v.as_i64() {
- Some(offset) => offset,
- None => { return Err(Error::syntax("offset is not an i64")); }
- }
- }
- None => { return Err(Error::missing_field("offset")); }
- };
- let limit = match object.pop("limit") {
- Some(limit) => {
- match v.as_i64() {
- Some(limit) => limit,
- None => { return Err(Error::syntax("limit is not an i64")); }
- }
- }
- None => { return Err(Error::missing_field("limit")); }
- };
- let total = match object.pop("total") {
- Some(total) => {
- match v.as_i64() {
- Some(total) => total,
- None => { return Err(Error::syntax("total is not an i64")); }
- }
- }
- None => { return Err(Error::missing_field("total")); }
- };
- let iter = object.into_iter();
- let item = match iter.next() {
- Some(item) => item,
- None => { return Err(Error::syntax("missing item")); }
- };
- // Make sure there isn't anything else left.
- if iter.next().is_some() {
- Err(Error::syntax("too many items"))
- } else {
- match serde_json::from_value(values) {
- Ok(values) => {
- Ok(Pagination {
- offset: offset,,
- limit: limit,
- total: total,
- values: values,
- })
- }
- Err(err) => {
- Err(Error::syntax(err.description()))
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement