aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Walters <rwalters@digitalstirling.com>2020-10-23 20:03:06 -0700
committerRichard Walters <rwalters@digitalstirling.com>2020-10-23 20:03:06 -0700
commitd8551fbf9514a8eea30d675f65c11b326193d164 (patch)
tree8aeb7c6d5e89c5e0332c54d1511ee8218f0fcf09
parentbc57f058e630f9f2e53867b48e0e710c72d7d8ec (diff)
Add functions to remove parts of a URI and return them
-rw-r--r--src/uri.rs43
1 files changed, 43 insertions, 0 deletions
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<Authority> {
+ self.authority.take()
+ }
+
+ /// Remove and return the fragment portion (if any) of the URI.
+ #[must_use]
+ pub fn take_fragment(&mut self) -> Option<Vec<u8>> {
+ self.fragment.take()
+ }
+
+ /// Remove and return the query portion (if any) of the URI.
+ #[must_use]
+ pub fn take_query(&mut self) -> Option<Vec<u8>> {
+ self.query.take()
+ }
+
+ /// Remove and return the scheme portion (if any) of the URI.
+ #[must_use]
+ pub fn take_scheme(&mut self) -> Option<String> {
+ 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());
+ }
+
}