facebook_graph_api_object_paging/
lib.rs

1//! https://developers.facebook.com/docs/graph-api/results
2
3// https://developers.facebook.com/docs/graph-api/results#cursors
4pub mod cursor_based_pagination {
5    use serde::{Deserialize, Serialize};
6
7    #[derive(Deserialize, Serialize, Debug, Clone)]
8    pub struct Paging {
9        pub cursors: PagingCursors,
10        pub previous: Option<String>,
11        pub next: Option<String>,
12    }
13
14    #[derive(Deserialize, Serialize, Debug, Clone)]
15    pub struct PagingCursors {
16        pub before: Option<Box<str>>,
17        pub after: Option<Box<str>>,
18    }
19
20    impl Paging {
21        pub fn next_cursor(&self) -> Option<String> {
22            if self.next.is_some() {
23                self.cursors.after.as_ref().map(|after| after.to_string())
24            } else {
25                None
26            }
27        }
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn test_de_paging_for_cursor_based_pagination() {
37        let paging: cursor_based_pagination::Paging = serde_json::from_str(
38            r#"{
39                "cursors": {
40                    "before": "QVFIUnk4X3VfWlhjc1ZA6R2FPNWJhMDNFWm90Qm1mT2hKUkF5NW1aa25hbWJiY0lnT1o3SWtKcW9UckxhcGR1YnBTd2x1aTktSWlrME4tUFljWU5tRTIwNldR",
41                    "after": "QVFIUktSN2czMUNMNUxHWmJGYWkzUFBKTjNObWlaVHEzYTBua1ZA2X1BDaEM0bXhGdkhGZAkxkeDlrX2tkRkpQclRUWjlSaEtLZAWV3TlRPeUoyQVJ1VFpYTjR3"
42                }
43            }"#,
44        ).unwrap();
45        assert_eq!(paging.next_cursor(), None);
46    }
47}