diff --git a/src/uri/path.rs b/src/uri/path.rs index 5013626c..84caac47 100644 --- a/src/uri/path.rs +++ b/src/uri/path.rs @@ -7,6 +7,18 @@ use byte_str::ByteStr; use convert::HttpTryFrom; use super::{ErrorKind, InvalidUri, InvalidUriBytes, URI_CHARS}; +/// Represents the segments of a URI +#[derive(Debug)] +pub struct PathSegments<'a>(str::Split<'a, char>); + +impl<'a> std::ops::Deref for PathSegments<'a> { + type Target = str::Split<'a, char>; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + /// Represents the path component of a URI #[derive(Clone)] pub struct PathAndQuery { @@ -170,6 +182,25 @@ impl PathAndQuery { ret } + /// Returs the segments of path component + /// + /// # Examples + /// + /// ``` + /// # use http::uri::*; + /// + /// let path_and_query : PathAndQuery = "/hello/world".parse().unwrap(); + /// + /// assert_eq!( + /// path_and_query.path_segments().to_owned().collect::>(), + /// vec!["hello", "world"] + /// ); + /// ``` + pub fn path_segments(&self) -> PathSegments + { + PathSegments(self.path()[1..].split('/')) + } + /// Returns the query string component /// /// The query component contains non-hierarchical data that, along with data diff --git a/src/uri/tests.rs b/src/uri/tests.rs index f580e89a..a183250b 100644 --- a/src/uri/tests.rs +++ b/src/uri/tests.rs @@ -377,6 +377,17 @@ test_parse! { query = Some("foo={bar|baz}\\^`"), } +#[test] +fn test_uri_parse_segments() { + use super::*; + let path: PathAndQuery = "/foo/bar/baz".parse().unwrap(); + let segments = path.path_segments(); + assert_eq!( + segments.to_owned().collect::>(), + vec!["foo", "bar", "baz"] + ); +} + #[test] fn test_uri_parse_error() { fn err(s: &str) {