Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add path_segments to PathAndSegments #276

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/uri/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of implementing Deref, could you implement, could we implement Iterator<Item = &'a str> and DoubleEndedIterator? The impl would proxy to str::Slice<'a, char>'s implementation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think that the the CI failure is due to referencing std instead of ::std.

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 {
Expand Down Expand Up @@ -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<_>>(),
/// 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
Expand Down
11 changes: 11 additions & 0 deletions src/uri/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<_>>(),
vec!["foo", "bar", "baz"]
);
}

#[test]
fn test_uri_parse_error() {
fn err(s: &str) {
Expand Down