]> git.lizzy.rs Git - rust.git/commitdiff
liburl: remove redundant fields in Url.
authorKevin Butler <haqkrs@gmail.com>
Fri, 20 Jun 2014 23:05:06 +0000 (00:05 +0100)
committerKevin Butler <haqkrs@gmail.com>
Fri, 4 Jul 2014 00:28:20 +0000 (01:28 +0100)
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]

src/liburl/lib.rs

index dbdd7a6aa1736318da9facfccdb5c8b676bac878..a5031e11d4cd03c8566f25e2481bcac4cc347058 100644 (file)
@@ -54,22 +54,16 @@ pub struct Url {
     pub host: String,
     /// A TCP port number, for example `8080`.
     pub port: Option<String>,
-    /// The path component of a URL, for example `/foo/bar`.
-    pub path: String,
-    /// The query component of a URL.
-    /// `vec!(("baz".to_string(), "qux".to_string()))` represents the fragment
-    /// `baz=qux` in the above example.
-    pub query: Query,
-    /// The fragment component, such as `quz`. Not including the leading `#` character.
-    pub fragment: Option<String>
+    /// The path component of a URL, for example `/foo/bar?baz=qux#quz`.
+    pub path: Path,
 }
 
-#[deriving(Clone, PartialEq)]
+#[deriving(Clone, PartialEq, Eq)]
 pub struct Path {
     /// The path component of a URL, for example `/foo/bar`.
     pub path: String,
     /// The query component of a URL.
-    /// `vec!(("baz".to_string(), "qux".to_string()))` represents the fragment
+    /// `vec![("baz".to_string(), "qux".to_string())]` represents the fragment
     /// `baz=qux` in the above example.
     pub query: Query,
     /// The fragment component, such as `quz`. Not including the leading `#` character.
@@ -102,9 +96,7 @@ pub fn new(scheme: String,
             user: user,
             host: host,
             port: port,
-            path: path,
-            query: query,
-            fragment: fragment,
+            path: Path::new(path, query, fragment)
         }
     }
 
@@ -836,18 +828,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
             }
         }
 
-        try!(write!(f, "{}", self.path));
-
-        if !self.query.is_empty() {
-            try!(write!(f, "?{}", query_to_str(&self.query)));
-        }
-
-        match self.fragment {
-            Some(ref fragment) => {
-                write!(f, "#{}", encode_component(fragment.as_slice()))
-            }
-            None => Ok(()),
-        }
+        write!(f, "{}", self.path)
     }
 }
 
@@ -855,7 +836,7 @@ impl fmt::Show for Path {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         try!(write!(f, "{}", self.path));
         if !self.query.is_empty() {
-            try!(write!(f, "?{}", self.query))
+            try!(write!(f, "?{}", query_to_str(&self.query)))
         }
 
         match self.fragment {
@@ -981,9 +962,9 @@ fn test_url_parse() {
         assert_eq!(&u.user, &Some(UserInfo::new("user".to_string(), Some("pass".to_string()))));
         assert_eq!(&u.host, &"rust-lang.org".to_string());
         assert_eq!(&u.port, &Some("8080".to_string()));
-        assert_eq!(&u.path, &"/doc/~u".to_string());
-        assert_eq!(&u.query, &vec!(("s".to_string(), "v".to_string())));
-        assert_eq!(&u.fragment, &Some("something".to_string()));
+        assert_eq!(&u.path.path, &"/doc/~u".to_string());
+        assert_eq!(&u.path.query, &vec!(("s".to_string(), "v".to_string())));
+        assert_eq!(&u.path.fragment, &Some("something".to_string()));
     }
 
     #[test]
@@ -1002,7 +983,7 @@ fn test_url_parse_host_slash() {
         let urlstr = "http://0.42.42.42/";
         let url = from_str::<Url>(urlstr).unwrap();
         assert!(url.host == "0.42.42.42".to_string());
-        assert!(url.path == "/".to_string());
+        assert!(url.path.path == "/".to_string());
     }
 
     #[test]
@@ -1020,20 +1001,20 @@ fn test_url_host_with_port() {
         assert_eq!(&url.host, &"host".to_string());
         assert_eq!(&url.port, &Some("1234".to_string()));
         // is empty path really correct? Other tests think so
-        assert_eq!(&url.path, &"".to_string());
+        assert_eq!(&url.path.path, &"".to_string());
         let urlstr = "scheme://host:1234/";
         let url = from_str::<Url>(urlstr).unwrap();
         assert_eq!(&url.scheme, &"scheme".to_string());
         assert_eq!(&url.host, &"host".to_string());
         assert_eq!(&url.port, &Some("1234".to_string()));
-        assert_eq!(&url.path, &"/".to_string());
+        assert_eq!(&url.path.path, &"/".to_string());
     }
 
     #[test]
     fn test_url_with_underscores() {
         let urlstr = "http://dotcom.com/file_name.html";
         let url = from_str::<Url>(urlstr).unwrap();
-        assert!(url.path == "/file_name.html".to_string());
+        assert!(url.path.path == "/file_name.html".to_string());
     }
 
     #[test]
@@ -1047,7 +1028,7 @@ fn test_path_with_underscores() {
     fn test_url_with_dashes() {
         let urlstr = "http://dotcom.com/file-name.html";
         let url = from_str::<Url>(urlstr).unwrap();
-        assert!(url.path == "/file-name.html".to_string());
+        assert!(url.path.path == "/file-name.html".to_string());
     }
 
     #[test]
@@ -1133,8 +1114,8 @@ fn test_scheme_host_fragment_only_url_parse_and_format() {
     fn test_url_component_encoding() {
         let url = "http://rust-lang.org/doc%20uments?ba%25d%20=%23%26%2B";
         let u = from_str::<Url>(url).unwrap();
-        assert!(u.path == "/doc uments".to_string());
-        assert!(u.query == vec!(("ba%d ".to_string(), "#&+".to_string())));
+        assert!(u.path.path == "/doc uments".to_string());
+        assert!(u.path.query == vec!(("ba%d ".to_string(), "#&+".to_string())));
     }
 
     #[test]