Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. struct Pagination<'a, T> {
  2. offset: i64,
  3. limit: i64,
  4. total: i64,
  5. values: Vec<T>,
  6. }
  7.  
  8. impl<T> Deserialize for Pagination<T>
  9. where T: Deserialize
  10. {
  11. fn deserialize<D>(de: &mut D) -> Result<Self, D::Error>
  12. where D: Deserializer,
  13. {
  14. let mut object: BTreeMap<String, serde_json::Value> =
  15. try!(Deserialize::deserialize(de));
  16.  
  17. let offset = match object.pop("offset") {
  18. Some(offset) => {
  19. match v.as_i64() {
  20. Some(offset) => offset,
  21. None => { return Err(Error::syntax("offset is not an i64")); }
  22. }
  23. }
  24. None => { return Err(Error::missing_field("offset")); }
  25. };
  26.  
  27. let limit = match object.pop("limit") {
  28. Some(limit) => {
  29. match v.as_i64() {
  30. Some(limit) => limit,
  31. None => { return Err(Error::syntax("limit is not an i64")); }
  32. }
  33. }
  34. None => { return Err(Error::missing_field("limit")); }
  35. };
  36.  
  37. let total = match object.pop("total") {
  38. Some(total) => {
  39. match v.as_i64() {
  40. Some(total) => total,
  41. None => { return Err(Error::syntax("total is not an i64")); }
  42. }
  43. }
  44. None => { return Err(Error::missing_field("total")); }
  45. };
  46.  
  47. let iter = object.into_iter();
  48.  
  49. let item = match iter.next() {
  50. Some(item) => item,
  51. None => { return Err(Error::syntax("missing item")); }
  52. };
  53.  
  54. // Make sure there isn't anything else left.
  55. if iter.next().is_some() {
  56. Err(Error::syntax("too many items"))
  57. } else {
  58. match serde_json::from_value(values) {
  59. Ok(values) => {
  60. Ok(Pagination {
  61. offset: offset,,
  62. limit: limit,
  63. total: total,
  64. values: values,
  65. })
  66. }
  67. Err(err) => {
  68. Err(Error::syntax(err.description()))
  69. }
  70. }
  71.  
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement