Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement feature to use url::Url rather than http::Uri #10

Merged
merged 5 commits into from
Jan 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ license = "MIT"
http = "0.2.1"
regex = "1"
lazy_static = "1.4.0"
url = {version = "2.2.2", optional = true }
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ Refer to <https://tools.ietf.org/html/rfc8288#section-3.3> (October 2017), **the

Therefore, if you find that key is `None`, please check if you provide the `rel` type.

# Feature: `url`

If you enable the `url` feature, the `uri` field of struct [`Link`](struct.Link.html) will be
of type url::Url from the [url crate](https://crates.io/crates/url), rather than the
`http::Uri` it normally is. This allows direct use of the `uri` field with other popular
crates that use `url`, such as [`reqwest`](https://crates.io/crates/reqwest).

**NOTE:** This implictly disabled support for relative refs, as URLs do not support relative
refs (whereas URIs do).


## How to contribute

Pull a request or open an issue to describe your changes or problems.
Expand Down
54 changes: 36 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,25 @@
//! Refer to <https://tools.ietf.org/html/rfc8288#section-3.3> (October 2017), **the rel parameter MUST be present**.
//!
//! Therefore, if you find that key is `None`, please check if you provide the `rel` type.
//!
//! # Feature: `url`
//!
//! If you enable the `url` feature, the `uri` field of struct [`Link`](struct.Link.html) will be
//! of type url::Url from the [url crate](https://crates.io/crates/url), rather than the
//! `http::Uri` it normally is. This allows direct use of the `uri` field with other popular
//! crates that use `url`, such as [`reqwest`](https://crates.io/crates/reqwest).
//!
//! **NOTE:** This implictly disabled support for relative refs, as URLs do not support relative
//! refs (whereas URIs do).

use std::collections::HashMap;

#[cfg(not(feature = "url"))]
use http::Uri;

#[cfg(feature = "url")]
use url::Url as Uri;

use std::fmt;

/// A `Result` alias where the `Err` case is [`parse_link_header::Error`].
Expand Down Expand Up @@ -264,24 +279,27 @@ mod tests {

assert_eq!(expected, parsed);

let mut rel_link_expected = HashMap::new();

rel_link_expected.insert(
Some("foo/bar".to_string()),
Link {
uri: "/foo/bar".parse().unwrap(),
raw_uri: "/foo/bar".to_string(),
queries: HashMap::new(),
params: [("rel".to_string(), "foo/bar".to_string())]
.iter()
.cloned()
.collect(),
},
);

let rel_link_parsed = parse("</foo/bar>; rel=\"foo/bar\"").unwrap();

assert_eq!(rel_link_expected, rel_link_parsed);
#[cfg(not(feature = "url"))]
{
let mut rel_link_expected = HashMap::new();

rel_link_expected.insert(
Some("foo/bar".to_string()),
Link {
uri: "/foo/bar".parse().unwrap(),
raw_uri: "/foo/bar".to_string(),
queries: HashMap::new(),
params: [("rel".to_string(), "foo/bar".to_string())]
.iter()
.cloned()
.collect(),
},
);

let rel_link_parsed = parse(r#"</foo/bar>; rel="foo/bar""#).unwrap();

assert_eq!(rel_link_expected, rel_link_parsed);
}
}

#[test]
Expand Down