Skip to main content

stakewiz_rs/
params.rs

1/// Optional query parameters supported by many Stakewiz endpoints.
2#[derive(Debug, Default, Clone)]
3pub struct QueryParams {
4    pub sort: Option<String>,
5    pub limit: Option<u32>,
6}
7
8impl QueryParams {
9    pub fn new() -> Self {
10        Self::default()
11    }
12
13    /// Sort ascending by a field (e.g. `"created_at"`, `"epoch"`, `"stake"`).
14    pub fn sort_asc(mut self, field: &str) -> Self {
15        self.sort = Some(field.to_string());
16        self
17    }
18
19    /// Sort descending by a field (prepends `-`).
20    pub fn sort_desc(mut self, field: &str) -> Self {
21        self.sort = Some(format!("-{field}"));
22        self
23    }
24
25    /// Limit number of results.
26    pub fn limit(mut self, n: u32) -> Self {
27        self.limit = Some(n);
28        self
29    }
30
31    pub(crate) fn to_query_pairs(&self) -> Vec<(&str, String)> {
32        let mut pairs = Vec::new();
33        if let Some(ref sort) = self.sort {
34            pairs.push(("sort", sort.clone()));
35        }
36        if let Some(limit) = self.limit {
37            pairs.push(("limit", limit.to_string()));
38        }
39        pairs
40    }
41}