Skip to content

Commit 762ee3e

Browse files
authored
feat: add getter for scheme type (#52)
* feat: add getter for schema type * fixup: code review * fixup: rename to scheme * fixup: code review
1 parent 1ca681c commit 762ee3e

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/ffi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ extern "C" {
8383
pub fn ada_get_search(url: *mut ada_url) -> ada_string;
8484
pub fn ada_get_protocol(url: *mut ada_url) -> ada_string;
8585
pub fn ada_get_host_type(url: *mut ada_url) -> c_uint;
86+
pub fn ada_get_scheme_type(url: *mut ada_url) -> c_uint;
8687

8788
// Setters
8889
pub fn ada_set_href(url: *mut ada_url, input: *const c_char, length: usize) -> bool;

src/lib.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,33 @@ impl From<c_uint> for HostType {
7878
}
7979
}
8080

81+
/// Defines the scheme type of the url.
82+
#[derive(Debug, Clone, PartialEq, Eq)]
83+
pub enum SchemeType {
84+
Http = 0,
85+
NotSpecial = 1,
86+
Https = 2,
87+
Ws = 3,
88+
Ftp = 4,
89+
Wss = 5,
90+
File = 6,
91+
}
92+
93+
impl From<c_uint> for SchemeType {
94+
fn from(value: c_uint) -> Self {
95+
match value {
96+
0 => SchemeType::Http,
97+
1 => SchemeType::NotSpecial,
98+
2 => SchemeType::Https,
99+
3 => SchemeType::Ws,
100+
4 => SchemeType::Ftp,
101+
5 => SchemeType::Wss,
102+
6 => SchemeType::File,
103+
_ => SchemeType::NotSpecial,
104+
}
105+
}
106+
}
107+
81108
/// Components are a serialization-free representation of a URL.
82109
/// For usages where string serialization has a high cost, you can
83110
/// use url components with `href` attribute.
@@ -224,6 +251,11 @@ impl Url {
224251
HostType::from(unsafe { ffi::ada_get_host_type(self.0) })
225252
}
226253

254+
/// Returns the type of the scheme such as http, https, etc.
255+
pub fn scheme_type(&self) -> SchemeType {
256+
SchemeType::from(unsafe { ffi::ada_get_scheme_type(self.0) })
257+
}
258+
227259
/// Return the origin of this URL
228260
///
229261
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-origin)
@@ -907,6 +939,8 @@ mod test {
907939
"https://username:[email protected]:9090/search?query#hash"
908940
);
909941

942+
assert_eq!(out.scheme_type(), SchemeType::Https);
943+
910944
out.set_username(Some("new-username")).unwrap();
911945
assert_eq!(out.username(), "new-username");
912946

@@ -937,6 +971,7 @@ mod test {
937971

938972
out.set_protocol("wss").unwrap();
939973
assert_eq!(out.protocol(), "wss:");
974+
assert_eq!(out.scheme_type(), SchemeType::Wss);
940975

941976
assert!(out.has_credentials());
942977
assert!(out.has_non_empty_username());
@@ -948,6 +983,52 @@ mod test {
948983
assert_eq!(out.host_type(), HostType::Domain);
949984
}
950985

986+
#[test]
987+
fn scheme_types() {
988+
assert_eq!(
989+
Url::parse("file:///foo/bar", None)
990+
.expect("bad url")
991+
.scheme_type(),
992+
SchemeType::File
993+
);
994+
assert_eq!(
995+
Url::parse("ws://example.com/ws", None)
996+
.expect("bad url")
997+
.scheme_type(),
998+
SchemeType::Ws
999+
);
1000+
assert_eq!(
1001+
Url::parse("wss://example.com/wss", None)
1002+
.expect("bad url")
1003+
.scheme_type(),
1004+
SchemeType::Wss
1005+
);
1006+
assert_eq!(
1007+
Url::parse("ftp://example.com/file.txt", None)
1008+
.expect("bad url")
1009+
.scheme_type(),
1010+
SchemeType::Ftp
1011+
);
1012+
assert_eq!(
1013+
Url::parse("http://example.com/file.txt", None)
1014+
.expect("bad url")
1015+
.scheme_type(),
1016+
SchemeType::Http
1017+
);
1018+
assert_eq!(
1019+
Url::parse("https://example.com/file.txt", None)
1020+
.expect("bad url")
1021+
.scheme_type(),
1022+
SchemeType::Https
1023+
);
1024+
assert_eq!(
1025+
Url::parse("foo://example.com", None)
1026+
.expect("bad url")
1027+
.scheme_type(),
1028+
SchemeType::NotSpecial
1029+
);
1030+
}
1031+
9511032
#[test]
9521033
fn can_parse_simple_url() {
9531034
assert!(Url::can_parse("https://google.com", None));

0 commit comments

Comments
 (0)