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

Add _mut version of accessors. Fix #7.

Simon Sapin 12 лет назад
Родитель
Сommit
d96d1ac3b0
1 измененных файлов с 65 добавлено и 0 удалено
  1. 65 0
      src/lib.rs

+ 65 - 0
src/lib.rs

@@ -594,6 +594,15 @@ impl Url {
         }
         }
     }
     }
 
 
+    /// If the URL is *non-relative*, return a mutable reference to the string scheme data.
+    #[inline]
+    pub fn non_relative_scheme_data_mut<'a>(&'a mut self) -> Option<&'a mut String> {
+        match self.scheme_data {
+            RelativeSchemeData(..) => None,
+            NonRelativeSchemeData(ref mut scheme_data) => Some(scheme_data),
+        }
+    }
+
     /// If the URL is in a *relative scheme*, return the structured scheme data.
     /// If the URL is in a *relative scheme*, return the structured scheme data.
     #[inline]
     #[inline]
     pub fn relative_scheme_data<'a>(&'a self) -> Option<&'a RelativeSchemeData> {
     pub fn relative_scheme_data<'a>(&'a self) -> Option<&'a RelativeSchemeData> {
@@ -603,12 +612,28 @@ impl Url {
         }
         }
     }
     }
 
 
+    /// If the URL is in a *relative scheme*,
+    /// return a mutable reference to the structured scheme data.
+    #[inline]
+    pub fn relative_scheme_data_mut<'a>(&'a mut self) -> Option<&'a mut RelativeSchemeData> {
+        match self.scheme_data {
+            RelativeSchemeData(ref mut scheme_data) => Some(scheme_data),
+            NonRelativeSchemeData(..) => None,
+        }
+    }
+
     /// If the URL is in a *relative scheme*, return its username.
     /// If the URL is in a *relative scheme*, return its username.
     #[inline]
     #[inline]
     pub fn username<'a>(&'a self) -> Option<&'a str> {
     pub fn username<'a>(&'a self) -> Option<&'a str> {
         self.relative_scheme_data().map(|scheme_data| scheme_data.username.as_slice())
         self.relative_scheme_data().map(|scheme_data| scheme_data.username.as_slice())
     }
     }
 
 
+    /// If the URL is in a *relative scheme*, return a mutable reference to its username.
+    #[inline]
+    pub fn username_mut<'a>(&'a mut self) -> Option<&'a mut String> {
+        self.relative_scheme_data_mut().map(|scheme_data| &mut scheme_data.username)
+    }
+
     /// Percent-decode the URL’s username, if any.
     /// Percent-decode the URL’s username, if any.
     ///
     ///
     /// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
     /// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
@@ -625,6 +650,12 @@ impl Url {
             scheme_data.password.as_ref().map(|password| password.as_slice()))
             scheme_data.password.as_ref().map(|password| password.as_slice()))
     }
     }
 
 
+    /// If the URL is in a *relative scheme*, return a mutable reference to its password, if any.
+    #[inline]
+    pub fn password_mut<'a>(&'a mut self) -> Option<&'a mut String> {
+        self.relative_scheme_data_mut().and_then(|scheme_data| scheme_data.password.as_mut())
+    }
+
     /// Percent-decode the URL’s password, if any.
     /// Percent-decode the URL’s password, if any.
     ///
     ///
     /// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
     /// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
@@ -641,6 +672,12 @@ impl Url {
         self.relative_scheme_data().map(|scheme_data| &scheme_data.host)
         self.relative_scheme_data().map(|scheme_data| &scheme_data.host)
     }
     }
 
 
+    /// If the URL is in a *relative scheme*, return a mutable reference to its structured host.
+    #[inline]
+    pub fn host_mut<'a>(&'a mut self) -> Option<&'a mut Host> {
+        self.relative_scheme_data_mut().map(|scheme_data| &mut scheme_data.host)
+    }
+
     /// If the URL is in a *relative scheme* and its host is a domain,
     /// If the URL is in a *relative scheme* and its host is a domain,
     /// return the domain as a string.
     /// return the domain as a string.
     #[inline]
     #[inline]
@@ -648,6 +685,13 @@ impl Url {
         self.relative_scheme_data().and_then(|scheme_data| scheme_data.domain())
         self.relative_scheme_data().and_then(|scheme_data| scheme_data.domain())
     }
     }
 
 
+    /// If the URL is in a *relative scheme* and its host is a domain,
+    /// return a mutable reference to the domain string.
+    #[inline]
+    pub fn domain_mut<'a>(&'a mut self) -> Option<&'a mut String> {
+        self.relative_scheme_data_mut().and_then(|scheme_data| scheme_data.domain_mut())
+    }
+
     /// If the URL is in a *relative scheme*, serialize its host as a string.
     /// If the URL is in a *relative scheme*, serialize its host as a string.
     ///
     ///
     /// A domain a returned as-is, an IPv6 address between [] square brackets.
     /// A domain a returned as-is, an IPv6 address between [] square brackets.
@@ -662,12 +706,24 @@ impl Url {
         self.relative_scheme_data().map(|scheme_data| scheme_data.port.as_slice())
         self.relative_scheme_data().map(|scheme_data| scheme_data.port.as_slice())
     }
     }
 
 
+    /// If the URL is in a *relative scheme*, return a mutable reference to its port.
+    #[inline]
+    pub fn port_mut<'a>(&'a mut self) -> Option<&'a mut String> {
+        self.relative_scheme_data_mut().map(|scheme_data| &mut scheme_data.port)
+    }
+
     /// If the URL is in a *relative scheme*, return its path components.
     /// If the URL is in a *relative scheme*, return its path components.
     #[inline]
     #[inline]
     pub fn path<'a>(&'a self) -> Option<&'a [String]> {
     pub fn path<'a>(&'a self) -> Option<&'a [String]> {
         self.relative_scheme_data().map(|scheme_data| scheme_data.path.as_slice())
         self.relative_scheme_data().map(|scheme_data| scheme_data.path.as_slice())
     }
     }
 
 
+    /// If the URL is in a *relative scheme*, return a mutable reference to its path components.
+    #[inline]
+    pub fn path_mut<'a>(&'a mut self) -> Option<&'a mut Vec<String>> {
+        self.relative_scheme_data_mut().map(|scheme_data| &mut scheme_data.path)
+    }
+
     /// If the URL is in a *relative scheme*, serialize its path as a string.
     /// If the URL is in a *relative scheme*, serialize its path as a string.
     ///
     ///
     /// The returned string starts with a "/" slash, and components are separated by slashes.
     /// The returned string starts with a "/" slash, and components are separated by slashes.
@@ -813,6 +869,15 @@ impl RelativeSchemeData {
         }
         }
     }
     }
 
 
+    /// If the host is a domain, return a mutable reference to the domain string.
+    #[inline]
+    pub fn domain_mut<'a>(&'a mut self) -> Option<&'a mut String> {
+        match self.host {
+            Domain(ref mut domain) => Some(domain),
+            _ => None,
+        }
+    }
+
     /// Serialize the path as a string.
     /// Serialize the path as a string.
     ///
     ///
     /// The returned string starts with a "/" slash, and components are separated by slashes.
     /// The returned string starts with a "/" slash, and components are separated by slashes.