everymap-providers-here 0.2.2

HERE Technologies provider for EveryMap — geocoding, routing, traffic, isolines, and more
Documentation
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
pub mod types;

use crate::client::HereClient;
use async_trait::async_trait;
use everymap_core::domains::search::{
    GeocodeOptions, Geocoder, ReverseGeocodeOptions, SearchResponse, SearchResult,
};
use everymap_core::error::EveryMapResult;
use everymap_core::types::Coordinate;
use std::sync::Arc;

// Re-export all public types from the types module
pub use types::*;

const GEOCODE_BASE_URL: &str = "https://geocode.search.hereapi.com/v1";
const REVERSE_GEOCODE_BASE_URL: &str = "https://revgeocode.search.hereapi.com/v1";
const DISCOVER_BASE_URL: &str = "https://discover.search.hereapi.com/v1";
const AUTOSUGGEST_BASE_URL: &str = "https://autosuggest.search.hereapi.com/v1";

/// Exhaustive options for HERE Geocoding & Search API v7 geocode/reverseGeocode.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct HereGeocodeOptions {
    pub at: Option<Coordinate>,
    pub in_filter: Option<String>,
    pub qq: Option<String>,
    pub lang: Option<String>,
    pub limit: Option<u32>,
    pub radius: Option<f64>,
    pub political_view: Option<String>,
    pub address_names_mode: Option<AddressNamesMode>,
    pub address_names_variant: Option<String>,
    pub postal_code_mode: Option<PostalCodeMode>,
    pub types: Option<Vec<SearchType>>,
    pub with: Option<Vec<WithFeature>>,
    pub show: Option<Vec<ShowFeature>>,
    pub show_map_references: Option<Vec<ShowMapReference>>,
    pub show_nav_attributes: Option<Vec<ShowNavAttribute>>,
    pub show_related: Option<Vec<ShowRelated>>,
    pub show_translations: Option<Vec<ShowTranslation>>,
    pub request_id: Option<String>,
}

/// Request wrapper for the HERE-specific discover endpoint.
pub struct DiscoverRequest {
    pub query: String,
    pub options: HereDiscoverOptions,
}

/// Request wrapper for the HERE-specific autosuggest endpoint.
pub struct AutosuggestRequest {
    pub query: String,
    pub options: HereAutosuggestOptions,
}

/// Implementation of Geocoder for HERE Technologies.
///
/// In addition to the core `Geocoder` trait methods (geocode, reverse_geocode),
/// this struct provides HERE-specific methods: `discover` and `autosuggest`.
pub struct HereGeocoder {
    pub(crate) client: Arc<HereClient>,
    pub(crate) geocode_base_url: String,
    pub(crate) reverse_geocode_base_url: String,
    pub(crate) discover_base_url: String,
    pub(crate) autosuggest_base_url: String,
}

impl HereGeocoder {
    pub fn new(client: Arc<HereClient>) -> Self {
        Self {
            client,
            geocode_base_url: GEOCODE_BASE_URL.to_string(),
            reverse_geocode_base_url: REVERSE_GEOCODE_BASE_URL.to_string(),
            discover_base_url: DISCOVER_BASE_URL.to_string(),
            autosuggest_base_url: AUTOSUGGEST_BASE_URL.to_string(),
        }
    }

    pub fn with_base_url(client: Arc<HereClient>, base_url: String) -> Self {
        Self {
            client,
            geocode_base_url: base_url.clone(),
            reverse_geocode_base_url: base_url.clone(),
            discover_base_url: base_url.clone(),
            autosuggest_base_url: base_url,
        }
    }

    /// Discover places/POIs matching a query.
    pub async fn discover(&self, req: DiscoverRequest) -> EveryMapResult<HereDiscoverResponse> {
        let mut params: Vec<(&str, String)> = Vec::new();
        params.push(("q", req.query));
        apply_discover_options(&req.options, &mut params);

        let url = format!("{}/discover", self.discover_base_url);
        let builder = self
            .client
            .build_request(reqwest::Method::GET, &url)
            .query(&params);

        let here_res: HereDiscoverResponse = self.client.request_json(builder).await?;

        Ok(here_res)
    }

    /// Get autosuggest results for a partial query (type-ahead).
    pub async fn autosuggest(
        &self,
        req: AutosuggestRequest,
    ) -> EveryMapResult<HereAutosuggestResponse> {
        let mut params: Vec<(&str, String)> = Vec::new();
        params.push(("q", req.query));
        apply_autosuggest_options(&req.options, &mut params);

        let url = format!("{}/autosuggest", self.autosuggest_base_url);
        let builder = self
            .client
            .build_request(reqwest::Method::GET, &url)
            .query(&params);

        let here_res: HereAutosuggestResponse = self.client.request_json(builder).await?;

        Ok(here_res)
    }
}

/// Convert core `GeocodeOptions` to HERE-specific `HereGeocodeOptions`,
/// extracting common fields and parsing `provider_extra` for HERE-specific ones.
fn geocode_options_from_core(options: &GeocodeOptions) -> HereGeocodeOptions {
    let mut here_options = HereGeocodeOptions {
        limit: options.limit,
        lang: options.language.clone(),
        ..Default::default()
    };

    // Convert bounding_box to HERE's `at` + `in_filter` convention if present
    if let Some(bb) = &options.bounding_box {
        // Use center of bounding box as `at`
        if let Ok(center) = Coordinate::new(
            (bb.north_east.lat + bb.south_west.lat) / 2.0,
            (bb.north_east.lng + bb.south_west.lng) / 2.0,
        ) {
            here_options.at = Some(center);
        }
        // Use bounding box as `in` filter
        here_options.in_filter = Some(format!(
            "bbox:{},{},{},{}",
            bb.south_west.lng, bb.south_west.lat, bb.north_east.lng, bb.north_east.lat
        ));
    }

    // Convert country_codes to HERE's `in_filter` if not already set
    if !options.country_codes.is_empty() {
        let countries = options.country_codes.join(",");
        if let Some(existing) = &mut here_options.in_filter {
            // Append country filter to existing in_filter
            *existing = format!("{}+countryCode:{}", existing, countries);
        } else {
            here_options.in_filter = Some(format!("countryCode:{}", countries));
        }
    }

    // Extract HERE-specific options from provider_extra
    if let Some(extra) = &options.provider_extra {
        if let Some(obj) = extra.as_object() {
            extract_here_geocode_extra(obj, &mut here_options);
        }
    }

    here_options
}

/// Convert core `ReverseGeocodeOptions` to HERE-specific `HereGeocodeOptions`,
/// extracting common fields and parsing `provider_extra` for HERE-specific ones.
fn reverse_geocode_options_from_core(options: &ReverseGeocodeOptions) -> HereGeocodeOptions {
    let mut here_options = HereGeocodeOptions {
        limit: options.limit,
        lang: options.language.clone(),
        radius: options.radius,
        ..Default::default()
    };

    // Extract HERE-specific options from provider_extra
    if let Some(extra) = &options.provider_extra {
        if let Some(obj) = extra.as_object() {
            extract_here_geocode_extra(obj, &mut here_options);
        }
    }

    here_options
}

/// Extract HERE-specific fields from a JSON object into `HereGeocodeOptions`.
fn extract_here_geocode_extra(
    obj: &serde_json::Map<String, serde_json::Value>,
    here_options: &mut HereGeocodeOptions,
) {
    if let Some(v) = obj.get("at") {
        if let Ok(coordinate) = serde_json::from_value::<Coordinate>(v.clone()) {
            here_options.at = Some(coordinate);
        }
    }
    if let Some(v) = obj.get("in_filter").and_then(|v| v.as_str()) {
        here_options.in_filter = Some(v.to_string());
    }
    if let Some(v) = obj.get("qq").and_then(|v| v.as_str()) {
        here_options.qq = Some(v.to_string());
    }
    if let Some(v) = obj.get("lang").and_then(|v| v.as_str()) {
        here_options.lang = Some(v.to_string());
    }
    if let Some(v) = obj.get("limit").and_then(|v| v.as_u64()) {
        here_options.limit = Some(v as u32);
    }
    if let Some(v) = obj.get("political_view").and_then(|v| v.as_str()) {
        here_options.political_view = Some(v.to_string());
    }
    if let Some(v) = obj.get("address_names_variant").and_then(|v| v.as_str()) {
        here_options.address_names_variant = Some(v.to_string());
    }
    if let Some(v) = obj.get("request_id").and_then(|v| v.as_str()) {
        here_options.request_id = Some(v.to_string());
    }
    if let Some(v) = obj.get("address_names_mode") {
        if let Ok(parsed) = serde_json::from_value::<AddressNamesMode>(v.clone()) {
            here_options.address_names_mode = Some(parsed);
        }
    }
    if let Some(v) = obj.get("postal_code_mode") {
        if let Ok(parsed) = serde_json::from_value::<PostalCodeMode>(v.clone()) {
            here_options.postal_code_mode = Some(parsed);
        }
    }
    if let Some(v) = obj.get("types").and_then(|v| v.as_array()) {
        let parsed: Vec<SearchType> = v
            .iter()
            .filter_map(|item| serde_json::from_value(item.clone()).ok())
            .collect();
        if !parsed.is_empty() {
            here_options.types = Some(parsed);
        }
    }
    if let Some(v) = obj.get("with").and_then(|v| v.as_array()) {
        let parsed: Vec<WithFeature> = v
            .iter()
            .filter_map(|item| serde_json::from_value(item.clone()).ok())
            .collect();
        if !parsed.is_empty() {
            here_options.with = Some(parsed);
        }
    }
    if let Some(v) = obj.get("show").and_then(|v| v.as_array()) {
        let parsed: Vec<ShowFeature> = v
            .iter()
            .filter_map(|item| serde_json::from_value(item.clone()).ok())
            .collect();
        if !parsed.is_empty() {
            here_options.show = Some(parsed);
        }
    }
    if let Some(v) = obj.get("show_map_references").and_then(|v| v.as_array()) {
        let parsed: Vec<ShowMapReference> = v
            .iter()
            .filter_map(|item| serde_json::from_value(item.clone()).ok())
            .collect();
        if !parsed.is_empty() {
            here_options.show_map_references = Some(parsed);
        }
    }
    if let Some(v) = obj.get("show_nav_attributes").and_then(|v| v.as_array()) {
        let parsed: Vec<ShowNavAttribute> = v
            .iter()
            .filter_map(|item| serde_json::from_value(item.clone()).ok())
            .collect();
        if !parsed.is_empty() {
            here_options.show_nav_attributes = Some(parsed);
        }
    }
    if let Some(v) = obj.get("show_related").and_then(|v| v.as_array()) {
        let parsed: Vec<ShowRelated> = v
            .iter()
            .filter_map(|item| serde_json::from_value(item.clone()).ok())
            .collect();
        if !parsed.is_empty() {
            here_options.show_related = Some(parsed);
        }
    }
    if let Some(v) = obj.get("show_translations").and_then(|v| v.as_array()) {
        let parsed: Vec<ShowTranslation> = v
            .iter()
            .filter_map(|item| serde_json::from_value(item.clone()).ok())
            .collect();
        if !parsed.is_empty() {
            here_options.show_translations = Some(parsed);
        }
    }
}

/// Helper to apply geocode options to query params.
fn apply_geocode_options(options: &HereGeocodeOptions, params: &mut Vec<(&str, String)>) {
    if let Some(at) = &options.at {
        params.push(("at", format!("{},{}", at.lat, at.lng)));
    }
    if let Some(in_filter) = &options.in_filter {
        params.push(("in", in_filter.clone()));
    }
    if let Some(qq) = &options.qq {
        params.push(("qq", qq.clone()));
    }
    if let Some(lang) = &options.lang {
        params.push(("lang", lang.clone()));
    }
    if let Some(limit) = options.limit {
        params.push(("limit", limit.to_string()));
    }
    if let Some(radius) = options.radius {
        params.push(("radius", radius.to_string()));
    }
    if let Some(political_view) = &options.political_view {
        params.push(("politicalView", political_view.clone()));
    }
    if let Some(address_names_mode) = &options.address_names_mode {
        let value = crate::util::enum_as_str(address_names_mode);
        params.push(("addressNamesMode", value));
    }
    if let Some(address_names_variant) = &options.address_names_variant {
        params.push(("addressNamesVariant", address_names_variant.clone()));
    }
    if let Some(postal_code_mode) = &options.postal_code_mode {
        let value = crate::util::enum_as_str(postal_code_mode);
        params.push(("postalCodeMode", value));
    }
    if let Some(types) = &options.types {
        let value = types
            .iter()
            .map(crate::util::enum_as_str)
            .collect::<Vec<_>>()
            .join(",");
        params.push(("types", value));
    }
    if let Some(with) = &options.with {
        let value = with
            .iter()
            .map(crate::util::enum_as_str)
            .collect::<Vec<_>>()
            .join(",");
        params.push(("with", value));
    }
    if let Some(show) = &options.show {
        let value = show
            .iter()
            .map(crate::util::enum_as_str)
            .collect::<Vec<_>>()
            .join(",");
        params.push(("show", value));
    }
    if let Some(show_map_references) = &options.show_map_references {
        let value = show_map_references
            .iter()
            .map(crate::util::enum_as_str)
            .collect::<Vec<_>>()
            .join(",");
        params.push(("showMapReferences", value));
    }
    if let Some(show_nav_attributes) = &options.show_nav_attributes {
        let value = show_nav_attributes
            .iter()
            .map(crate::util::enum_as_str)
            .collect::<Vec<_>>()
            .join(",");
        params.push(("showNavAttributes", value));
    }
    if let Some(show_related) = &options.show_related {
        let value = show_related
            .iter()
            .map(crate::util::enum_as_str)
            .collect::<Vec<_>>()
            .join(",");
        params.push(("showRelated", value));
    }
    if let Some(show_translations) = &options.show_translations {
        let value = show_translations
            .iter()
            .map(crate::util::enum_as_str)
            .collect::<Vec<_>>()
            .join(",");
        params.push(("showTranslations", value));
    }
}

/// Helper to apply discover options to query params.
fn apply_discover_options(options: &HereDiscoverOptions, params: &mut Vec<(&str, String)>) {
    if let Some(at) = &options.at {
        params.push(("at", format!("{},{}", at.lat, at.lng)));
    }
    if let Some(in_filter) = &options.in_filter {
        params.push(("in", in_filter.clone()));
    }
    if let Some(lang) = &options.lang {
        params.push(("lang", lang.clone()));
    }
    if let Some(limit) = options.limit {
        params.push(("limit", limit.to_string()));
    }
    if let Some(political_view) = &options.political_view {
        params.push(("politicalView", political_view.clone()));
    }
    if let Some(types) = &options.types {
        let value = types
            .iter()
            .map(crate::util::enum_as_str)
            .collect::<Vec<_>>()
            .join(",");
        params.push(("types", value));
    }
    if let Some(with) = &options.with {
        let value = with
            .iter()
            .map(crate::util::enum_as_str)
            .collect::<Vec<_>>()
            .join(",");
        params.push(("with", value));
    }
    if let Some(show) = &options.show {
        let value = show
            .iter()
            .map(crate::util::enum_as_str)
            .collect::<Vec<_>>()
            .join(",");
        params.push(("show", value));
    }
    if let Some(mobility) = &options.mobility_mode {
        let value = crate::util::enum_as_str(mobility);
        params.push(("mobilityMode", value));
    }
    if let Some(ranking) = &options.ranking {
        let value = crate::util::enum_as_str(ranking);
        params.push(("ranking", value));
    }
    if let Some(offset) = options.offset {
        params.push(("offset", offset.to_string()));
    }
}

/// Helper to apply autosuggest options to query params.
fn apply_autosuggest_options(options: &HereAutosuggestOptions, params: &mut Vec<(&str, String)>) {
    if let Some(at) = &options.at {
        params.push(("at", format!("{},{}", at.lat, at.lng)));
    }
    if let Some(in_filter) = &options.in_filter {
        params.push(("in", in_filter.clone()));
    }
    if let Some(lang) = &options.lang {
        params.push(("lang", lang.clone()));
    }
    if let Some(limit) = options.limit {
        params.push(("limit", limit.to_string()));
    }
    if let Some(political_view) = &options.political_view {
        params.push(("politicalView", political_view.clone()));
    }
    if let Some(types) = &options.types {
        let value = types
            .iter()
            .map(crate::util::enum_as_str)
            .collect::<Vec<_>>()
            .join(",");
        params.push(("types", value));
    }
    if let Some(with) = &options.with {
        let value = with
            .iter()
            .map(crate::util::enum_as_str)
            .collect::<Vec<_>>()
            .join(",");
        params.push(("with", value));
    }
    if let Some(show) = &options.show {
        let value = show
            .iter()
            .map(crate::util::enum_as_str)
            .collect::<Vec<_>>()
            .join(",");
        params.push(("show", value));
    }
    if let Some(show_map_references) = &options.show_map_references {
        let value = show_map_references
            .iter()
            .map(crate::util::enum_as_str)
            .collect::<Vec<_>>()
            .join(",");
        params.push(("showMapReferences", value));
    }
    if let Some(mobility) = &options.mobility_mode {
        let value = crate::util::enum_as_str(mobility);
        params.push(("mobilityMode", value));
    }
    if let Some(ranking) = &options.ranking {
        let value = crate::util::enum_as_str(ranking);
        params.push(("ranking", value));
    }
    if let Some(terms_limit) = options.terms_limit {
        params.push(("termsLimit", terms_limit.to_string()));
    }
    if let Some(offset) = options.offset {
        params.push(("offset", offset.to_string()));
    }
}

#[async_trait]
impl Geocoder for HereGeocoder {
    async fn geocode(
        &self,
        query: &str,
        options: &GeocodeOptions,
    ) -> EveryMapResult<SearchResponse> {
        let mut params: Vec<(&str, String)> = Vec::new();

        if !query.is_empty() {
            params.push(("q", query.to_string()));
        }

        let here_options = geocode_options_from_core(options);
        apply_geocode_options(&here_options, &mut params);

        let url = format!("{}/geocode", self.geocode_base_url);
        let mut builder = self
            .client
            .build_request(reqwest::Method::GET, &url)
            .query(&params);

        if let Some(rid) = &here_options.request_id {
            builder = builder.header("X-Request-ID", rid.as_str());
        }

        let here_res: HereSearchResponse = self.client.request_json(builder).await?;

        let items = here_res.items.into_iter().map(SearchResult::from).collect();

        Ok(SearchResponse { items })
    }

    async fn reverse_geocode(
        &self,
        coordinate: &Coordinate,
        options: &ReverseGeocodeOptions,
    ) -> EveryMapResult<SearchResponse> {
        let mut params: Vec<(&str, String)> =
            vec![("at", format!("{},{}", coordinate.lat, coordinate.lng))];

        let here_options = reverse_geocode_options_from_core(options);
        apply_geocode_options(&here_options, &mut params);

        let url = format!("{}/revgeocode", self.reverse_geocode_base_url);
        let mut builder = self
            .client
            .build_request(reqwest::Method::GET, &url)
            .query(&params);

        if let Some(rid) = &here_options.request_id {
            builder = builder.header("X-Request-ID", rid.as_str());
        }

        let here_res: HereSearchResponse = self.client.request_json(builder).await?;

        let items = here_res.items.into_iter().map(SearchResult::from).collect();

        Ok(SearchResponse { items })
    }
}