1use crate::enums::{Department, Region};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize)]
6#[serde(tag = "locationType")]
7pub enum Location {
8 #[serde(rename = "region")]
9 Region { region_id: String },
10 #[serde(rename = "department")]
11 Department {
12 region_id: String,
13 department_id: String,
14 },
15 #[serde(rename = "city")]
16 City {
17 area: Area,
18 city: Option<String>,
19 label: Option<String>,
20 },
21 #[serde(rename = "place")]
22 Place {
23 place: String,
24 label: String,
25 area: Area,
26 },
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct Area {
31 pub lat: f64,
32 pub lng: f64,
33 #[serde(skip_serializing_if = "Option::is_none")]
34 pub radius: Option<u32>,
35 #[serde(skip_serializing_if = "Option::is_none")]
36 pub default_radius: Option<u32>,
37}
38
39#[derive(Debug, Clone)]
41pub struct City {
42 pub lat: f64,
43 pub lng: f64,
44 pub radius: Option<u32>,
45 pub city: Option<String>,
46}
47
48impl City {
49 pub fn new(lat: f64, lng: f64, radius: Option<u32>, city: Option<String>) -> Self {
50 Self {
51 lat,
52 lng,
53 radius,
54 city,
55 }
56 }
57}
58
59impl From<City> for Location {
60 fn from(city: City) -> Self {
61 Location::City {
62 area: Area {
63 lat: city.lat,
64 lng: city.lng,
65 radius: city.radius,
66 default_radius: None,
67 },
68 city: city.city.clone(),
69 label: city.city.map(|c| format!("{c} (toute la ville)")),
70 }
71 }
72}
73
74impl From<Region> for Location {
75 fn from(region: Region) -> Self {
76 Location::Region {
77 region_id: region.id().to_string(),
78 }
79 }
80}
81
82impl From<Department> for Location {
83 fn from(department: Department) -> Self {
84 Location::Department {
85 region_id: department.region_id().to_string(),
86 department_id: department.id().to_string(),
87 }
88 }
89}
90
91#[derive(Debug, Clone, Deserialize)]
93pub struct LocationDetails {
94 pub country_id: Option<String>,
95 pub region_id: Option<String>,
96 pub region_name: Option<String>,
97 pub department_id: Option<String>,
98 pub department_name: Option<String>,
99 pub city_label: Option<String>,
100 pub city: Option<String>,
101 pub zipcode: Option<String>,
102 pub lat: Option<f64>,
103 pub lng: Option<f64>,
104 pub source: Option<String>,
105 pub provider: Option<String>,
106 pub is_shape: Option<bool>,
107}