diff --git a/url/src/lib.rs b/url/src/lib.rs index 65e965e45..023a89a28 100644 --- a/url/src/lib.rs +++ b/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. /// diff --git a/url/src/parser.rs b/url/src/parser.rs index 0553caa81..c32090e20 100644 --- a/url/src/parser.rs +++ b/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> From for SchemeType { + fn from(s: T) -> Self { + match s.as_ref() { "http" | "https" | "ws" | "wss" | "ftp" => SchemeType::SpecialNotFile, "file" => SchemeType::File, _ => SchemeType::NotSpecial,