square_api_client/models/
list_cards_parameters.rs

1//! Model struct for ListCardsParameters (query parameters)
2
3use super::enums::SortOrder;
4
5const DEFAULT_SORT_ORDER: SortOrder = SortOrder::Asc;
6
7/// This is a model struct for ListCardsParameters (query parameters)
8#[derive(Clone, Debug)]
9pub struct ListCardsParameters {
10    /// A pagination cursor returned by a previous call to this endpoint. Provide this to
11    /// retrieve the next set of results for your original query.
12    /// See [Pagination](https://developer.squareup.com/docs/basics/api101/pagination) for
13    /// more information.
14    pub cursor: String,
15    /// Limit results to cards associated with the customer supplied. By default, all cards owned
16    /// by the merchant are returned.
17    pub customer_id: String,
18    /// Includes disabled cards. By default, all enabled cards owned by the merchant are returned.
19    pub include_disabled: bool,
20    /// Limit results to cards associated with the reference_id supplied.
21    pub reference_id: String,
22    /// Sorts the returned list by when the card was created with the specified order. This field
23    /// defaults to ASC.
24    pub sort_order: SortOrder,
25}
26
27impl ListCardsParameters {
28    pub fn to_query_string(&self) -> String {
29        self.to_string()
30    }
31}
32
33impl Default for ListCardsParameters {
34    fn default() -> Self {
35        Self {
36            cursor: Default::default(),
37            customer_id: Default::default(),
38            include_disabled: Default::default(),
39            reference_id: Default::default(),
40            sort_order: DEFAULT_SORT_ORDER,
41        }
42    }
43}
44
45impl From<ListCardsParameters> for String {
46    fn from(list_cards_parameters: ListCardsParameters) -> Self {
47        list_cards_parameters.to_string()
48    }
49}
50
51impl ToString for ListCardsParameters {
52    fn to_string(&self) -> String {
53        let mut params = Vec::new();
54
55        if !self.cursor.is_empty() {
56            params.push(format!("cursor={}", self.cursor));
57        }
58
59        if !self.customer_id.is_empty() {
60            params.push(format!("customer_id={}", self.customer_id));
61        }
62
63        if self.include_disabled {
64            params.push(String::from("include_disabled=true"));
65        }
66
67        if !self.reference_id.is_empty() {
68            params.push(format!("reference_id={}", self.reference_id));
69        }
70
71        if self.sort_order != DEFAULT_SORT_ORDER {
72            params.push(format!("sort_order={}", serde_json::to_string(&self.sort_order).unwrap()));
73        }
74
75        if params.is_empty() {
76            String::new()
77        } else {
78            format!("?{}", params.join("&"))
79        }
80    }
81}