stakewiz-rs 0.1.0

Unofficial Rust client for the Stakewiz API - Solana validator analytics
Documentation
/// Optional query parameters supported by many Stakewiz endpoints.
#[derive(Debug, Default, Clone)]
pub struct QueryParams {
    pub sort: Option<String>,
    pub limit: Option<u32>,
}

impl QueryParams {
    pub fn new() -> Self {
        Self::default()
    }

    /// Sort ascending by a field (e.g. `"created_at"`, `"epoch"`, `"stake"`).
    pub fn sort_asc(mut self, field: &str) -> Self {
        self.sort = Some(field.to_string());
        self
    }

    /// Sort descending by a field (prepends `-`).
    pub fn sort_desc(mut self, field: &str) -> Self {
        self.sort = Some(format!("-{field}"));
        self
    }

    /// Limit number of results.
    pub fn limit(mut self, n: u32) -> Self {
        self.limit = Some(n);
        self
    }

    pub(crate) fn to_query_pairs(&self) -> Vec<(&str, String)> {
        let mut pairs = Vec::new();
        if let Some(ref sort) = self.sort {
            pairs.push(("sort", sort.clone()));
        }
        if let Some(limit) = self.limit {
            pairs.push(("limit", limit.to_string()));
        }
        pairs
    }
}