assignmentlib/jsonb/
geo_point.rs

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
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct GeoPoint {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub latlng: Option<LatLng>,
}

impl GeoPoint {
    pub fn normalize(&self) -> GeoPoint {
        GeoPoint {
            id: Option::from(if self.id.is_none() {
                Uuid::new_v4()
            } else {
                self.id.unwrap()
            }),
            name: self.name.clone(),
            latlng: self.latlng.clone(),
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct LatLng {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lat: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lng: Option<f64>,
}

impl LatLng {
    pub fn normalize(&self) -> LatLng {
        LatLng {
            lat: self.lat.clone(),
            lng: self.lng.clone(),
        }
    }
}