Skip to content

Commit ed47c47

Browse files
committed
liburl: remove redundant fields in Url.
url.path - Now a Path instead of a String. To fix old code: url.path => url.path.path url.query => url.path.query url.fragment => url.path.fragment Not much point having the Path struct if it's not going to be used. [breaking-change]
1 parent a9e82e1 commit ed47c47

File tree

1 file changed

+17
-36
lines changed

1 file changed

+17
-36
lines changed

src/liburl/lib.rs

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,16 @@ pub struct Url {
5454
pub host: String,
5555
/// A TCP port number, for example `8080`.
5656
pub port: Option<String>,
57-
/// The path component of a URL, for example `/foo/bar`.
58-
pub path: String,
59-
/// The query component of a URL.
60-
/// `vec!(("baz".to_string(), "qux".to_string()))` represents the fragment
61-
/// `baz=qux` in the above example.
62-
pub query: Query,
63-
/// The fragment component, such as `quz`. Not including the leading `#` character.
64-
pub fragment: Option<String>
57+
/// The path component of a URL, for example `/foo/bar?baz=qux#quz`.
58+
pub path: Path,
6559
}
6660

67-
#[deriving(Clone, PartialEq)]
61+
#[deriving(Clone, PartialEq, Eq)]
6862
pub struct Path {
6963
/// The path component of a URL, for example `/foo/bar`.
7064
pub path: String,
7165
/// The query component of a URL.
72-
/// `vec!(("baz".to_string(), "qux".to_string()))` represents the fragment
66+
/// `vec![("baz".to_string(), "qux".to_string())]` represents the fragment
7367
/// `baz=qux` in the above example.
7468
pub query: Query,
7569
/// The fragment component, such as `quz`. Not including the leading `#` character.
@@ -102,9 +96,7 @@ impl Url {
10296
user: user,
10397
host: host,
10498
port: port,
105-
path: path,
106-
query: query,
107-
fragment: fragment,
99+
path: Path::new(path, query, fragment)
108100
}
109101
}
110102

@@ -836,26 +828,15 @@ impl fmt::Show for Url {
836828
}
837829
}
838830

839-
try!(write!(f, "{}", self.path));
840-
841-
if !self.query.is_empty() {
842-
try!(write!(f, "?{}", query_to_str(&self.query)));
843-
}
844-
845-
match self.fragment {
846-
Some(ref fragment) => {
847-
write!(f, "#{}", encode_component(fragment.as_slice()))
848-
}
849-
None => Ok(()),
850-
}
831+
write!(f, "{}", self.path)
851832
}
852833
}
853834

854835
impl fmt::Show for Path {
855836
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
856837
try!(write!(f, "{}", self.path));
857838
if !self.query.is_empty() {
858-
try!(write!(f, "?{}", self.query))
839+
try!(write!(f, "?{}", query_to_str(&self.query)))
859840
}
860841

861842
match self.fragment {
@@ -981,9 +962,9 @@ mod tests {
981962
assert_eq!(&u.user, &Some(UserInfo::new("user".to_string(), Some("pass".to_string()))));
982963
assert_eq!(&u.host, &"rust-lang.org".to_string());
983964
assert_eq!(&u.port, &Some("8080".to_string()));
984-
assert_eq!(&u.path, &"/doc/~u".to_string());
985-
assert_eq!(&u.query, &vec!(("s".to_string(), "v".to_string())));
986-
assert_eq!(&u.fragment, &Some("something".to_string()));
965+
assert_eq!(&u.path.path, &"/doc/~u".to_string());
966+
assert_eq!(&u.path.query, &vec!(("s".to_string(), "v".to_string())));
967+
assert_eq!(&u.path.fragment, &Some("something".to_string()));
987968
}
988969

989970
#[test]
@@ -1002,7 +983,7 @@ mod tests {
1002983
let urlstr = "http://0.42.42.42/";
1003984
let url = from_str::<Url>(urlstr).unwrap();
1004985
assert!(url.host == "0.42.42.42".to_string());
1005-
assert!(url.path == "/".to_string());
986+
assert!(url.path.path == "/".to_string());
1006987
}
1007988

1008989
#[test]
@@ -1020,20 +1001,20 @@ mod tests {
10201001
assert_eq!(&url.host, &"host".to_string());
10211002
assert_eq!(&url.port, &Some("1234".to_string()));
10221003
// is empty path really correct? Other tests think so
1023-
assert_eq!(&url.path, &"".to_string());
1004+
assert_eq!(&url.path.path, &"".to_string());
10241005
let urlstr = "scheme://host:1234/";
10251006
let url = from_str::<Url>(urlstr).unwrap();
10261007
assert_eq!(&url.scheme, &"scheme".to_string());
10271008
assert_eq!(&url.host, &"host".to_string());
10281009
assert_eq!(&url.port, &Some("1234".to_string()));
1029-
assert_eq!(&url.path, &"/".to_string());
1010+
assert_eq!(&url.path.path, &"/".to_string());
10301011
}
10311012

10321013
#[test]
10331014
fn test_url_with_underscores() {
10341015
let urlstr = "http://dotcom.com/file_name.html";
10351016
let url = from_str::<Url>(urlstr).unwrap();
1036-
assert!(url.path == "/file_name.html".to_string());
1017+
assert!(url.path.path == "/file_name.html".to_string());
10371018
}
10381019

10391020
#[test]
@@ -1047,7 +1028,7 @@ mod tests {
10471028
fn test_url_with_dashes() {
10481029
let urlstr = "http://dotcom.com/file-name.html";
10491030
let url = from_str::<Url>(urlstr).unwrap();
1050-
assert!(url.path == "/file-name.html".to_string());
1031+
assert!(url.path.path == "/file-name.html".to_string());
10511032
}
10521033

10531034
#[test]
@@ -1133,8 +1114,8 @@ mod tests {
11331114
fn test_url_component_encoding() {
11341115
let url = "http://rust-lang.org/doc%20uments?ba%25d%20=%23%26%2B";
11351116
let u = from_str::<Url>(url).unwrap();
1136-
assert!(u.path == "/doc uments".to_string());
1137-
assert!(u.query == vec!(("ba%d ".to_string(), "#&+".to_string())));
1117+
assert!(u.path.path == "/doc uments".to_string());
1118+
assert!(u.path.query == vec!(("ba%d ".to_string(), "#&+".to_string())));
11381119
}
11391120

11401121
#[test]

0 commit comments

Comments
 (0)