Skip to content

Commit 6a1937a

Browse files
authored
Try #48:
2 parents 1736b26 + bce0d1d commit 6a1937a

File tree

5 files changed

+66
-56
lines changed

5 files changed

+66
-56
lines changed

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ edition = "2018"
1111

1212
[dependencies]
1313
thiserror = "1.0"
14-
geo-types = "0.6"
14+
geo-types = "0.7"
1515
num-traits = "0.2"
1616
serde = { version = "1.0", features = ["derive"] }
1717
serde_json = "1.0"
18-
reqwest = { version = "0.11", default-features = false, features = ["default-tls", "blocking", "json"] }
19-
hyper = "0.14.11"
18+
reqwest = { version = "0.11", default-features = false, features = [
19+
"default-tls",
20+
"blocking",
21+
"json",
22+
] }
23+
hyper = "0.14"
2024
chrono = { version = "0.4", features = ["serde"] }
2125

2226
[features]

src/geoadmin.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
//! let res = geoadmin.forward(&address);
1616
//! assert_eq!(res.unwrap(), vec![Point::new(7.451352119445801, 46.92793655395508)]);
1717
//! ```
18+
use std::fmt::Debug;
19+
1820
use crate::Deserialize;
1921
use crate::GeocodingError;
2022
use crate::InputBounds;
2123
use crate::Point;
2224
use crate::UA_STRING;
2325
use crate::{Client, HeaderMap, HeaderValue, USER_AGENT};
2426
use crate::{Forward, Reverse};
25-
use num_traits::{Float, Pow};
27+
use geo_types::CoordFloat;
28+
use num_traits::Pow;
2629

2730
/// An instance of the GeoAdmin geocoding service
2831
pub struct GeoAdmin {
@@ -34,7 +37,7 @@ pub struct GeoAdmin {
3437
/// An instance of a parameter builder for GeoAdmin geocoding
3538
pub struct GeoAdminParams<'a, T>
3639
where
37-
T: Float,
40+
T: CoordFloat,
3841
{
3942
searchtext: &'a str,
4043
origins: &'a str,
@@ -44,7 +47,7 @@ where
4447

4548
impl<'a, T> GeoAdminParams<'a, T>
4649
where
47-
T: Float,
50+
T: CoordFloat,
4851
{
4952
/// Create a new GeoAdmin parameter builder
5053
/// # Example:
@@ -159,7 +162,7 @@ impl GeoAdmin {
159162
params: &GeoAdminParams<T>,
160163
) -> Result<GeoAdminForwardResponse<T>, GeocodingError>
161164
where
162-
T: Float,
165+
T: CoordFloat,
163166
for<'de> T: Deserialize<'de>,
164167
{
165168
// For lifetime issues
@@ -219,7 +222,7 @@ impl Default for GeoAdmin {
219222

220223
impl<T> Forward<T> for GeoAdmin
221224
where
222-
T: Float,
225+
T: CoordFloat,
223226
for<'de> T: Deserialize<'de>,
224227
{
225228
/// A forward-geocoding lookup of an address. Please see [the documentation](https://api3.geo.admin.ch/services/sdiservices.html#search) for details.
@@ -258,7 +261,7 @@ where
258261

259262
impl<T> Reverse<T> for GeoAdmin
260263
where
261-
T: Float,
264+
T: CoordFloat,
262265
for<'de> T: Deserialize<'de>,
263266
{
264267
/// A reverse lookup of a point. More detail on the format of the
@@ -309,7 +312,7 @@ where
309312
// See [the documentation](https://www.swisstopo.admin.ch/content/swisstopo-internet/en/online/calculation-services/_jcr_content/contentPar/tabs/items/documents_publicatio/tabPar/downloadlist/downloadItems/19_1467104393233.download/ch1903wgs84_e.pdf) for more details
310313
fn wgs84_to_lv03<T>(p: &Point<T>) -> Point<T>
311314
where
312-
T: Float,
315+
T: CoordFloat,
313316
{
314317
let lambda = (p.x().to_f64().unwrap() * 3600.0 - 26782.5) / 10000.0;
315318
let phi = (p.y().to_f64().unwrap() * 3600.0 - 169028.66) / 10000.0;
@@ -356,7 +359,7 @@ where
356359
#[derive(Debug, Deserialize)]
357360
pub struct GeoAdminForwardResponse<T>
358361
where
359-
T: Float,
362+
T: CoordFloat,
360363
{
361364
pub features: Vec<GeoAdminForwardLocation<T>>,
362365
}
@@ -365,7 +368,7 @@ where
365368
#[derive(Debug, Deserialize)]
366369
pub struct GeoAdminForwardLocation<T>
367370
where
368-
T: Float,
371+
T: CoordFloat,
369372
{
370373
id: Option<usize>,
371374
pub properties: ForwardLocationProperties<T>,
@@ -454,7 +457,7 @@ mod test {
454457
fn new_with_sr_forward_test() {
455458
let geoadmin = GeoAdmin::new().with_sr("2056");
456459
let address = "Seftigenstrasse 264, 3084 Wabern";
457-
let res = geoadmin.forward(&address);
460+
let res = geoadmin.forward(address);
458461
assert_eq!(res.unwrap(), vec![Point::new(2_600_968.75, 1_197_427.0)]);
459462
}
460463

@@ -463,7 +466,7 @@ mod test {
463466
let geoadmin =
464467
GeoAdmin::new().with_endpoint("https://api3.geo.admin.ch/rest/services/api/");
465468
let address = "Seftigenstrasse 264, 3084 Wabern";
466-
let res = geoadmin.forward(&address);
469+
let res = geoadmin.forward(address);
467470
assert_eq!(
468471
res.unwrap(),
469472
vec![Point::new(7.451352119445801, 46.92793655395508)]
@@ -474,7 +477,7 @@ mod test {
474477
fn with_sr_forward_full_test() {
475478
let geoadmin = GeoAdmin::new().with_sr("2056");
476479
let bbox = InputBounds::new((2_600_967.75, 1_197_426.0), (2_600_969.75, 1_197_428.0));
477-
let params = GeoAdminParams::new(&"Seftigenstrasse Bern")
480+
let params = GeoAdminParams::new("Seftigenstrasse Bern")
478481
.with_origins("address")
479482
.with_bbox(&bbox)
480483
.build();
@@ -490,7 +493,7 @@ mod test {
490493
fn forward_full_test() {
491494
let geoadmin = GeoAdmin::new();
492495
let bbox = InputBounds::new((7.4513398, 46.92792859), (7.4513662, 46.9279467));
493-
let params = GeoAdminParams::new(&"Seftigenstrasse Bern")
496+
let params = GeoAdminParams::new("Seftigenstrasse Bern")
494497
.with_origins("address")
495498
.with_bbox(&bbox)
496499
.build();
@@ -506,7 +509,7 @@ mod test {
506509
fn forward_test() {
507510
let geoadmin = GeoAdmin::new();
508511
let address = "Seftigenstrasse 264, 3084 Wabern";
509-
let res = geoadmin.forward(&address);
512+
let res = geoadmin.forward(address);
510513
assert_eq!(
511514
res.unwrap(),
512515
vec![Point::new(7.451352119445801, 46.92793655395508)]

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
2828
static UA_STRING: &str = "Rust-Geocoding";
2929

30-
use chrono;
30+
use geo_types::CoordFloat;
3131
pub use geo_types::{Coordinate, Point};
32-
use num_traits::Float;
3332
use reqwest::blocking::Client;
3433
use reqwest::header::ToStrError;
3534
use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT};
3635
use serde::de::DeserializeOwned;
3736
use serde::{Deserialize, Serialize};
37+
use std::fmt::Debug;
3838
use std::num::ParseIntError;
3939
use thiserror::Error;
4040

@@ -85,7 +85,7 @@ pub enum GeocodingError {
8585
/// ```
8686
pub trait Reverse<T>
8787
where
88-
T: Float,
88+
T: CoordFloat,
8989
{
9090
// NOTE TO IMPLEMENTERS: Point coordinates are lon, lat (x, y)
9191
// You may have to provide these coordinates in reverse order,
@@ -113,7 +113,7 @@ where
113113
/// ```
114114
pub trait Forward<T>
115115
where
116-
T: Float,
116+
T: CoordFloat,
117117
{
118118
// NOTE TO IMPLEMENTERS: while returned provider point data may not be in
119119
// lon, lat (x, y) order, Geocoding requires this order in its output Point
@@ -128,15 +128,15 @@ where
128128
#[derive(Copy, Clone, Debug)]
129129
pub struct InputBounds<T>
130130
where
131-
T: Float,
131+
T: CoordFloat,
132132
{
133133
pub minimum_lonlat: Point<T>,
134134
pub maximum_lonlat: Point<T>,
135135
}
136136

137137
impl<T> InputBounds<T>
138138
where
139-
T: Float,
139+
T: CoordFloat,
140140
{
141141
/// Create a new `InputBounds` struct by passing 2 `Point`s defining:
142142
/// - minimum (bottom-left) longitude and latitude coordinates
@@ -155,7 +155,7 @@ where
155155
/// Convert borrowed input bounds into the correct String representation
156156
impl<T> From<InputBounds<T>> for String
157157
where
158-
T: Float,
158+
T: CoordFloat,
159159
{
160160
fn from(ip: InputBounds<T>) -> String {
161161
// Return in lon, lat order

src/opencage.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
//! // "Carrer de Calatrava, 68, 08017 Barcelone, Espagne"
2525
//! println!("{:?}", res.unwrap());
2626
//! ```
27-
use crate::chrono::naive::serde::ts_seconds::deserialize as from_ts;
28-
use crate::chrono::NaiveDateTime;
2927
use crate::DeserializeOwned;
3028
use crate::GeocodingError;
3129
use crate::InputBounds;
@@ -34,9 +32,12 @@ use crate::UA_STRING;
3432
use crate::{Client, HeaderMap, HeaderValue, USER_AGENT};
3533
use crate::{Deserialize, Serialize};
3634
use crate::{Forward, Reverse};
37-
use num_traits::Float;
35+
use chrono::naive::serde::ts_seconds::deserialize as from_ts;
36+
use chrono::NaiveDateTime;
37+
use geo_types::CoordFloat;
3838
use serde::Deserializer;
3939
use std::collections::HashMap;
40+
use std::fmt::Debug;
4041
use std::sync::{Arc, Mutex};
4142

4243
macro_rules! add_optional_param {
@@ -146,7 +147,7 @@ impl<'a> Opencage<'a> {
146147
///```
147148
pub fn reverse_full<T>(&self, point: &Point<T>) -> Result<OpencageResponse<T>, GeocodingError>
148149
where
149-
T: Float + DeserializeOwned,
150+
T: CoordFloat + DeserializeOwned,
150151
{
151152
let q = format!(
152153
"{}, {}",
@@ -156,9 +157,9 @@ impl<'a> Opencage<'a> {
156157
);
157158
let mut query = vec![
158159
("q", q.as_str()),
159-
(&"key", &self.api_key),
160-
(&"no_annotations", "0"),
161-
(&"no_record", "1"),
160+
("key", &self.api_key),
161+
("no_annotations", "0"),
162+
("no_record", "1"),
162163
];
163164
query.extend(self.parameters.as_query());
164165

@@ -248,7 +249,7 @@ impl<'a> Opencage<'a> {
248249
bounds: U,
249250
) -> Result<OpencageResponse<T>, GeocodingError>
250251
where
251-
T: Float + DeserializeOwned,
252+
T: CoordFloat + DeserializeOwned,
252253
U: Into<Option<InputBounds<T>>>,
253254
{
254255
let ann = String::from("0");
@@ -291,7 +292,7 @@ impl<'a> Opencage<'a> {
291292

292293
impl<'a, T> Reverse<T> for Opencage<'a>
293294
where
294-
T: Float + DeserializeOwned,
295+
T: CoordFloat + DeserializeOwned,
295296
{
296297
/// A reverse lookup of a point. More detail on the format of the
297298
/// returned `String` can be found [here](https://blog.opencagedata.com/post/99059889253/good-looking-addresses-solving-the-berlin-berlin)
@@ -336,7 +337,7 @@ where
336337

337338
impl<'a, T> Forward<T> for Opencage<'a>
338339
where
339-
T: Float + DeserializeOwned,
340+
T: CoordFloat + DeserializeOwned,
340341
{
341342
/// A forward-geocoding lookup of an address. Please see [the documentation](https://opencagedata.com/api#ambiguous-results) for details
342343
/// of best practices in order to obtain good-quality results.
@@ -511,7 +512,7 @@ where
511512
#[derive(Debug, Serialize, Deserialize)]
512513
pub struct OpencageResponse<T>
513514
where
514-
T: Float,
515+
T: CoordFloat,
515516
{
516517
pub documentation: String,
517518
pub licenses: Vec<HashMap<String, String>>,
@@ -528,7 +529,7 @@ where
528529
#[derive(Debug, Clone, Serialize, Deserialize)]
529530
pub struct Results<T>
530531
where
531-
T: Float,
532+
T: CoordFloat,
532533
{
533534
pub annotations: Option<Annotations<T>>,
534535
pub bounds: Option<Bounds<T>>,
@@ -542,7 +543,7 @@ where
542543
#[derive(Debug, Clone, Serialize, Deserialize)]
543544
pub struct Annotations<T>
544545
where
545-
T: Float,
546+
T: CoordFloat,
546547
{
547548
pub dms: Option<HashMap<String, String>>,
548549
pub mgrs: Option<String>,
@@ -615,7 +616,7 @@ pub struct Timestamp {
615616
#[derive(Debug, Clone, Serialize, Deserialize)]
616617
pub struct Bounds<T>
617618
where
618-
T: Float,
619+
T: CoordFloat,
619620
{
620621
pub northeast: HashMap<String, T>,
621622
pub southwest: HashMap<String, T>,
@@ -652,7 +653,7 @@ mod test {
652653
fn forward_test() {
653654
let oc = Opencage::new("dcdbf0d783374909b3debee728c7cc10".to_string());
654655
let address = "Schwabing, München";
655-
let res = oc.forward(&address);
656+
let res = oc.forward(address);
656657
assert_eq!(
657658
res.unwrap(),
658659
vec![Point(Coordinate {
@@ -678,7 +679,7 @@ mod test {
678679
minimum_lonlat: Point::new(-0.13806939125061035, 51.51989264641164),
679680
maximum_lonlat: Point::new(-0.13427138328552246, 51.52319711775629),
680681
};
681-
let res = oc.forward_full(&address, bbox).unwrap();
682+
let res = oc.forward_full(address, bbox).unwrap();
682683
let first_result = &res.results[0];
683684
assert!(first_result.formatted.contains("UCL"));
684685
}
@@ -690,7 +691,7 @@ mod test {
690691
Point::new(-0.13806939125061035, 51.51989264641164),
691692
Point::new(-0.13427138328552246, 51.52319711775629),
692693
);
693-
let res = oc.forward_full(&address, bbox).unwrap();
694+
let res = oc.forward_full(address, bbox).unwrap();
694695
let first_result = &res.results[0];
695696
assert!(first_result
696697
.formatted
@@ -704,7 +705,7 @@ mod test {
704705
Point::from((-0.13806939125061035, 51.51989264641164)),
705706
Point::from((-0.13427138328552246, 51.52319711775629)),
706707
);
707-
let res = oc.forward_full(&address, bbox).unwrap();
708+
let res = oc.forward_full(address, bbox).unwrap();
708709
let first_result = &res.results[0];
709710
assert!(first_result
710711
.formatted
@@ -718,7 +719,7 @@ mod test {
718719
(-0.13806939125061035, 51.51989264641164),
719720
(-0.13427138328552246, 51.52319711775629),
720721
);
721-
let res = oc.forward_full(&address, bbox).unwrap();
722+
let res = oc.forward_full(address, bbox).unwrap();
722723
let first_result = &res.results[0];
723724
assert!(first_result
724725
.formatted
@@ -728,7 +729,7 @@ mod test {
728729
fn forward_full_test_nobox() {
729730
let oc = Opencage::new("dcdbf0d783374909b3debee728c7cc10".to_string());
730731
let address = "Moabit, Berlin, Germany";
731-
let res = oc.forward_full(&address, NOBOX).unwrap();
732+
let res = oc.forward_full(address, NOBOX).unwrap();
732733
let first_result = &res.results[0];
733734
assert_eq!(first_result.formatted, "Moabit, Berlin, Germany");
734735
}

0 commit comments

Comments
 (0)