diff --git a/Cargo.toml b/Cargo.toml index b2c9dd7..77b7c5a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,4 @@ license = "MIT" http = "0.2.1" regex = "1" lazy_static = "1.4.0" +url = {version = "2.2.2", optional = true } diff --git a/README.md b/README.md index b9bd223..9dc3386 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,17 @@ Refer to (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. diff --git a/src/lib.rs b/src/lib.rs index e8c753d..0255f75 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,10 +58,25 @@ //! Refer to (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`]. @@ -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("; 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#"; rel="foo/bar""#).unwrap(); + + assert_eq!(rel_link_expected, rel_link_parsed); + } } #[test]