From d8551fbf9514a8eea30d675f65c11b326193d164 Mon Sep 17 00:00:00 2001 From: Richard Walters Date: Fri, 23 Oct 2020 20:03:06 -0700 Subject: Add functions to remove parts of a URI and return them --- src/uri.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/uri.rs b/src/uri.rs index ea362c5..87935a5 100644 --- a/src/uri.rs +++ b/src/uri.rs @@ -684,6 +684,30 @@ impl Uri { } } + /// Remove and return the authority portion (if any) of the URI. + #[must_use] + pub fn take_authority(&mut self) -> Option { + self.authority.take() + } + + /// Remove and return the fragment portion (if any) of the URI. + #[must_use] + pub fn take_fragment(&mut self) -> Option> { + self.fragment.take() + } + + /// Remove and return the query portion (if any) of the URI. + #[must_use] + pub fn take_query(&mut self) -> Option> { + self.query.take() + } + + /// Remove and return the scheme portion (if any) of the URI. + #[must_use] + pub fn take_scheme(&mut self) -> Option { + self.scheme.take() + } + /// Borrow the userinfo portion (if any) of the Authority (if any) of the /// URI. /// @@ -1512,4 +1536,23 @@ mod tests { } } + #[test] + fn take_parts() { + let mut uri = Uri::parse("https://www.example.com/foo?bar#baz").unwrap(); + assert_eq!(Some("https"), uri.take_scheme().as_deref()); + assert_eq!("//www.example.com/foo?bar#baz", uri.to_string()); + assert!(matches!( + uri.take_authority(), + Some(authority) if authority.host() == b"www.example.com" + )); + assert_eq!("/foo?bar#baz", uri.to_string()); + assert!(matches!(uri.take_authority(), None)); + assert_eq!(Some(&b"bar"[..]), uri.take_query().as_deref()); + assert_eq!("/foo#baz", uri.to_string()); + assert_eq!(None, uri.take_query().as_deref()); + assert_eq!(Some(&b"baz"[..]), uri.take_fragment().as_deref()); + assert_eq!("/foo", uri.to_string()); + assert_eq!(None, uri.take_fragment().as_deref()); + } + } -- cgit v1.2.3