Skip to content

Commit fdcb1a6

Browse files
committed
use Url as type for struct and fmt
1 parent 09fb048 commit fdcb1a6

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/content/content_location.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::convert::TryInto;
77
///
88
/// # Specifications
99
///
10-
/// - [RFC 7231, section 3.1.4.2: Content-Length](https://tools.ietf.org/html/rfc7231#section-3.1.4.2)
10+
/// - [RFC 7231, section 3.1.4.2: Content-Location](https://tools.ietf.org/html/rfc7231#section-3.1.4.2)
1111
///
1212
/// # Examples
1313
///
@@ -17,7 +17,7 @@ use std::convert::TryInto;
1717
/// use http_types::{Response,Url};
1818
/// use http_types::content::{ContentLocation};
1919
///
20-
/// let content_location = ContentLocation::new("https://example.net/".to_string());
20+
/// let content_location = ContentLocation::new(Url::parse("https://example.net/")?);
2121
///
2222
/// let mut res = Response::new(200);
2323
/// content_location.apply(&mut res);
@@ -34,16 +34,16 @@ pub struct ContentLocation {
3434

3535
impl ContentLocation {
3636
/// Create a new instance of `Content-Location` header.
37-
pub fn new(url: String) -> Self {
38-
Self { url : Url::parse(&url).unwrap() }
37+
pub fn new(url: Url) -> Self {
38+
Self { url }
3939
}
4040

4141
/// Create a new instance from headers.
4242
pub fn from_headers<U>(base_url: U, headers: impl AsRef<Headers>) -> crate::Result<Option<Self>>
4343
where
4444
U: TryInto<Url>,
4545
U::Error: std::fmt::Debug,
46-
{
46+
{
4747
let headers = match headers.as_ref().get(CONTENT_LOCATION) {
4848
Some(headers) => headers,
4949
None => return Ok(None),
@@ -52,7 +52,7 @@ impl ContentLocation {
5252
// If we successfully parsed the header then there's always at least one
5353
// entry. We want the last entry.
5454
let value = headers.iter().last().unwrap();
55-
let base = base_url.try_into()?;
55+
let base = base_url.try_into().unwrap();
5656
let url = base.join(value.as_str().trim()).status(400)?;
5757
Ok(Some(Self { url }))
5858
}
@@ -81,8 +81,8 @@ impl ContentLocation {
8181
}
8282

8383
/// Set the url.
84-
pub fn set_location(&mut self, location: &str) {
85-
self.url = Url::parse(location).unwrap();
84+
pub fn set_location(&mut self, location: Url) {
85+
self.url = location
8686
}
8787
}
8888

@@ -93,12 +93,14 @@ mod test {
9393

9494
#[test]
9595
fn smoke() -> crate::Result<()> {
96-
let content_location = ContentLocation::new("https://example.net/test.json".to_string());
96+
let content_location = ContentLocation::new(Url::parse("https://example.net/test.json")?);
9797

9898
let mut headers = Headers::new();
9999
content_location.apply(&mut headers);
100100

101-
let content_location = ContentLocation::from_headers( Url::parse("https://example.net/").unwrap(), headers )?.unwrap();
101+
let content_location =
102+
ContentLocation::from_headers(Url::parse("https://example.net/").unwrap(), headers)?
103+
.unwrap();
102104
assert_eq!(content_location.location(), "https://example.net/test.json");
103105
Ok(())
104106
}
@@ -107,7 +109,9 @@ mod test {
107109
fn bad_request_on_parse_error() -> crate::Result<()> {
108110
let mut headers = Headers::new();
109111
headers.insert(CONTENT_LOCATION, "htt://<nori ate the tag. yum.>");
110-
let err = ContentLocation::from_headers(Url::parse("https://example.net").unwrap(), headers).unwrap_err();
112+
let err =
113+
ContentLocation::from_headers(Url::parse("https://example.net").unwrap(), headers)
114+
.unwrap_err();
111115
assert_eq!(err.status(), 400);
112116
Ok(())
113117
}

src/content/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
pub mod accept_encoding;
99
pub mod content_encoding;
1010

11+
mod content_location;
1112
mod encoding;
1213
mod encoding_proposal;
13-
mod content_location;
1414

1515
#[doc(inline)]
1616
pub use accept_encoding::AcceptEncoding;
1717
#[doc(inline)]
1818
pub use content_encoding::ContentEncoding;
19+
pub use content_location::ContentLocation;
1920
pub use encoding::Encoding;
2021
pub use encoding_proposal::EncodingProposal;
21-
pub use content_location::ContentLocation;

0 commit comments

Comments
 (0)