#[derive(Debug, Default, Clone)]
pub struct QueryParams {
pub sort: Option<String>,
pub limit: Option<u32>,
}
impl QueryParams {
pub fn new() -> Self {
Self::default()
}
pub fn sort_asc(mut self, field: &str) -> Self {
self.sort = Some(field.to_string());
self
}
pub fn sort_desc(mut self, field: &str) -> Self {
self.sort = Some(format!("-{field}"));
self
}
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
}
}