google_places_api/models/constants/
place.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use crate::models::Photo;

#[derive(Debug, Serialize, Deserialize)]
pub struct PlaceDetailsPlace {
    // Basic
    #[serde(rename = "place_id")]
    pub id: String,
    pub name: Option<String>,
    pub address_components: Option<Vec<AddressComponent>>,
    pub adr_address: Option<String>,
    pub business_status: Option<String>,
    pub formatted_address: Option<String>,
    pub geometry: Option<Geometry>,
    pub icon: Option<String>,
    pub icon_mask_base_uri: Option<String>,
    pub icon_background_color: Option<String>,
    pub photos: Option<Vec<Photo>>,
    pub plus_code: Option<PlusCode>,
    pub types: Option<Vec<String>>,
    pub url: Option<String>,
    pub utc_offset: Option<i32>,
    pub vicinity: Option<String>,
    pub wheelchair_accessible_entrance: Option<bool>,

    // Contact
    pub current_opening_hours: Option<PlaceOpeningHours>,
    pub formatted_phone_number: Option<String>,
    pub international_phone_number: Option<String>,
    pub opening_hours: Option<OpeningHours>,
    pub secondary_opening_hours: Option<Vec<PlaceOpeningHours>>,
    pub website: Option<String>,

    // Atmosphere
    pub curbside_pickup: Option<bool>,
    pub delivery: Option<bool>,
    pub dine_in: Option<bool>,
    pub editorial_summary: Option<PlaceEditorialSummary>,
    pub price_level: Option<i32>,
    pub rating: Option<f32>,
    pub reservable: Option<bool>,
    pub reviews: Option<Vec<Review>>,
    pub serves_beer: Option<bool>,
    pub serves_breakfast: Option<bool>,
    pub serves_brunch: Option<bool>,
    pub serves_dinner: Option<bool>,
    pub serves_lunch: Option<bool>,
    pub serves_vegetarian_food: Option<bool>,
    pub serves_wine: Option<bool>,
    pub takeout: Option<bool>,
    pub user_ratings_total: Option<i32>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PlaceSearchPlace {
    // Basic
    #[serde(rename = "place_id")]
    pub id: String,
    pub name: Option<String>,
    pub business_status: Option<String>,
    pub formatted_address: Option<String>,
    pub geometry: Option<Geometry>,
    pub icon: Option<String>,
    pub icon_mask_base_uri: Option<String>,
    pub icon_background_color: Option<String>,
    pub photos: Option<Vec<Photo>>,
    pub plus_code: Option<PlusCode>,
    pub types: Option<Vec<String>>,
    pub vicinity: Option<String>,

    // Contact
    pub opening_hours: Option<OpeningHours>,
    
    // Atmosphere
    pub price_level: Option<i32>,
    pub rating: Option<f32>,
    pub user_ratings_total: Option<i32>,
}


#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddressComponent {
    pub long_name: Option<String>,
    pub short_name: Option<String>,
    pub types: Option<Vec<String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Geometry {
    pub location: Option<Location>,
    pub viewport: Option<Viewport>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Location {
    pub lat: Option<f64>,
    pub lon: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Viewport {
    pub northeast: Option<Location>,
    pub southwest: Option<Location>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpeningHours {
    pub open_now: Option<bool>,
    pub periods: Option<Vec<Period>>,
    pub weekday_text: Option<Vec<String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Period {
    pub open: Option<DayTime>,
    pub close: Option<DayTime>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DayTime {
    pub day: Option<i32>,
    pub time: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlusCode {
    pub compound_code: Option<String>,
    pub global_code: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Review {
    pub author_name: String,
    pub author_url: Option<String>,
    pub language: Option<String>,
    pub original_language: Option<String>,
    pub translated: Option<bool>,
    pub profile_photo_url: Option<String>,
    pub rating: i32,
    pub relative_time_description: String,
    pub text: Option<String>,
    pub time: i64,
}

// Additional structs

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlaceOpeningHours {
    pub open_now: Option<bool>,
    pub periods: Option<Vec<OpeningHoursPeriod>>,
    pub special_days: Option<Vec<PlaceSpecialDay>>,
    #[serde(rename = "type")]
    pub place_opening_hours_type: Option<String>,
    pub weekday_text: Option<Vec<String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlaceSpecialDay {
    pub date: Option<String>,
    pub exceptional_hours: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpeningHoursPeriod {
    pub open: Option<OpeningHoursTime>,
    pub close: Option<OpeningHoursTime>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpeningHoursTime {
    pub day: Option<i32>,
    pub time: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlaceEditorialSummary {
    pub body: Option<String>,
    pub attribution: Option<String>,
    pub language: Option<String>,
    pub overview: Option<String>,
}

impl PlaceDetailsPlace {
    pub fn display(&self) -> String {
        let json_value: Value = json!(self);
        let cleaned_value = remove_empty_fields(&json_value);
        serde_json::to_string_pretty(&cleaned_value).unwrap_or_else(|_| String::from("Error formatting Place"))
    }
}

impl PlaceSearchPlace {
    pub fn display(&self) -> String {
        let json_value: Value = json!(self);
        let cleaned_value = remove_empty_fields(&json_value);
        serde_json::to_string_pretty(&cleaned_value).unwrap_or_else(|_| String::from("Error formatting Place"))
    }
}

fn remove_empty_fields(value: &Value) -> Value {
    match value {
        Value::Object(obj) => {
            let cleaned_fields: serde_json::Map<String, Value> = obj
                .iter()
                .filter(|(_, v)| !v.is_null())
                .map(|(k, v)| (k.clone(), remove_empty_fields(v)))
                .collect();

            Value::Object(cleaned_fields)
        }
        Value::Array(arr) => {
            let cleaned_array: Vec<Value> = arr
                .iter()
                .map(|v| remove_empty_fields(v))
                .collect();

            Value::Array(cleaned_array)
        }
        _ => value.clone(),
    }
}