1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::sdk::{has_f64_param, has_i32_param, has_param, has_vec_param};
use crate::us_autocomplete_api::suggestion::SuggestionListing;
use serde::Serialize;

#[derive(Clone, Debug, PartialEq)]
pub struct Lookup {
    pub prefix: String,
    pub max_suggestions: i32,
    pub city_filter: Vec<String>,
    pub state_filter: Vec<String>,
    pub preferences: Vec<String>,
    pub geolocation: Geolocation,
    pub prefer_ratio: f64,

    pub results: SuggestionListing,
}

impl Default for Lookup {
    fn default() -> Self {
        Lookup {
            prefix: String::default(),
            max_suggestions: 0,
            city_filter: vec![],
            state_filter: vec![],
            prefer_ratio: 0.0,
            geolocation: Geolocation::default(),

            results: SuggestionListing {
                suggestions: vec![],
            },
            preferences: vec![],
        }
    }
}

impl Lookup {
    pub(crate) fn into_param_array(self) -> Vec<(String, String)> {
        vec![
            has_param("prefix".to_string(), self.prefix),
            has_i32_param("max_suggestions".to_string(), self.max_suggestions, 0),
            has_vec_param("city_filter".to_string(), ",", self.city_filter),
            has_vec_param("state_filter".to_string(), ",", self.state_filter),
            has_vec_param("preferences".to_string(), ",", self.preferences),
            self.geolocation.geolocation_to_param(),
            has_f64_param("prefer_ratio".to_string(), self.prefer_ratio, 0.0),
        ]
        .iter()
        .filter_map(Option::clone)
        .collect::<Vec<_>>()
    }
}

#[derive(Default, Debug, Clone, PartialEq, Serialize)]
pub enum Geolocation {
    #[default]
    #[serde(rename = "none")]
    GeolocateNone,
    #[serde(rename = "city")]
    GeolocateCity,
    #[serde(rename = "state")]
    GeolocateState,
}

impl Geolocation {
    fn geolocation_to_param(self) -> Option<(String, String)> {
        match self {
            Geolocation::GeolocateNone => Some(("geolocate".to_string(), "false".to_string())),
            Geolocation::GeolocateCity => None,
            Geolocation::GeolocateState => {
                Some(("geolocate_precision".to_string(), "state".to_string()))
            }
        }
    }
}