use super::enums::SortOrder;
use std::fmt::Display;
const DEFAULT_SORT_ORDER: SortOrder = SortOrder::Asc;
#[derive(Clone, Debug)]
pub struct ListCardsParameters {
pub cursor: String,
pub customer_id: String,
pub include_disabled: bool,
pub reference_id: String,
pub sort_order: SortOrder,
}
impl ListCardsParameters {
pub fn to_query_string(&self) -> String {
self.to_string()
}
}
impl Default for ListCardsParameters {
fn default() -> Self {
Self {
cursor: Default::default(),
customer_id: Default::default(),
include_disabled: Default::default(),
reference_id: Default::default(),
sort_order: DEFAULT_SORT_ORDER,
}
}
}
impl From<ListCardsParameters> for String {
fn from(list_cards_parameters: ListCardsParameters) -> Self {
list_cards_parameters.to_string()
}
}
impl Display for ListCardsParameters {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut params = Vec::new();
if !self.cursor.is_empty() {
params.push(format!("cursor={}", self.cursor));
}
if !self.customer_id.is_empty() {
params.push(format!("customer_id={}", self.customer_id));
}
if self.include_disabled {
params.push(String::from("include_disabled=true"));
}
if !self.reference_id.is_empty() {
params.push(format!("reference_id={}", self.reference_id));
}
if self.sort_order != DEFAULT_SORT_ORDER {
params.push(format!(
"sort_order={}",
serde_json::to_string(&self.sort_order).unwrap()
));
}
let str = if params.is_empty() {
String::new()
} else {
format!("?{}", params.join("&"))
};
write!(f, "{}", str)
}
}