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 1 commit
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
33 changes: 25 additions & 8 deletions src/uri/path.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{cmp, fmt, str};
use std::str::FromStr;
use std::{cmp, fmt, str};

use bytes::Bytes;

use super::{ErrorKind, InvalidUri, InvalidUriBytes, URI_CHARS};
use byte_str::ByteStr;
use convert::HttpTryFrom;
use super::{ErrorKind, InvalidUri, InvalidUriBytes, URI_CHARS};
20chan marked this conversation as resolved.
Show resolved Hide resolved

/// Represents the path component of a URI
#[derive(Clone)]
Expand Down Expand Up @@ -56,10 +56,7 @@ impl PathAndQuery {
//
// Allowed: 0x21 / 0x24 - 0x3B / 0x3D / 0x3F - 0x7E
match b {
0x21 |
0x24...0x3B |
0x3D |
0x3F...0x7E => {},
0x21 | 0x24...0x3B | 0x3D | 0x3F...0x7E => {}
20chan marked this conversation as resolved.
Show resolved Hide resolved
_ => return Err(ErrorKind::InvalidUriChar.into()),
}
}
Expand Down Expand Up @@ -108,8 +105,7 @@ impl PathAndQuery {
pub fn from_static(src: &'static str) -> Self {
let src = Bytes::from_static(src.as_bytes());

PathAndQuery::from_shared(src)
.unwrap()
PathAndQuery::from_shared(src).unwrap()
20chan marked this conversation as resolved.
Show resolved Hide resolved
}

pub(super) fn empty() -> Self {
Expand Down Expand Up @@ -169,6 +165,27 @@ impl PathAndQuery {

ret
}
/// Returs the segments of path component
///
/// # Examples
///
/// ```
/// # use http::uri::*;
///
/// let path_and_query: PathAndQuery = "/hello/world".parse().unwrap();
///
/// assert!(path_and_query.path_segments().is_some());
/// assert_eq!(path_and_query.path_segments().unwrap().collect::<Vec<_>>(), vec!["hello",
/// "world"])
/// ```
pub fn path_segments(&self) -> Option<str::Split<char>> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should probably return a custom iterator: struct PathSegments<str::Split<...>>

Is Option significant in this case or can the iterator be returned directly? Uri::path_and_query already returns an Option.

Copy link
Author

Choose a reason for hiding this comment

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

You're right, Option in this context is meanless.

Copy link
Author

Choose a reason for hiding this comment

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

And custom iterator you said, I need help.. I'm so new to rust, When I write any custom iterator, compiler raise lifetime error. T^T

let path = self.path();
if path.starts_with('/') {
Some(path[1..].split('/'))
} else {
None
}
}

/// Returns the query string component
///
Expand Down
25 changes: 21 additions & 4 deletions src/uri/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::str::FromStr;

use super::{ErrorKind, InvalidUri, Uri, URI_CHARS, Port};
use super::{ErrorKind, InvalidUri, Port, Uri, URI_CHARS};
20chan marked this conversation as resolved.
Show resolved Hide resolved

#[test]
fn test_char_table() {
Expand All @@ -12,9 +12,9 @@ fn test_char_table() {
}

macro_rules! part {
($s:expr) => (
($s:expr) => {
20chan marked this conversation as resolved.
Show resolved Hide resolved
Some(&$s.parse().unwrap())
)
};
}

macro_rules! test_parse {
Expand Down Expand Up @@ -138,7 +138,6 @@ test_parse! {
port_part = Port::from_str("3000").ok(),
}


test_parse! {
test_uri_parse_absolute_with_default_port_http,
"http://127.0.0.1:80",
Expand Down Expand Up @@ -377,6 +376,24 @@ test_parse! {
query = Some("foo={bar|baz}\\^`"),
}

#[test]
fn test_uri_parse_segments() {
use super::*;
let path: PathAndQuery = "/foo/bar/baz".parse().unwrap();
assert!(path.path_segments().is_some());
assert_eq!(
path.path_segments().unwrap().collect::<Vec<_>>(),
vec!["foo", "bar", "baz"]
);

let path: PathAndQuery = "/foo/bar/baz/".parse().unwrap();
assert!(path.path_segments().is_some());
assert_eq!(
path.path_segments().unwrap().collect::<Vec<_>>(),
vec!["foo", "bar", "baz", ""]
);
}

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