@@ -5,6 +5,8 @@ use std::convert::TryInto;
5
5
6
6
/// Indicates an alternate location for the returned data.
7
7
///
8
+ /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Location)
9
+ ///
8
10
/// # Specifications
9
11
///
10
12
/// - [RFC 7231, section 3.1.4.2: Content-Location](https://tools.ietf.org/html/rfc7231#section-3.1.4.2)
@@ -14,8 +16,8 @@ use std::convert::TryInto;
14
16
/// ```
15
17
/// # fn main() -> http_types::Result<()> {
16
18
/// #
17
- /// use http_types::{Response,Url};
18
- /// use http_types::content::{ ContentLocation} ;
19
+ /// use http_types::{Response, Url};
20
+ /// use http_types::content::ContentLocation;
19
21
///
20
22
/// let content_location = ContentLocation::new(Url::parse("https://example.net/")?);
21
23
///
@@ -24,7 +26,7 @@ use std::convert::TryInto;
24
26
///
25
27
/// let url = Url::parse("https://example.net/")?;
26
28
/// let content_location = ContentLocation::from_headers(url, res)?.unwrap();
27
- /// assert_eq!(content_location.location(), "https://example.net/");
29
+ /// assert_eq!(content_location.location(), &Url::parse( "https://example.net/")? );
28
30
/// #
29
31
/// # Ok(()) }
30
32
/// ```
@@ -81,13 +83,19 @@ impl ContentLocation {
81
83
}
82
84
83
85
/// Get the url.
84
- pub fn location ( & self ) -> String {
85
- self . url . to_string ( )
86
+ pub fn location ( & self ) -> & Url {
87
+ & self . url
86
88
}
87
89
88
90
/// Set the url.
89
- pub fn set_location ( & mut self , location : Url ) {
91
+ pub fn set_location < U > ( & mut self , location : U )
92
+ where
93
+ U : TryInto < Url > ,
94
+ U :: Error : std:: fmt:: Debug ,
95
+ {
90
96
self . url = location
97
+ . try_into ( )
98
+ . expect ( "Could not convert into valid URL" )
91
99
}
92
100
}
93
101
@@ -106,7 +114,10 @@ mod test {
106
114
let content_location =
107
115
ContentLocation :: from_headers ( Url :: parse ( "https://example.net/" ) . unwrap ( ) , headers) ?
108
116
. unwrap ( ) ;
109
- assert_eq ! ( content_location. location( ) , "https://example.net/test.json" ) ;
117
+ assert_eq ! (
118
+ content_location. location( ) ,
119
+ & Url :: parse( "https://example.net/test.json" ) ?
120
+ ) ;
110
121
Ok ( ( ) )
111
122
}
112
123
0 commit comments