smarty_rust_sdk/us_autocomplete_pro_api/
lookup.rs

1use crate::sdk::{has_param, has_vec_param};
2use crate::us_autocomplete_pro_api::suggestion::SuggestionListing;
3use serde::Serialize;
4
5#[derive(Clone, Debug, PartialEq)]
6pub struct Lookup {
7    pub search: String,
8    pub source: String,
9    pub max_results: i32,
10    pub city_filter: Vec<String>,
11    pub state_filter: Vec<String>,
12    pub zip_filter: Vec<String>,
13    pub exclude_states: Vec<String>,
14    pub prefer_city: Vec<String>,
15    pub prefer_state: Vec<String>,
16    pub prefer_zip: Vec<String>,
17    pub prefer_ratio: i32,
18    pub geolocation: Geolocation,
19
20    pub results: SuggestionListing,
21}
22
23impl Default for Lookup {
24    fn default() -> Self {
25        Lookup {
26            search: String::default(),
27            source: String::default(),
28            max_results: 0,
29            city_filter: vec![],
30            state_filter: vec![],
31            zip_filter: vec![],
32            exclude_states: vec![],
33            prefer_city: vec![],
34            prefer_state: vec![],
35            prefer_zip: vec![],
36            prefer_ratio: 0,
37            geolocation: Geolocation::default(),
38
39            results: SuggestionListing {
40                suggestions: vec![],
41            },
42        }
43    }
44}
45
46impl Lookup {
47    pub(crate) fn into_param_array(self) -> Vec<(String, String)> {
48        let geolocation_self = self.clone();
49        vec![
50            has_param("search".to_string(), self.search),
51            has_param("source".to_string(), self.source),
52            has_param("max_results".to_string(), self.max_results),
53            has_vec_param("include_only_cities".to_string(), ";", self.city_filter),
54            has_vec_param("include_only_states".to_string(), ";", self.state_filter),
55            has_vec_param("include_only_zip_codes".to_string(), ";", self.zip_filter),
56            has_vec_param("exclude_states".to_string(), ";", self.exclude_states),
57            has_vec_param("prefer_states".to_string(), ";", self.prefer_state),
58            has_vec_param("prefer_zip_codes".to_string(), ";", self.prefer_zip),
59            has_param("prefer_ratio".to_string(), self.prefer_ratio),
60            geolocation_self.geolocation_param(),
61        ]
62        .iter()
63        .filter_map(Option::clone)
64        .collect::<Vec<_>>()
65    }
66
67    fn geolocation_param(self) -> Option<(String, String)> {
68        if !self.zip_filter.is_empty() || !self.prefer_zip.is_empty() {
69            return Some(("prefer_geolocation".to_string(), "none".to_string()));
70        }
71
72        match self.geolocation {
73            Geolocation::GeolocateCity => {
74                Some(("prefer_geolocation".to_string(), "city".to_string()))
75            }
76            Geolocation::GeolocateNone => {
77                Some(("prefer_geolocation".to_string(), "none".to_string()))
78            }
79        }
80    }
81}
82
83#[derive(Default, Debug, Clone, PartialEq, Serialize)]
84pub enum Geolocation {
85    #[default]
86    #[serde(rename = "none")]
87    GeolocateNone,
88    #[serde(rename = "city")]
89    GeolocateCity,
90}