Skip to content

Commit 3d3be4a

Browse files
committed
api::ApiUrl - derive Hash & Eq & re-export ApiUrl in util
1 parent cc5b1c4 commit 3d3be4a

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

primitives/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub mod supermarket;
2020
pub mod targeting;
2121

2222
pub mod util {
23+
pub use api::ApiUrl;
24+
2325
pub mod api;
2426
pub mod tests {
2527
use slog::{o, Discard, Drain, Logger};

primitives/src/util/api.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{convert::TryFrom, fmt, str::FromStr};
33
use parse_display::Display;
44
use serde::{Deserialize, Serialize};
55
use thiserror::Error;
6-
use url::Url as Url;
6+
use url::Url;
77

88
// `url::Url::scheme()` returns lower-cased ASCII string without `:`
99
const SCHEMES: [&str; 2] = ["http", "https"];
@@ -35,11 +35,10 @@ pub enum Error {
3535
/// - `Query`, e.g. `?query_param=value`, `?query_param`, `?query=value&....`, etc.
3636
///
3737
/// [`url::Url`]: url::Url
38-
#[derive(Clone, Display, PartialEq, Deserialize, Serialize)]
38+
#[derive(Clone, Hash, Display, Ord, PartialOrd, Eq, PartialEq, Deserialize, Serialize)]
3939
#[serde(try_from = "Url", into = "Url")]
4040
pub struct ApiUrl(Url);
4141

42-
4342
impl ApiUrl {
4443
pub fn parse(input: &str) -> Result<Self, Error> {
4544
Self::from_str(input)
@@ -73,15 +72,15 @@ impl TryFrom<Url> for ApiUrl {
7372
return Err(Error::ShouldBeABase);
7473
}
7574

76-
if let Some(_) = url.fragment() {
75+
if url.fragment().is_some() {
7776
return Err(Error::HasFragment);
7877
}
7978

8079
if !SCHEMES.contains(&url.scheme()) {
8180
return Err(Error::InvalidScheme(url.scheme().to_string()));
8281
}
8382

84-
if let Some(_) = url.query() {
83+
if url.query().is_some() {
8584
return Err(Error::HasQuery);
8685
}
8786

@@ -170,7 +169,8 @@ mod test {
170169
// HTTPS, authority, domain, port and path
171170
(
172171
"https://username:password@jerry.adex.network:3335/leader",
173-
ApiUrl::from_str("https://username:password@jerry.adex.network:3335/leader").unwrap(),
172+
ApiUrl::from_str("https://username:password@jerry.adex.network:3335/leader")
173+
.unwrap(),
174174
),
175175
];
176176

@@ -208,20 +208,27 @@ mod test {
208208
}
209209
}
210210

211-
212211
#[test]
213212
fn api_endpoint() {
214213
let api_url = ApiUrl::parse("http://127.0.0.1/leader").expect("It is a valid API URL");
215214

216-
let expected = url::Url::parse("http://127.0.0.1/leader/endpoint?query=query value").expect("it is a valid Url");
215+
let expected = url::Url::parse("http://127.0.0.1/leader/endpoint?query=query value")
216+
.expect("it is a valid Url");
217217
let expected_url_encoded = "http://127.0.0.1/leader/endpoint?query=query%20value";
218218

219-
let actual = api_url.join("endpoint?query=query value").expect("Should join endpoint");
220-
let actual_should_strip_suffix = api_url.join("/endpoint?query=query value").expect("Should join endpoint and strip `/` suffix and preserve the original ApiUrl");
219+
let actual = api_url
220+
.join("endpoint?query=query value")
221+
.expect("Should join endpoint");
222+
let actual_should_strip_suffix = api_url
223+
.join("/endpoint?query=query value")
224+
.expect("Should join endpoint and strip `/` suffix and preserve the original ApiUrl");
221225
assert_eq!(&expected, &actual);
222226
assert_eq!(&expected, &actual_should_strip_suffix);
223-
227+
224228
assert_eq!(expected_url_encoded, &actual.to_string());
225-
assert_eq!(expected_url_encoded, &actual_should_strip_suffix.to_string());
229+
assert_eq!(
230+
expected_url_encoded,
231+
&actual_should_strip_suffix.to_string()
232+
);
226233
}
227234
}

0 commit comments

Comments
 (0)