Skip to content

Commit f705698

Browse files
committed
---
yaml --- r: 153148 b: refs/heads/try2 c: a9e82e1 h: refs/heads/master v: v3
1 parent 6b82b7e commit f705698

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 2194fd7569b227ba083c4f27e156af939e061c1b
8+
refs/heads/try2: a9e82e145e61f4b9825b4622f78ea9364387ae70
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/liburl/lib.rs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub struct Url {
6060
/// `vec!(("baz".to_string(), "qux".to_string()))` represents the fragment
6161
/// `baz=qux` in the above example.
6262
pub query: Query,
63-
/// The fragment component, such as `quz`. Doesn't include the leading `#` character.
63+
/// The fragment component, such as `quz`. Not including the leading `#` character.
6464
pub fragment: Option<String>
6565
}
6666

@@ -72,7 +72,7 @@ pub struct Path {
7272
/// `vec!(("baz".to_string(), "qux".to_string()))` represents the fragment
7373
/// `baz=qux` in the above example.
7474
pub query: Query,
75-
/// The fragment component, such as `quz`. Doesn't include the leading `#` character.
75+
/// The fragment component, such as `quz`. Not including the leading `#` character.
7676
pub fragment: Option<String>
7777
}
7878

@@ -152,6 +152,30 @@ impl Path {
152152
fragment: fragment,
153153
}
154154
}
155+
156+
/// Parses a URL path, converting it from a string to a `Path` representation.
157+
///
158+
/// # Arguments
159+
/// * rawpath - a string representing the path component of a URL.
160+
///
161+
/// # Return value
162+
///
163+
/// `Err(e)` if the string did not represent a valid URL path, where `e` is a
164+
/// `String` error message. Otherwise, `Ok(p)` where `p` is a `Path` struct
165+
/// representing the URL path.
166+
pub fn parse(rawpath: &str) -> Result<Path, String> {
167+
let (path, rest) = try!(get_path(rawpath, false));
168+
169+
// query and fragment
170+
let (query, fragment) = try!(get_query_fragment(rest.as_slice()));
171+
172+
Ok(Path{ path: path, query: query, fragment: fragment })
173+
}
174+
}
175+
176+
#[deprecated="use `Path::parse`"]
177+
pub fn path_from_str(s: &str) -> Result<Path, String> {
178+
Path::parse(s)
155179
}
156180

157181
impl UserInfo {
@@ -763,21 +787,6 @@ fn get_query_fragment(rawurl: &str) ->
763787
return Ok((query_from_str(q.as_slice()), f));
764788
}
765789

766-
pub fn path_from_str(rawpath: &str) -> Result<Path, String> {
767-
let (path, rest) = match get_path(rawpath, false) {
768-
Ok(val) => val,
769-
Err(e) => return Err(e)
770-
};
771-
772-
// query and fragment
773-
let (query, fragment) = match get_query_fragment(rest.as_slice()) {
774-
Ok(val) => val,
775-
Err(e) => return Err(e),
776-
};
777-
778-
Ok(Path{ path: path, query: query, fragment: fragment })
779-
}
780-
781790
impl FromStr for Url {
782791
fn from_str(s: &str) -> Option<Url> {
783792
match Url::parse(s) {
@@ -789,7 +798,7 @@ impl FromStr for Url {
789798

790799
impl FromStr for Path {
791800
fn from_str(s: &str) -> Option<Path> {
792-
match path_from_str(s) {
801+
match Path::parse(s) {
793802
Ok(path) => Some(path),
794803
Err(_) => None
795804
}
@@ -957,9 +966,8 @@ fn test_get_path() {
957966

958967
#[cfg(test)]
959968
mod tests {
960-
use {encode_form_urlencoded, decode_form_urlencoded,
961-
decode, encode, encode_component, decode_component,
962-
path_from_str, UserInfo, get_scheme, Url};
969+
use {encode_form_urlencoded, decode_form_urlencoded, decode, encode,
970+
encode_component, decode_component, UserInfo, get_scheme, Url, Path};
963971

964972
use std::collections::HashMap;
965973

@@ -982,7 +990,7 @@ mod tests {
982990
fn test_path_parse() {
983991
let path = "/doc/~u?s=v#something";
984992

985-
let up = path_from_str(path);
993+
let up = from_str::<Path>(path);
986994
let u = up.unwrap();
987995
assert_eq!(&u.path, &"/doc/~u".to_string());
988996
assert_eq!(&u.query, &vec!(("s".to_string(), "v".to_string())));
@@ -1000,7 +1008,7 @@ mod tests {
10001008
#[test]
10011009
fn test_path_parse_host_slash() {
10021010
let pathstr = "/";
1003-
let path = path_from_str(pathstr).unwrap();
1011+
let path = from_str::<Path>(pathstr).unwrap();
10041012
assert!(path.path == "/".to_string());
10051013
}
10061014

@@ -1031,7 +1039,7 @@ mod tests {
10311039
#[test]
10321040
fn test_path_with_underscores() {
10331041
let pathstr = "/file_name.html";
1034-
let path = path_from_str(pathstr).unwrap();
1042+
let path = from_str::<Path>(pathstr).unwrap();
10351043
assert!(path.path == "/file_name.html".to_string());
10361044
}
10371045

@@ -1045,7 +1053,7 @@ mod tests {
10451053
#[test]
10461054
fn test_path_with_dashes() {
10471055
let pathstr = "/file-name.html";
1048-
let path = path_from_str(pathstr).unwrap();
1056+
let path = from_str::<Path>(pathstr).unwrap();
10491057
assert!(path.path == "/file-name.html".to_string());
10501058
}
10511059

@@ -1132,7 +1140,7 @@ mod tests {
11321140
#[test]
11331141
fn test_path_component_encoding() {
11341142
let path = "/doc%20uments?ba%25d%20=%23%26%2B";
1135-
let p = path_from_str(path).unwrap();
1143+
let p = from_str::<Path>(path).unwrap();
11361144
assert!(p.path == "/doc uments".to_string());
11371145
assert!(p.query == vec!(("ba%d ".to_string(), "#&+".to_string())));
11381146
}

0 commit comments

Comments
 (0)