Просмотр исходного кода

Move URLUtil setters to a separate trait

Simon Sapin 12 лет назад
Родитель
Сommit
6652cd3086
1 измененных файлов с 83 добавлено и 66 удалено
  1. 83 66
      src/url.rs

+ 83 - 66
src/url.rs

@@ -105,8 +105,82 @@ impl Url {
         parser::parse_url(input, base_url, silent_handler)
     }
 
+    pub fn serialize(&self) -> String {
+        let mut result = self.serialize_no_fragment();
+        match self.fragment {
+            None => (),
+            Some(ref fragment) => {
+                result.push_str("#");
+                result.push_str(fragment.as_slice());
+            }
+        }
+        result
+    }
+
+    pub fn serialize_no_fragment(&self) -> String {
+        let mut result = self.scheme.clone();
+        result.push_str(":");
+        match self.scheme_data {
+            RelativeSchemeData(SchemeRelativeUrl {
+                ref username, ref password, ref host, ref port, ref path
+            }) => {
+                result.push_str("//");
+                if !username.is_empty() || password.is_some() {
+                    result.push_str(username.as_slice());
+                    match password {
+                        &None => (),
+                        &Some(ref password) => {
+                            result.push_str(":");
+                            result.push_str(password.as_slice());
+                        }
+                    }
+                    result.push_str("@");
+                }
+                result.push_str(host.serialize().as_slice());
+                if port.len() > 0 {
+                    result.push_str(":");
+                    result.push_str(port.as_slice());
+                }
+                if path.len() > 0 {
+                    for path_part in path.iter() {
+                        result.push_str("/");
+                        result.push_str(path_part.as_slice());
+                    }
+                } else {
+                    result.push_str("/");
+                }
+            },
+            OtherSchemeData(ref data) => result.push_str(data.as_slice()),
+        }
+        match self.query {
+            None => (),
+            Some(ref query) => {
+                result.push_str("?");
+                result.push_str(query.as_slice());
+            }
+        }
+        result
+    }
+}
+
+
+/// These methods are not meant for use in Rust code,
+/// only to help implement the JavaScript URLUtils API: http://url.spec.whatwg.org/#urlutils
+trait UrlUtils {
+    fn set_scheme(&mut self, input: &str) -> ParseResult<()>;
+    fn set_username(&mut self, input: &str) -> ParseResult<()>;
+    fn set_password(&mut self, input: &str) -> ParseResult<()>;
+    fn set_host_and_port(&mut self, input: &str) -> ParseResult<()>;
+    fn set_host(&mut self, input: &str) -> ParseResult<()>;
+    fn set_port(&mut self, input: &str) -> ParseResult<()>;
+    fn set_path(&mut self, input: &str) -> ParseResult<()>;
+    fn set_query(&mut self, input: &str) -> ParseResult<()>;
+    fn set_fragment(&mut self, input: &str) -> ParseResult<()>;
+}
+
+impl UrlUtils for Url {
     /// `URLUtils.protocol` setter
-    pub fn set_scheme(&mut self, input: &str) -> ParseResult<()> {
+    fn set_scheme(&mut self, input: &str) -> ParseResult<()> {
         match parser::parse_scheme(input.as_slice(), parser::SetterContext) {
             Some((scheme, _)) => {
                 self.scheme = scheme;
@@ -117,7 +191,7 @@ impl Url {
     }
 
     /// `URLUtils.username` setter
-    pub fn set_username(&mut self, input: &str) -> ParseResult<()> {
+    fn set_username(&mut self, input: &str) -> ParseResult<()> {
         match self.scheme_data {
             RelativeSchemeData(SchemeRelativeUrl { ref mut username, .. }) => {
                 username.truncate(0);
@@ -129,7 +203,7 @@ impl Url {
     }
 
     /// `URLUtils.password` setter
-    pub fn set_password(&mut self, input: &str) -> ParseResult<()> {
+    fn set_password(&mut self, input: &str) -> ParseResult<()> {
         match self.scheme_data {
             RelativeSchemeData(SchemeRelativeUrl { ref mut password, .. }) => {
                 let mut new_password = String::new();
@@ -142,7 +216,7 @@ impl Url {
     }
 
     /// `URLUtils.host` setter
-    pub fn set_host_and_port(&mut self, input: &str) -> ParseResult<()> {
+    fn set_host_and_port(&mut self, input: &str) -> ParseResult<()> {
         match self.scheme_data {
             RelativeSchemeData(SchemeRelativeUrl { ref mut host, ref mut port, .. }) => {
                 let (new_host, new_port, _) = try!(parser::parse_host(
@@ -156,7 +230,7 @@ impl Url {
     }
 
     /// `URLUtils.hostname` setter
-    pub fn set_host(&mut self, input: &str) -> ParseResult<()> {
+    fn set_host(&mut self, input: &str) -> ParseResult<()> {
         match self.scheme_data {
             RelativeSchemeData(SchemeRelativeUrl { ref mut host, .. }) => {
                 let (new_host, _) = try!(parser::parse_hostname(
@@ -169,7 +243,7 @@ impl Url {
     }
 
     /// `URLUtils.port` setter
-    pub fn set_port(&mut self, input: &str) -> ParseResult<()> {
+    fn set_port(&mut self, input: &str) -> ParseResult<()> {
         match self.scheme_data {
             RelativeSchemeData(SchemeRelativeUrl { ref mut port, .. }) => {
                 if self.scheme.as_slice() == "file" {
@@ -185,7 +259,7 @@ impl Url {
     }
 
     /// `URLUtils.pathname` setter
-    pub fn set_path(&mut self, input: &str) -> ParseResult<()> {
+    fn set_path(&mut self, input: &str) -> ParseResult<()> {
         match self.scheme_data {
             RelativeSchemeData(SchemeRelativeUrl { ref mut path, .. }) => {
                 let (new_path, _) = try!(parser::parse_path_start(
@@ -201,7 +275,7 @@ impl Url {
     }
 
     /// `URLUtils.search` setter
-    pub fn set_query(&mut self, input: &str) -> ParseResult<()> {
+    fn set_query(&mut self, input: &str) -> ParseResult<()> {
         // FIXME: This is in the spec, but seems superfluous.
         match self.scheme_data {
             RelativeSchemeData(_) => (),
@@ -220,7 +294,7 @@ impl Url {
     }
 
     /// `URLUtils.hash` setter
-    pub fn set_fragment(&mut self, input: &str) -> ParseResult<()> {
+    fn set_fragment(&mut self, input: &str) -> ParseResult<()> {
         if self.scheme.as_slice() == "javascript" {
             return Err("Can not set fragment on a javascript: URL.")
         }
@@ -232,63 +306,6 @@ impl Url {
         };
         Ok(())
     }
-
-    pub fn serialize(&self) -> String {
-        let mut result = self.serialize_no_fragment();
-        match self.fragment {
-            None => (),
-            Some(ref fragment) => {
-                result.push_str("#");
-                result.push_str(fragment.as_slice());
-            }
-        }
-        result
-    }
-
-    pub fn serialize_no_fragment(&self) -> String {
-        let mut result = self.scheme.clone();
-        result.push_str(":");
-        match self.scheme_data {
-            RelativeSchemeData(SchemeRelativeUrl {
-                ref username, ref password, ref host, ref port, ref path
-            }) => {
-                result.push_str("//");
-                if !username.is_empty() || password.is_some() {
-                    result.push_str(username.as_slice());
-                    match password {
-                        &None => (),
-                        &Some(ref password) => {
-                            result.push_str(":");
-                            result.push_str(password.as_slice());
-                        }
-                    }
-                    result.push_str("@");
-                }
-                result.push_str(host.serialize().as_slice());
-                if port.len() > 0 {
-                    result.push_str(":");
-                    result.push_str(port.as_slice());
-                }
-                if path.len() > 0 {
-                    for path_part in path.iter() {
-                        result.push_str("/");
-                        result.push_str(path_part.as_slice());
-                    }
-                } else {
-                    result.push_str("/");
-                }
-            },
-            OtherSchemeData(ref data) => result.push_str(data.as_slice()),
-        }
-        match self.query {
-            None => (),
-            Some(ref query) => {
-                result.push_str("?");
-                result.push_str(query.as_slice());
-            }
-        }
-        result
-    }
 }