libtad_models/places/
geo.rs1use super::Country;
2use serde::Deserialize;
3
4#[derive(Debug, Deserialize)]
5pub struct Geo {
7 pub name: String,
9
10 pub state: Option<String>,
12
13 pub country: Country,
15
16 #[serde(
18 deserialize_with = "custom_deserialize::float_or_empty_string",
19 default
20 )]
21 pub latitude: Option<f32>,
22
23 #[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}