Skip to content

Commit 2194fd7

Browse files
committed
liburl: rename and move from_str to Url::parse_str.
url::from_str => url::Url::parse_str The FromStr trait still works, but its confusing to have a from_str free function that retuns a Result, while the regular from_str returns an Option, hence the rename. [breaking-change]
1 parent dd812cc commit 2194fd7

File tree

1 file changed

+61
-72
lines changed

1 file changed

+61
-72
lines changed

src/liburl/lib.rs

Lines changed: 61 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,13 @@ use std::uint;
3636
/// # Example
3737
///
3838
/// ```rust
39-
/// use url::{Url, UserInfo};
39+
/// use url::Url;
4040
///
41-
/// let url = Url { scheme: "https".to_string(),
42-
/// user: Some(UserInfo { user: "username".to_string(), pass: None }),
43-
/// host: "example.com".to_string(),
44-
/// port: Some("8080".to_string()),
45-
/// path: "/foo/bar".to_string(),
46-
/// query: vec!(("baz".to_string(), "qux".to_string())),
47-
/// fragment: Some("quz".to_string()) };
48-
/// // https://username@example.com:8080/foo/bar?baz=qux#quz
41+
/// let raw = "https://username@example.com:8080/foo/bar?baz=qux#quz";
42+
/// match Url::parse(raw) {
43+
/// Ok(u) => println!("Parsed '{}'", u),
44+
/// Err(e) => println!("Couldn't parse '{}': {}", raw, e),
45+
/// }
4946
/// ```
5047
#[deriving(Clone, PartialEq, Eq)]
5148
pub struct Url {
@@ -110,6 +107,38 @@ impl Url {
110107
fragment: fragment,
111108
}
112109
}
110+
111+
/// Parses a URL, converting it from a string to a `Url` representation.
112+
///
113+
/// # Arguments
114+
/// * rawurl - a string representing the full URL, including scheme.
115+
///
116+
/// # Return value
117+
///
118+
/// `Err(e)` if the string did not represent a valid URL, where `e` is a
119+
/// `String` error message. Otherwise, `Ok(u)` where `u` is a `Url` struct
120+
/// representing the URL.
121+
pub fn parse(rawurl: &str) -> Result<Url, String> {
122+
// scheme
123+
let (scheme, rest) = try!(get_scheme(rawurl));
124+
125+
// authority
126+
let (userinfo, host, port, rest) = try!(get_authority(rest.as_slice()));
127+
128+
// path
129+
let has_authority = host.len() > 0;
130+
let (path, rest) = try!(get_path(rest.as_slice(), has_authority));
131+
132+
// query and fragment
133+
let (query, fragment) = try!(get_query_fragment(rest.as_slice()));
134+
135+
Ok(Url::new(scheme, userinfo, host, port, path, query, fragment))
136+
}
137+
}
138+
139+
#[deprecated="use `Url::parse`"]
140+
pub fn from_str(s: &str) -> Result<Url, String> {
141+
Url::parse(s)
113142
}
114143

115144
impl Path {
@@ -734,46 +763,6 @@ fn get_query_fragment(rawurl: &str) ->
734763
return Ok((query_from_str(q.as_slice()), f));
735764
}
736765

737-
/**
738-
* Parses a URL, converting it from a string to `Url` representation.
739-
*
740-
* # Arguments
741-
*
742-
* `rawurl` - a string representing the full URL, including scheme.
743-
*
744-
* # Returns
745-
*
746-
* A `Url` struct type representing the URL.
747-
*/
748-
pub fn from_str(rawurl: &str) -> Result<Url, String> {
749-
// scheme
750-
let (scheme, rest) = match get_scheme(rawurl) {
751-
Ok(val) => val,
752-
Err(e) => return Err(e),
753-
};
754-
755-
// authority
756-
let (userinfo, host, port, rest) = match get_authority(rest.as_slice()) {
757-
Ok(val) => val,
758-
Err(e) => return Err(e),
759-
};
760-
761-
// path
762-
let has_authority = host.len() > 0;
763-
let (path, rest) = match get_path(rest.as_slice(), has_authority) {
764-
Ok(val) => val,
765-
Err(e) => return Err(e),
766-
};
767-
768-
// query and fragment
769-
let (query, fragment) = match get_query_fragment(rest.as_slice()) {
770-
Ok(val) => val,
771-
Err(e) => return Err(e),
772-
};
773-
774-
Ok(Url::new(scheme, userinfo, host, port, path, query, fragment))
775-
}
776-
777766
pub fn path_from_str(rawpath: &str) -> Result<Path, String> {
778767
let (path, rest) = match get_path(rawpath, false) {
779768
Ok(val) => val,
@@ -791,7 +780,7 @@ pub fn path_from_str(rawpath: &str) -> Result<Path, String> {
791780

792781
impl FromStr for Url {
793782
fn from_str(s: &str) -> Option<Url> {
794-
match from_str(s) {
783+
match Url::parse(s) {
795784
Ok(url) => Some(url),
796785
Err(_) => None
797786
}
@@ -969,16 +958,16 @@ fn test_get_path() {
969958
#[cfg(test)]
970959
mod tests {
971960
use {encode_form_urlencoded, decode_form_urlencoded,
972-
decode, encode, from_str, encode_component, decode_component,
973-
path_from_str, UserInfo, get_scheme};
961+
decode, encode, encode_component, decode_component,
962+
path_from_str, UserInfo, get_scheme, Url};
974963

975964
use std::collections::HashMap;
976965

977966
#[test]
978967
fn test_url_parse() {
979968
let url = "http://user:pass@rust-lang.org:8080/doc/~u?s=v#something";
980969

981-
let up = from_str(url);
970+
let up = from_str::<Url>(url);
982971
let u = up.unwrap();
983972
assert_eq!(&u.scheme, &"http".to_string());
984973
assert_eq!(&u.user, &Some(UserInfo::new("user".to_string(), Some("pass".to_string()))));
@@ -1003,7 +992,7 @@ mod tests {
1003992
#[test]
1004993
fn test_url_parse_host_slash() {
1005994
let urlstr = "http://0.42.42.42/";
1006-
let url = from_str(urlstr).unwrap();
995+
let url = from_str::<Url>(urlstr).unwrap();
1007996
assert!(url.host == "0.42.42.42".to_string());
1008997
assert!(url.path == "/".to_string());
1009998
}
@@ -1018,14 +1007,14 @@ mod tests {
10181007
#[test]
10191008
fn test_url_host_with_port() {
10201009
let urlstr = "scheme://host:1234";
1021-
let url = from_str(urlstr).unwrap();
1010+
let url = from_str::<Url>(urlstr).unwrap();
10221011
assert_eq!(&url.scheme, &"scheme".to_string());
10231012
assert_eq!(&url.host, &"host".to_string());
10241013
assert_eq!(&url.port, &Some("1234".to_string()));
10251014
// is empty path really correct? Other tests think so
10261015
assert_eq!(&url.path, &"".to_string());
10271016
let urlstr = "scheme://host:1234/";
1028-
let url = from_str(urlstr).unwrap();
1017+
let url = from_str::<Url>(urlstr).unwrap();
10291018
assert_eq!(&url.scheme, &"scheme".to_string());
10301019
assert_eq!(&url.host, &"host".to_string());
10311020
assert_eq!(&url.port, &Some("1234".to_string()));
@@ -1035,7 +1024,7 @@ mod tests {
10351024
#[test]
10361025
fn test_url_with_underscores() {
10371026
let urlstr = "http://dotcom.com/file_name.html";
1038-
let url = from_str(urlstr).unwrap();
1027+
let url = from_str::<Url>(urlstr).unwrap();
10391028
assert!(url.path == "/file_name.html".to_string());
10401029
}
10411030

@@ -1049,7 +1038,7 @@ mod tests {
10491038
#[test]
10501039
fn test_url_with_dashes() {
10511040
let urlstr = "http://dotcom.com/file-name.html";
1052-
let url = from_str(urlstr).unwrap();
1041+
let url = from_str::<Url>(urlstr).unwrap();
10531042
assert!(url.path == "/file-name.html".to_string());
10541043
}
10551044

@@ -1067,75 +1056,75 @@ mod tests {
10671056

10681057
#[test]
10691058
fn test_invalid_scheme_errors() {
1070-
assert!(from_str("99://something").is_err());
1071-
assert!(from_str("://something").is_err());
1059+
assert!(Url::parse("99://something").is_err());
1060+
assert!(Url::parse("://something").is_err());
10721061
}
10731062

10741063
#[test]
10751064
fn test_full_url_parse_and_format() {
10761065
let url = "http://user:pass@rust-lang.org/doc?s=v#something";
1077-
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
1066+
assert_eq!(from_str::<Url>(url).unwrap().to_str().as_slice(), url);
10781067
}
10791068

10801069
#[test]
10811070
fn test_userless_url_parse_and_format() {
10821071
let url = "http://rust-lang.org/doc?s=v#something";
1083-
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
1072+
assert_eq!(from_str::<Url>(url).unwrap().to_str().as_slice(), url);
10841073
}
10851074

10861075
#[test]
10871076
fn test_queryless_url_parse_and_format() {
10881077
let url = "http://user:pass@rust-lang.org/doc#something";
1089-
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
1078+
assert_eq!(from_str::<Url>(url).unwrap().to_str().as_slice(), url);
10901079
}
10911080

10921081
#[test]
10931082
fn test_empty_query_url_parse_and_format() {
10941083
let url = "http://user:pass@rust-lang.org/doc?#something";
10951084
let should_be = "http://user:pass@rust-lang.org/doc#something";
1096-
assert_eq!(from_str(url).unwrap().to_str().as_slice(), should_be);
1085+
assert_eq!(from_str::<Url>(url).unwrap().to_str().as_slice(), should_be);
10971086
}
10981087

10991088
#[test]
11001089
fn test_fragmentless_url_parse_and_format() {
11011090
let url = "http://user:pass@rust-lang.org/doc?q=v";
1102-
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
1091+
assert_eq!(from_str::<Url>(url).unwrap().to_str().as_slice(), url);
11031092
}
11041093

11051094
#[test]
11061095
fn test_minimal_url_parse_and_format() {
11071096
let url = "http://rust-lang.org/doc";
1108-
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
1097+
assert_eq!(from_str::<Url>(url).unwrap().to_str().as_slice(), url);
11091098
}
11101099

11111100
#[test]
11121101
fn test_url_with_port_parse_and_format() {
11131102
let url = "http://rust-lang.org:80/doc";
1114-
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
1103+
assert_eq!(from_str::<Url>(url).unwrap().to_str().as_slice(), url);
11151104
}
11161105

11171106
#[test]
11181107
fn test_scheme_host_only_url_parse_and_format() {
11191108
let url = "http://rust-lang.org";
1120-
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
1109+
assert_eq!(from_str::<Url>(url).unwrap().to_str().as_slice(), url);
11211110
}
11221111

11231112
#[test]
11241113
fn test_pathless_url_parse_and_format() {
11251114
let url = "http://user:pass@rust-lang.org?q=v#something";
1126-
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
1115+
assert_eq!(from_str::<Url>(url).unwrap().to_str().as_slice(), url);
11271116
}
11281117

11291118
#[test]
11301119
fn test_scheme_host_fragment_only_url_parse_and_format() {
11311120
let url = "http://rust-lang.org#something";
1132-
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
1121+
assert_eq!(from_str::<Url>(url).unwrap().to_str().as_slice(), url);
11331122
}
11341123

11351124
#[test]
11361125
fn test_url_component_encoding() {
11371126
let url = "http://rust-lang.org/doc%20uments?ba%25d%20=%23%26%2B";
1138-
let u = from_str(url).unwrap();
1127+
let u = from_str::<Url>(url).unwrap();
11391128
assert!(u.path == "/doc uments".to_string());
11401129
assert!(u.query == vec!(("ba%d ".to_string(), "#&+".to_string())));
11411130
}
@@ -1151,7 +1140,7 @@ mod tests {
11511140
#[test]
11521141
fn test_url_without_authority() {
11531142
let url = "mailto:test@email.com";
1154-
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
1143+
assert_eq!(from_str::<Url>(url).unwrap().to_str().as_slice(), url);
11551144
}
11561145

11571146
#[test]

0 commit comments

Comments
 (0)