Explorar o código

Url is special (#826)

* Make SchemeType::from into From trait

* Expose whether URL is special as Url::is_special
Quentin Santos %!s(int64=3) %!d(string=hai) anos
pai
achega
c6299f92d8
Modificáronse 2 ficheiros con 25 adicións e 2 borrados
  1. 21 0
      url/src/lib.rs
  2. 4 2
      url/src/parser.rs

+ 21 - 0
url/src/lib.rs

@@ -819,6 +819,27 @@ impl Url {
         self.slice(..self.scheme_end)
     }
 
+    /// Return whether the URL is special (has a special scheme)
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use url::Url;
+    /// # use url::ParseError;
+    ///
+    /// # fn run() -> Result<(), ParseError> {
+    /// assert!(Url::parse("http:///tmp/foo")?.is_special());
+    /// assert!(Url::parse("file:///tmp/foo")?.is_special());
+    /// assert!(!Url::parse("moz:///tmp/foo")?.is_special());
+    /// # Ok(())
+    /// # }
+    /// # run().unwrap();
+    /// ```
+    pub fn is_special(&self) -> bool {
+        let scheme_type = SchemeType::from(self.scheme());
+        scheme_type.is_special()
+    }
+
     /// Return whether the URL has an 'authority',
     /// which can contain a username, password, host, and port number.
     ///

+ 4 - 2
url/src/parser.rs

@@ -157,9 +157,11 @@ impl SchemeType {
     pub fn is_file(&self) -> bool {
         matches!(*self, SchemeType::File)
     }
+}
 
-    pub fn from(s: &str) -> Self {
-        match s {
+impl<T: AsRef<str>> From<T> for SchemeType {
+    fn from(s: T) -> Self {
+        match s.as_ref() {
             "http" | "https" | "ws" | "wss" | "ftp" => SchemeType::SpecialNotFile,
             "file" => SchemeType::File,
             _ => SchemeType::NotSpecial,