Skip to content

Commit c6299f9

Browse files
authored
Url is special (servo#826)
* Make SchemeType::from into From trait * Expose whether URL is special as Url::is_special
1 parent a4633be commit c6299f9

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

url/src/lib.rs

+21
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,27 @@ impl Url {
819819
self.slice(..self.scheme_end)
820820
}
821821

822+
/// Return whether the URL is special (has a special scheme)
823+
///
824+
/// # Examples
825+
///
826+
/// ```
827+
/// use url::Url;
828+
/// # use url::ParseError;
829+
///
830+
/// # fn run() -> Result<(), ParseError> {
831+
/// assert!(Url::parse("http:///tmp/foo")?.is_special());
832+
/// assert!(Url::parse("file:///tmp/foo")?.is_special());
833+
/// assert!(!Url::parse("moz:///tmp/foo")?.is_special());
834+
/// # Ok(())
835+
/// # }
836+
/// # run().unwrap();
837+
/// ```
838+
pub fn is_special(&self) -> bool {
839+
let scheme_type = SchemeType::from(self.scheme());
840+
scheme_type.is_special()
841+
}
842+
822843
/// Return whether the URL has an 'authority',
823844
/// which can contain a username, password, host, and port number.
824845
///

url/src/parser.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,11 @@ impl SchemeType {
157157
pub fn is_file(&self) -> bool {
158158
matches!(*self, SchemeType::File)
159159
}
160+
}
160161

161-
pub fn from(s: &str) -> Self {
162-
match s {
162+
impl<T: AsRef<str>> From<T> for SchemeType {
163+
fn from(s: T) -> Self {
164+
match s.as_ref() {
163165
"http" | "https" | "ws" | "wss" | "ftp" => SchemeType::SpecialNotFile,
164166
"file" => SchemeType::File,
165167
_ => SchemeType::NotSpecial,

0 commit comments

Comments
 (0)