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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
use crate::types::constants::place::Location;
use crate::types::constants::{Language, PlaceSearchPlace, PlaceTypes};
use crate::types::TextSearchResult;
use reqwest::Client;
use std::time::Duration;
use tokio::time::sleep;
pub struct TextSearch<'a> {
text_query: Option<String>,
radius: Option<f64>,
language: Option<Language>,
location: Option<Location>,
maxprice: Option<u8>,
minprice: Option<u8>,
opennow: Option<bool>,
pagetoken: Option<String>,
region: Option<String>,
place_type: Option<String>,
api_key: String,
client: &'a Client,
result: TextSearchResult,
}
impl<'a> TextSearch<'a> {
pub fn new(api_key: &str, client: &'a Client) -> Self {
Self {
text_query: None,
radius: None,
language: None,
location: None,
maxprice: None,
minprice: None,
opennow: None,
pagetoken: None,
region: None,
place_type: None,
api_key: String::from(api_key),
client: client,
result: Default::default(),
}
}
/**
Assign the query string for a TextSearch call.
text_query -> The query text.
*/
pub fn with_query(&mut self, text_query: &str) -> &mut TextSearch<'a> {
self.text_query = Some(String::from(text_query));
self
}
/**
Assign the radius for a TextSearch call.
radius -> The search radius.
*/
pub fn with_radius(&mut self, radius: f64) -> &mut TextSearch<'a> {
self.radius = Some(radius);
self
}
/**
Assign the language for a TextSearch call.
language -> The language parameter.
*/
pub fn with_language(&mut self, language: Language) -> &mut TextSearch<'a> {
self.language = Some(language);
self
}
/**
Assign the location for a TextSearch call.
location -> The location parameter.
*/
pub fn with_location(&mut self, location: Location) -> &mut TextSearch<'a> {
self.location = Some(location);
self
}
/**
Assign the max price for a TextSearch call.
maxprice -> The maximum price level.
*/
pub fn with_maxprice(&mut self, maxprice: u8) -> &mut TextSearch<'a> {
self.maxprice = Some(maxprice);
self
}
/**
Assign the min price for a TextSearch call.
minprice -> The minimum price level.
*/
pub fn with_minprice(&mut self, minprice: u8) -> &mut TextSearch<'a> {
self.minprice = Some(minprice);
self
}
/**
Assign the open now filter for a TextSearch call.
opennow -> Whether the search should only include places that are open now.
*/
pub fn with_opennow(&mut self, opennow: bool) -> &mut TextSearch<'a> {
self.opennow = Some(opennow);
self
}
/**
Assign the page token for a TextSearch call.
pagetoken -> The page token for the results.
*/
pub fn with_pagetoken(&mut self, pagetoken: &str) -> &mut TextSearch<'a> {
self.pagetoken = Some(String::from(pagetoken));
self
}
/**
Assign the region for a TextSearch call.
region -> The region parameter.
*/
pub fn with_region(&mut self, region: &str) -> &mut TextSearch<'a> {
self.region = Some(String::from(region));
self
}
/**
Assign the place type for a TextSearch call.
place_type -> The type of place.
*/
pub fn with_type(&mut self, place_type: PlaceTypes) -> &mut TextSearch<'a> {
self.place_type = Some(place_type.to_string());
self
}
fn build_params(&self) -> Vec<(&'static str, String)> {
let mut params = vec![("key", self.api_key.clone())];
if let Some(text_query) = self.text_query.clone() {
params.push(("query", text_query));
}
if let Some(radius) = self.radius {
params.push(("radius", radius.to_string()));
}
if let Some(language) = self.language {
params.push(("language", language.to_string()));
}
if let Some(location) = self.location.clone() {
params.push(("location", location.to_string()));
}
if let Some(maxprice) = self.maxprice {
params.push(("maxprice", maxprice.to_string()));
}
if let Some(minprice) = self.minprice {
params.push(("minprice", minprice.to_string()));
}
if let Some(opennow) = self.opennow {
params.push(("opennow", opennow.to_string()));
}
if let Some(pagetoken) = self.pagetoken.clone() {
params.push(("pagetoken", pagetoken));
}
if let Some(region) = self.region.clone() {
params.push(("region", region));
}
if let Some(place_type) = self.place_type.clone() {
params.push(("type", place_type));
}
params
}
/// Execute the TextSearch call in a non-blocking fashion.
///
/// This will make a request to the Google Places API and retrieve the results. The results will be stored in the `result` field of the `TextSearch` struct.
///
/// If the query is successful, the method will return `Some(self)`. If the query fails, the method will return `None`.
///
/// # Panics
///
/// Panics if the query_text and type fields are both `None`.
///
/// # Errors
///
/// If the query fails, the method will return `None`. In this case, you should check the error message contained in the `result` field of the `TextSearch` struct.
///
/// # Examples
///
///
pub async fn execute(&mut self, max_pages: usize) -> Option<&mut TextSearch<'a>> {
match (self.text_query.clone(), self.place_type.clone()) {
(Some(_), _) | (_, Some(_)) => {
let url = "https://maps.googleapis.com/maps/api/place/textsearch/json";
let mut params = self.build_params();
let mut page_count = 0;
while page_count < max_pages {
let resp = self.client.get(url).query(¶ms).send().await.unwrap();
match resp.json::<TextSearchResult>().await {
Ok(query_result) => {
if page_count == 0 {
// First page, initialize result
self.result = query_result.clone();
} else {
// Append subsequent pages
self.result.places.extend(query_result.places);
}
if let Some(next_page_token) = query_result.next_page_token {
self.pagetoken = Some(next_page_token);
params = self.build_params();
page_count += 1;
if page_count != max_pages {
sleep(Duration::from_millis(2000)).await;
}
} else {
break; // No more pages to fetch
}
}
Err(err) => {
println!("Failed to parse API response: {:?}", err);
return None;
}
}
}
Some(self)
}
(None, None) => {
panic!("Provide either query_text or type or both for query!");
}
}
}
/// Execute the call in a blocking fashion.
///
/// This function will return `None` if the response from the API cannot be parsed.
///
/// # Arguments
///
/// * `max_pages` - The maximum number of pages of results to fetch.
///
/// # Examples
///
///
#[cfg(feature = "blocking")]
pub fn execute_blocking(&mut self, max_pages: usize) -> Option<&mut TextSearch<'a>> {
tokio::runtime::Runtime::new()
.unwrap()
.block_on(self.execute(max_pages))
}
/**
This function returns an iterator (TextSearchIter) over the places in a TextSearch object. The iterator is initialized to start at the first place (index 0).
It allows you to iterate over the places in the TextSearch result without having to manually keep track of the index.
For example, you can use it like this:
*/
pub fn iter(&mut self) -> TextSearchIter<'_, 'a> {
TextSearchIter {
text_search: self,
current_index: 0,
}
}
/**
Retrieve the place at the specified index.
index -> The index of the place to retrieve.
Returns an `Option` with the place if it exists, or `None` if the index is out of range.
*/
pub fn at(&self, index: usize) -> Option<&PlaceSearchPlace> {
self.result.places.get(index)
}
/**
Retrieve a cloned `TextSearchResult`.
This function returns a clone of the `TextSearchResult` associated with the `TextSearch` instance.
# Returns
A `TextSearchResult` object that contains the results of the text search operation.
*/
pub fn get_result(&'a self) -> TextSearchResult {
self.result.clone()
}
}
pub struct TextSearchIter<'a, 'b> {
text_search: &'b mut TextSearch<'a>,
current_index: usize,
}
impl<'a, 'b> Iterator for TextSearchIter<'a, 'b> {
type Item = &'b PlaceSearchPlace;
/// Advances the iterator and returns the next value.
///
/// Returns `None` when iteration is finished.
///
/// # Safety
///
/// This function is unsafe because it uses `std::mem::transmute` to cast a reference to `PlaceSearchPlace` to a reference to `&'b PlaceSearchPlace`.
/// This is safe because the `TextSearchIterator` is only ever created from a `&'b mut TextSearch<'a>`, so we know that the lifetime of the `TextSearch`
/// is at least as long as the lifetime of the `TextSearchIterator`.
fn next(&mut self) -> Option<Self::Item> {
if self.current_index < self.text_search.result.places.len() {
let place = &self.text_search.result.places[self.current_index];
self.current_index += 1;
Some(unsafe { std::mem::transmute(place) })
} else {
None
}
}
}