Skip to content

Commit

Permalink
Url is special (#826)
Browse files Browse the repository at this point in the history
* Make SchemeType::from into From trait

* Expose whether URL is special as Url::is_special
  • Loading branch information
qsantos committed Mar 18, 2023
1 parent a4633be commit c6299f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
21 changes: 21 additions & 0 deletions url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
6 changes: 4 additions & 2 deletions url/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c6299f9

Please sign in to comment.