square_api_client/models/
list_customers_parameters.rs

1//! Model struct for ListCustomersParameters (query parameters)
2
3use super::enums::{SortCustomersField, SortOrder};
4
5const DEFAULT_SORT_ORDER: SortOrder = SortOrder::Asc;
6const DEFAULT_SORT_CUSTOMER_FIELD: SortCustomersField = SortCustomersField::CreatedAt;
7
8/// This is a model struct for ListCustomersParameters (query parameters)
9#[derive(Clone, Debug)]
10pub struct ListCustomersParameters {
11    /// A pagination cursor returned by a previous call to this endpoint. Provide this to
12    /// retrieve the next set of results for your original query.
13    /// See [Pagination](https://developer.squareup.com/docs/basics/api101/pagination) for
14    /// more information.
15    pub cursor: String,
16    /// Sorts the returned list by when the card was created with the specified order. This field
17    /// defaults to ASC.
18    pub sort_order: SortOrder,
19    /// Sorts based on a particular field. By default, customers are sorted alphanumerically
20    /// by concatenating their given_name and family_name.
21    /// If neither name field is set, string comparison is performed using one of the remaining fields
22    /// in the following order: company_name, email, phone_number.
23    pub sort_field: SortCustomersField,
24}
25
26impl ListCustomersParameters {
27    pub fn to_query_string(&self) -> String {
28        self.to_string()
29    }
30}
31
32impl From<ListCustomersParameters> for String {
33    fn from(list_customers_parameters: ListCustomersParameters) -> Self {
34        list_customers_parameters.to_string()
35    }
36}
37impl Default for ListCustomersParameters {
38    fn default() -> Self {
39        Self {
40            cursor: Default::default(),
41            sort_order: DEFAULT_SORT_ORDER,
42            sort_field: DEFAULT_SORT_CUSTOMER_FIELD,
43        }
44    }
45}
46
47impl ToString for ListCustomersParameters {
48    fn to_string(&self) -> String {
49        let mut params = Vec::new();
50
51        if !self.cursor.is_empty() {
52            params.push(format!("cursor={}", self.cursor));
53        }
54
55        if self.sort_field != DEFAULT_SORT_CUSTOMER_FIELD {
56            params.push(format!("sort_field={}", self.sort_field.to_string()));
57        }
58
59        if self.sort_order != DEFAULT_SORT_ORDER {
60            params.push(format!("sort_order={}", serde_json::to_string(&self.sort_order).unwrap()));
61        }
62
63        if params.is_empty() {
64            String::new()
65        } else {
66            format!("?{}", params.join("&"))
67        }
68    }
69}