1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use base64::{
    alphabet,
    engine::{self, general_purpose},
    Engine as _,
};
use serde::{Deserialize, Serialize};

pub(crate) const BASE64_ENGINE: engine::GeneralPurpose =
    engine::GeneralPurpose::new(&alphabet::URL_SAFE, general_purpose::NO_PAD);

#[derive(Debug, Serialize, Deserialize)]
pub struct Paging {
    #[serde(rename = "cursors")]
    cursor: Cursor,
}

impl std::fmt::Display for Paging {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Paging {{ ")?;
        writeln!(f, "cursor: {}", self.cursor)?;
        writeln!(f, "}}")
    }
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Cursor {
    before: Option<String>,
    after: Option<String>,
}

impl std::fmt::Display for Cursor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Cursor {{ ")?;
        if let Some(before) = &self.before {
            writeln!(f, "before: {before}")?;
        }
        if let Some(after) = &self.after {
            writeln!(f, "after: {after}")?;
        }
        writeln!(f, "}}")
    }
}

pub struct PagingBuilder {
    paging: Paging,
}

impl PagingBuilder {
    const fn new() -> Self {
        Self { paging: Paging { cursor: Cursor { before: None, after: None } } }
    }

    #[must_use]
    pub fn before(mut self, before: i32) -> Self {
        self.paging.cursor.set_before(before);
        self
    }

    #[must_use]
    pub fn after(mut self, after: i32) -> Self {
        self.paging.cursor.set_after(after);
        self
    }

    #[must_use]
    pub fn build(self) -> Paging {
        self.paging
    }
}

impl Paging {
    #[must_use]
    pub const fn new() -> Self {
        Self { cursor: Cursor::new() }
    }

    #[must_use]
    pub const fn is_some(&self) -> bool {
        self.cursor.is_some()
    }

    #[must_use]
    pub const fn is_none(&self) -> bool {
        self.cursor.is_none()
    }

    #[must_use]
    pub fn to_vec(&self) -> Vec<(&str, String)> {
        self.cursor.to_vec()
    }

    #[must_use]
    pub const fn builder() -> PagingBuilder {
        PagingBuilder::new()
    }
}

impl Default for Paging {
    fn default() -> Self {
        Self::new()
    }
}

impl Cursor {
    #[must_use]
    pub const fn new() -> Self {
        Self { before: None, after: None }
    }

    fn set_before(&mut self, before: i32) {
        self.before = Some(BASE64_ENGINE.encode(format!("{{\"pos\":{before}}}")));
    }

    fn set_after(&mut self, after: i32) {
        self.after = Some(BASE64_ENGINE.encode(format!("{{\"pos\":{after}}}")));
    }

    #[must_use]
    pub const fn is_some(&self) -> bool {
        self.before.is_some() || self.after.is_some()
    }

    #[must_use]
    pub const fn is_none(&self) -> bool {
        self.before.is_none() && self.after.is_none()
    }

    #[must_use]
    pub fn to_vec(&self) -> Vec<(&str, String)> {
        let mut vec = Vec::new();
        if let Some(ref before) = self.before {
            vec.push(("before", before.clone()));
        }
        if let Some(ref after) = self.after {
            vec.push(("after", after.clone()));
        }
        vec
    }
}

impl Default for Cursor {
    fn default() -> Self {
        Self::new()
    }
}