Skip to content

feat: add getter for scheme type #52

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

Merged
merged 4 commits into from
Sep 5, 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
1 change: 1 addition & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ extern "C" {
pub fn ada_get_search(url: *mut ada_url) -> ada_string;
pub fn ada_get_protocol(url: *mut ada_url) -> ada_string;
pub fn ada_get_host_type(url: *mut ada_url) -> c_uint;
pub fn ada_get_scheme_type(url: *mut ada_url) -> c_uint;

// Setters
pub fn ada_set_href(url: *mut ada_url, input: *const c_char, length: usize) -> bool;
Expand Down
81 changes: 81 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,33 @@ impl From<c_uint> for HostType {
}
}

/// Defines the scheme type of the url.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SchemeType {
Http = 0,
NotSpecial = 1,
Https = 2,
Ws = 3,
Ftp = 4,
Wss = 5,
File = 6,
}

impl From<c_uint> for SchemeType {
fn from(value: c_uint) -> Self {
match value {
0 => SchemeType::Http,
1 => SchemeType::NotSpecial,
2 => SchemeType::Https,
3 => SchemeType::Ws,
4 => SchemeType::Ftp,
5 => SchemeType::Wss,
6 => SchemeType::File,
_ => SchemeType::NotSpecial,
}
}
}

/// Components are a serialization-free representation of a URL.
/// For usages where string serialization has a high cost, you can
/// use url components with `href` attribute.
Expand Down Expand Up @@ -224,6 +251,11 @@ impl Url {
HostType::from(unsafe { ffi::ada_get_host_type(self.0) })
}

/// Returns the type of the scheme such as http, https, etc.
pub fn scheme_type(&self) -> SchemeType {
SchemeType::from(unsafe { ffi::ada_get_scheme_type(self.0) })
}

/// Return the origin of this URL
///
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-origin)
Expand Down Expand Up @@ -907,6 +939,8 @@ mod test {
"https://username:[email protected]:9090/search?query#hash"
);

assert_eq!(out.scheme_type(), SchemeType::Https);

out.set_username(Some("new-username")).unwrap();
assert_eq!(out.username(), "new-username");

Expand Down Expand Up @@ -937,6 +971,7 @@ mod test {

out.set_protocol("wss").unwrap();
assert_eq!(out.protocol(), "wss:");
assert_eq!(out.scheme_type(), SchemeType::Wss);

assert!(out.has_credentials());
assert!(out.has_non_empty_username());
Expand All @@ -948,6 +983,52 @@ mod test {
assert_eq!(out.host_type(), HostType::Domain);
}

#[test]
fn scheme_types() {
assert_eq!(
Url::parse("file:///foo/bar", None)
.expect("bad url")
.scheme_type(),
SchemeType::File
);
assert_eq!(
Url::parse("ws://example.com/ws", None)
.expect("bad url")
.scheme_type(),
SchemeType::Ws
);
assert_eq!(
Url::parse("wss://example.com/wss", None)
.expect("bad url")
.scheme_type(),
SchemeType::Wss
);
assert_eq!(
Url::parse("ftp://example.com/file.txt", None)
.expect("bad url")
.scheme_type(),
SchemeType::Ftp
);
assert_eq!(
Url::parse("http://example.com/file.txt", None)
.expect("bad url")
.scheme_type(),
SchemeType::Http
);
assert_eq!(
Url::parse("https://example.com/file.txt", None)
.expect("bad url")
.scheme_type(),
SchemeType::Https
);
assert_eq!(
Url::parse("foo://example.com", None)
.expect("bad url")
.scheme_type(),
SchemeType::NotSpecial
);
}

#[test]
fn can_parse_simple_url() {
assert!(Url::can_parse("https://google.com", None));
Expand Down