Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Url is special #826

Merged
merged 2 commits into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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