@@ -60,7 +60,7 @@ pub struct Url {
60
60
/// `vec!(("baz".to_string(), "qux".to_string()))` represents the fragment
61
61
/// `baz=qux` in the above example.
62
62
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.
64
64
pub fragment : Option < String >
65
65
}
66
66
@@ -72,7 +72,7 @@ pub struct Path {
72
72
/// `vec!(("baz".to_string(), "qux".to_string()))` represents the fragment
73
73
/// `baz=qux` in the above example.
74
74
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.
76
76
pub fragment : Option < String >
77
77
}
78
78
@@ -152,6 +152,30 @@ impl Path {
152
152
fragment : fragment,
153
153
}
154
154
}
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)
155
179
}
156
180
157
181
impl UserInfo {
@@ -763,21 +787,6 @@ fn get_query_fragment(rawurl: &str) ->
763
787
return Ok ( ( query_from_str ( q. as_slice ( ) ) , f) ) ;
764
788
}
765
789
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
-
781
790
impl FromStr for Url {
782
791
fn from_str ( s : & str ) -> Option < Url > {
783
792
match Url :: parse ( s) {
@@ -789,7 +798,7 @@ impl FromStr for Url {
789
798
790
799
impl FromStr for Path {
791
800
fn from_str ( s : & str ) -> Option < Path > {
792
- match path_from_str ( s) {
801
+ match Path :: parse ( s) {
793
802
Ok ( path) => Some ( path) ,
794
803
Err ( _) => None
795
804
}
@@ -957,9 +966,8 @@ fn test_get_path() {
957
966
958
967
#[ cfg( test) ]
959
968
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 } ;
963
971
964
972
use std:: collections:: HashMap ;
965
973
@@ -982,7 +990,7 @@ mod tests {
982
990
fn test_path_parse ( ) {
983
991
let path = "/doc/~u?s=v#something" ;
984
992
985
- let up = path_from_str ( path) ;
993
+ let up = from_str :: < Path > ( path) ;
986
994
let u = up. unwrap ( ) ;
987
995
assert_eq ! ( & u. path, & "/doc/~u" . to_string( ) ) ;
988
996
assert_eq ! ( & u. query, & vec!( ( "s" . to_string( ) , "v" . to_string( ) ) ) ) ;
@@ -1000,7 +1008,7 @@ mod tests {
1000
1008
#[ test]
1001
1009
fn test_path_parse_host_slash ( ) {
1002
1010
let pathstr = "/" ;
1003
- let path = path_from_str ( pathstr) . unwrap ( ) ;
1011
+ let path = from_str :: < Path > ( pathstr) . unwrap ( ) ;
1004
1012
assert ! ( path. path == "/" . to_string( ) ) ;
1005
1013
}
1006
1014
@@ -1031,7 +1039,7 @@ mod tests {
1031
1039
#[ test]
1032
1040
fn test_path_with_underscores ( ) {
1033
1041
let pathstr = "/file_name.html" ;
1034
- let path = path_from_str ( pathstr) . unwrap ( ) ;
1042
+ let path = from_str :: < Path > ( pathstr) . unwrap ( ) ;
1035
1043
assert ! ( path. path == "/file_name.html" . to_string( ) ) ;
1036
1044
}
1037
1045
@@ -1045,7 +1053,7 @@ mod tests {
1045
1053
#[ test]
1046
1054
fn test_path_with_dashes ( ) {
1047
1055
let pathstr = "/file-name.html" ;
1048
- let path = path_from_str ( pathstr) . unwrap ( ) ;
1056
+ let path = from_str :: < Path > ( pathstr) . unwrap ( ) ;
1049
1057
assert ! ( path. path == "/file-name.html" . to_string( ) ) ;
1050
1058
}
1051
1059
@@ -1132,7 +1140,7 @@ mod tests {
1132
1140
#[ test]
1133
1141
fn test_path_component_encoding ( ) {
1134
1142
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 ( ) ;
1136
1144
assert ! ( p. path == "/doc uments" . to_string( ) ) ;
1137
1145
assert ! ( p. query == vec!( ( "ba%d " . to_string( ) , "#&+" . to_string( ) ) ) ) ;
1138
1146
}
0 commit comments