libtad_models/places/
geo.rs

1use super::Country;
2use serde::Deserialize;
3
4#[derive(Debug, Deserialize)]
5/// Geographical information about a location.
6pub struct Geo {
7    /// The name of the location.
8    pub name: String,
9
10    /// The state of the location within the country (only if applicable).
11    pub state: Option<String>,
12
13    /// Country of the location.
14    pub country: Country,
15
16    /// Geographical latitude of the location.
17    #[serde(
18        deserialize_with = "custom_deserialize::float_or_empty_string",
19        default
20    )]
21    pub latitude: Option<f32>,
22
23    /// Geographical longitude of the location.
24    #[serde(
25        deserialize_with = "custom_deserialize::float_or_empty_string",
26        default
27    )]
28    pub longitude: Option<f32>,
29}
30
31mod custom_deserialize {
32    use serde::de::{self, Deserializer, Visitor};
33
34    pub fn float_or_empty_string<'de, D>(deserializer: D) -> Result<Option<f32>, D::Error>
35    where
36        D: Deserializer<'de>,
37    {
38        struct FloatOrString;
39
40        impl<'de> Visitor<'de> for FloatOrString {
41            type Value = Option<f32>;
42
43            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
44                formatter.write_str("float or empty string")
45            }
46
47            fn visit_str<E>(self, _: &str) -> Result<Self::Value, E>
48            where
49                E: de::Error,
50            {
51                Ok(None)
52            }
53
54            fn visit_f64<E>(self, s: f64) -> Result<Self::Value, E>
55            where
56                E: de::Error,
57            {
58                Ok(Some(s as f32))
59            }
60        }
61        deserializer.deserialize_any(FloatOrString)
62    }
63}