Skip to main content

bpi_rs/note/
params.rs

1use crate::ids::{Aid, Cvid, NoteId};
2use crate::{BpiError, BpiResult};
3
4/// Parameters for `/x/note/is_forbid`.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub struct NoteIsForbidParams {
7    aid: Aid,
8}
9
10impl NoteIsForbidParams {
11    pub fn new(aid: Aid) -> Self {
12        Self { aid }
13    }
14
15    pub(crate) fn query_pairs(&self) -> Vec<(&'static str, String)> {
16        vec![("aid", self.aid.to_string())]
17    }
18}
19
20/// Parameters for `/x/note/info`.
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub struct NotePrivateInfoParams {
23    aid: Aid,
24    note_id: NoteId,
25}
26
27impl NotePrivateInfoParams {
28    pub fn new(aid: Aid, note_id: NoteId) -> Self {
29        Self { aid, note_id }
30    }
31
32    pub(crate) fn query_pairs(&self) -> Vec<(&'static str, String)> {
33        vec![
34            ("oid", self.aid.to_string()),
35            ("oid_type", "0".to_string()),
36            ("note_id", self.note_id.to_string()),
37        ]
38    }
39}
40
41/// Parameters for `/x/note/publish/info`.
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub struct NotePublicInfoParams {
44    cvid: Cvid,
45}
46
47impl NotePublicInfoParams {
48    pub fn new(cvid: Cvid) -> Self {
49        Self { cvid }
50    }
51
52    pub(crate) fn query_pairs(&self) -> Vec<(&'static str, String)> {
53        vec![("cvid", self.cvid.to_string())]
54    }
55}
56
57/// Parameters for `/x/note/list/archive`.
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]
59pub struct NoteArchiveListParams {
60    aid: Aid,
61}
62
63impl NoteArchiveListParams {
64    pub fn new(aid: Aid) -> Self {
65        Self { aid }
66    }
67
68    pub(crate) fn query_pairs(&self) -> Vec<(&'static str, String)> {
69        vec![("oid", self.aid.to_string()), ("oid_type", "0".to_string())]
70    }
71}
72
73/// Parameters for `/x/note/list`.
74#[derive(Debug, Clone, Copy, PartialEq, Eq)]
75pub struct NoteUserPrivateListParams {
76    pagination: NotePagination,
77}
78
79impl NoteUserPrivateListParams {
80    pub fn new() -> Self {
81        Self {
82            pagination: NotePagination::default(),
83        }
84    }
85
86    pub fn with_page(mut self, page: u32) -> BpiResult<Self> {
87        self.pagination.page = validate_positive("pn", page)?;
88        Ok(self)
89    }
90
91    pub fn with_page_size(mut self, page_size: u32) -> BpiResult<Self> {
92        self.pagination.page_size = validate_positive("ps", page_size)?;
93        Ok(self)
94    }
95
96    pub(crate) fn query_pairs(&self) -> Vec<(&'static str, String)> {
97        self.pagination.query_pairs()
98    }
99}
100
101impl Default for NoteUserPrivateListParams {
102    fn default() -> Self {
103        Self::new()
104    }
105}
106
107/// Parameters for `/x/note/publish/list/archive`.
108#[derive(Debug, Clone, Copy, PartialEq, Eq)]
109pub struct NotePublicArchiveListParams {
110    aid: Aid,
111    pagination: NotePagination,
112}
113
114impl NotePublicArchiveListParams {
115    pub fn new(aid: Aid) -> Self {
116        Self {
117            aid,
118            pagination: NotePagination::default(),
119        }
120    }
121
122    pub fn with_page(mut self, page: u32) -> BpiResult<Self> {
123        self.pagination.page = validate_positive("pn", page)?;
124        Ok(self)
125    }
126
127    pub fn with_page_size(mut self, page_size: u32) -> BpiResult<Self> {
128        self.pagination.page_size = validate_positive("ps", page_size)?;
129        Ok(self)
130    }
131
132    pub(crate) fn query_pairs(&self) -> Vec<(&'static str, String)> {
133        let mut params = vec![("oid", self.aid.to_string()), ("oid_type", "0".to_string())];
134        params.extend(self.pagination.query_pairs());
135        params
136    }
137}
138
139/// Parameters for `/x/note/publish/list/user`.
140#[derive(Debug, Clone, Copy, PartialEq, Eq)]
141pub struct NoteUserPublicListParams {
142    pagination: NotePagination,
143}
144
145impl NoteUserPublicListParams {
146    pub fn new() -> Self {
147        Self {
148            pagination: NotePagination::default(),
149        }
150    }
151
152    pub fn with_page(mut self, page: u32) -> BpiResult<Self> {
153        self.pagination.page = validate_positive("pn", page)?;
154        Ok(self)
155    }
156
157    pub fn with_page_size(mut self, page_size: u32) -> BpiResult<Self> {
158        self.pagination.page_size = validate_positive("ps", page_size)?;
159        Ok(self)
160    }
161
162    pub(crate) fn query_pairs(&self) -> Vec<(&'static str, String)> {
163        self.pagination.query_pairs()
164    }
165}
166
167impl Default for NoteUserPublicListParams {
168    fn default() -> Self {
169        Self::new()
170    }
171}
172
173#[derive(Debug, Clone, Copy, PartialEq, Eq)]
174struct NotePagination {
175    page: u32,
176    page_size: u32,
177}
178
179impl NotePagination {
180    fn query_pairs(&self) -> Vec<(&'static str, String)> {
181        vec![
182            ("pn", self.page.to_string()),
183            ("ps", self.page_size.to_string()),
184        ]
185    }
186}
187
188impl Default for NotePagination {
189    fn default() -> Self {
190        Self {
191            page: 1,
192            page_size: 10,
193        }
194    }
195}
196
197fn validate_positive(field: &'static str, value: u32) -> BpiResult<u32> {
198    if value == 0 {
199        return Err(BpiError::invalid_parameter(
200            field,
201            "value must be greater than zero",
202        ));
203    }
204
205    Ok(value)
206}
207
208#[cfg(test)]
209mod tests {
210    use super::*;
211
212    #[test]
213    fn note_public_info_params_serializes_cvid() -> BpiResult<()> {
214        let params = NotePublicInfoParams::new(Cvid::new(15_160_286)?);
215
216        assert_eq!(params.query_pairs(), vec![("cvid", "15160286".to_string())]);
217        Ok(())
218    }
219
220    #[test]
221    fn note_user_public_list_params_serializes_default_pagination() {
222        let params = NoteUserPublicListParams::new();
223
224        assert_eq!(
225            params.query_pairs(),
226            vec![("pn", "1".to_string()), ("ps", "10".to_string())]
227        );
228    }
229
230    #[test]
231    fn note_public_archive_list_params_rejects_zero_page_size() -> BpiResult<()> {
232        let err = NotePublicArchiveListParams::new(Aid::new(338_677_252)?)
233            .with_page_size(0)
234            .unwrap_err();
235
236        assert!(matches!(
237            err,
238            BpiError::InvalidParameter { field: "ps", .. }
239        ));
240        Ok(())
241    }
242}