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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#[cfg(feature = "maxmind")]
use maxminddb::geoip2::City;
#[cfg(feature = "serde")]
use serde::Serialize;

/// The location information that providers must produce.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Location {
    /// Country in ISO 3166-1 alpha-2 format, such as "MX" for Mexico or "IT" for Italy.
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub country: Option<String>,

    /// Region/region (e.g. a US state) in ISO 3166-2 format, such as "QC"
    /// for Quebec (with country = "CA") or "TX" for Texas (with country = "US").
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub region: Option<String>,

    /// City, listed by name such as "Portland" or "Berlin".
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub city: Option<String>,

    /// The Designated Market Area code, as defined by [Nielsen]. Only defined in the US.
    ///
    /// [Nielsen]: https://www.nielsen.com/us/en/contact-us/intl-campaigns/dma-maps/
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    pub dma: Option<u16>,

    /// The name of the provider that produced this recommendation.
    pub provider: String,
}

macro_rules! location_field {
    ($field: ident, $type: ty) => {
        location_field!(
            $field,
            $type,
            concat!(
                "Get an owned copy of the ",
                stringify!($field),
                ", or the default if the field is None"
            )
        );
    };

    ($field: ident, $type: ty, $doc: expr) => {
        #[doc = $doc]
        pub fn $field(&self) -> $type {
            self.$field.clone().unwrap_or_default()
        }
    };
}

impl Location {
    /// Create a builder for a [`Location`] that can be assembled incrementally.
    pub fn build() -> LocationBuilder {
        LocationBuilder::default()
    }

    location_field!(country, String);
    location_field!(region, String);
    location_field!(city, String);
    location_field!(dma, u16);
}

#[derive(Default)]
pub struct LocationBuilder {
    country: Option<String>,
    region: Option<String>,
    city: Option<String>,
    dma: Option<u16>,
    provider: Option<String>,
}

macro_rules! builder_field {
    ($field: ident, $type: ty) => {
        pub fn $field<O: Into<Option<$type>>>(mut self, $field: O) -> Self {
            self.$field = $field.into();
            self
        }
    };
}

impl LocationBuilder {
    builder_field!(country, String);
    builder_field!(region, String);
    builder_field!(city, String);
    builder_field!(dma, u16);
    builder_field!(provider, String);

    pub fn finish(self) -> Result<Location, ()> {
        Ok(Location {
            country: self.country,
            region: self.region,
            city: self.city,
            dma: self.dma,
            provider: self.provider.ok_or(())?,
        })
    }
}

#[cfg(feature = "maxmind")]
impl<'a> From<(City<'a>, &str)> for LocationBuilder {
    fn from((val, preferred_language): (City<'a>, &str)) -> Self {
        Location::build()
            .country(
                val.country
                    .and_then(|country| country.iso_code)
                    .map(String::from),
            )
            .region(
                val.subdivisions
                    // Subdivisions are listed in least-specific order. In the US, this might mean that subdivisions is state and then county. We want only the first.
                    .and_then(|subdivisions| {
                        subdivisions
                            .get(0)
                            .and_then(|subdivision| subdivision.iso_code)
                    })
                    .map(ToString::to_string),
            )
            .city(
                val.city
                    .and_then(|city| city.names)
                    .and_then(|names| names.get(preferred_language).map(|name| name.to_string()))
                    .map(|name| (*name).to_string()),
            )
            .dma(val.location.and_then(|location| location.metro_code))
    }
}

#[cfg(test)]
mod tests {
    use super::Location;

    #[test]
    fn builder_works() {
        let location = Location::build()
            .country("US".to_string())
            .region("OR".to_string())
            .city("Portland".to_string())
            .dma(810)
            .provider("test".to_string())
            .finish()
            .unwrap();

        assert_eq!(
            location,
            Location {
                country: Some("US".to_string()),
                region: Some("OR".to_string()),
                city: Some("Portland".to_string()),
                dma: Some(810),
                provider: "test".to_string()
            }
        );
    }

    #[test]
    fn methods_get_values() {
        let location = Location::build()
            .country("US".to_string())
            .region("CA".to_string())
            .city("Sunnyvale".to_string())
            .dma(807)
            .provider("test".to_string())
            .finish()
            .unwrap();

        assert_eq!(location.country(), "US");
        assert_eq!(location.region(), "CA");
        assert_eq!(location.city(), "Sunnyvale");
        assert_eq!(location.dma(), 807);
    }

    #[test]
    fn methods_get_defaults() {
        let location = Location::build()
            .provider("test".to_string())
            .finish()
            .unwrap();

        assert_eq!(location.country(), "");
        assert_eq!(location.region(), "");
        assert_eq!(location.city(), "");
        assert_eq!(location.dma(), 0);
    }

    #[cfg(maxmind)]
    #[actix_rt::test]
    async fn known_ip() {
        use maxminddb::geoip2::model::City;

        use crate::providers::tests::maxmind::{MMDB_LOC, TEST_ADDR_1};

        let mmdb = maxminddb::Reader::open_readfile(path)
            .map_err(|e| Error::Setup(anyhow!("{}", e)))
            .expect("could not create mmdb");
        let db_value = mmdb.lookup::<City>(TEST_ADDR_1);
        let location: Location = (db_value, "en").into();

        assert_eq!(
            location,
            Location::build()
                .country("US".to_string())
                .region("WA".to_string())
                .city("Milton".to_string())
                .dma(819)
                .provider("maxmind".to_string())
                .finish()
                .expect("bug when creating location")
        );
    }
}