address_formatter/
place.rs

1use enum_map::{Enum, EnumMap};
2use serde::Serialize;
3use strum_macros::{Display, EnumIter, EnumString};
4
5/// A `Component` is a field of a [`Place`](struct.Place.html)
6#[derive(Enum, EnumString, Debug, Clone, EnumIter, Copy, Hash, Display, Eq, PartialEq)]
7#[strum(serialize_all = "snake_case")]
8pub enum Component {
9    /// Leftover field. Can hold a name of a POI, name of building, ... will often be display first
10    Attention,
11    /// house_number of the place
12    HouseNumber,
13    /// house of the place
14    House,
15    /// road of the place
16    Road,
17    /// village of the place
18    Village,
19    /// suburb of the place
20    Suburb,
21    /// city of the place
22    City,
23    /// county of the place
24    County,
25    /// county_code of the place
26    CountyCode,
27    /// postcode of the place
28    Postcode,
29    /// state_district of the place
30    StateDistrict,
31    /// state of the place
32    State,
33    /// state_code of the place
34    StateCode,
35    /// region of the place
36    Region,
37    /// island of the place
38    Island,
39    /// neighbourhood of the place
40    Neighbourhood,
41    /// country of the place
42    Country,
43    /// country_code of the place
44    CountryCode,
45    /// continent of the place
46    Continent,
47    /// town of the place
48    Town,
49    /// city_district of the place
50    CityDistrict,
51}
52
53impl serde::Serialize for Component {
54    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
55    where
56        S: ::serde::Serializer,
57    {
58        self.to_string().serialize(serializer)
59    }
60}
61
62/// A [`Place`](struct.Place.html) is a structured way to represent a postal address.
63///
64///
65/// Note: it is internally represented as an EnumMap to easily loop over all the fields
66#[derive(Debug, Default, Serialize)]
67pub struct Place(EnumMap<Component, Option<String>>);
68
69impl std::ops::Deref for Place {
70    type Target = EnumMap<Component, Option<String>>;
71    fn deref(&self) -> &Self::Target {
72        &self.0
73    }
74}
75impl std::ops::DerefMut for Place {
76    fn deref_mut(&mut self) -> &mut EnumMap<Component, Option<String>> {
77        &mut self.0
78    }
79}
80
81impl<'a, T> From<T> for Place
82where
83    T: IntoIterator<Item = (Component, &'a str)>,
84{
85    fn from(data: T) -> Self {
86        let mut a = Self::default();
87        for (k, v) in data.into_iter() {
88            a[k] = Some(v.to_owned());
89        }
90        a
91    }
92}