Skip to content

Commit e55c444

Browse files
committed
feat: add host_type() method for url
1 parent 182d30f commit e55c444

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/ffi.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(non_camel_case_types)]
2-
use std::ffi::c_char;
2+
use std::ffi::{c_char, c_uint};
33

44
#[repr(C)]
55
pub struct ada_url {
@@ -81,6 +81,7 @@ extern "C" {
8181
pub fn ada_get_pathname(url: *mut ada_url) -> ada_string;
8282
pub fn ada_get_search(url: *mut ada_url) -> ada_string;
8383
pub fn ada_get_protocol(url: *mut ada_url) -> ada_string;
84+
pub fn ada_get_url_host_type(url: *mut ada_url) -> c_uint;
8485

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

src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub mod ffi;
3535
mod idna;
3636
pub use idna::Idna;
3737

38+
use std::os::raw::c_uint;
3839
use std::{borrow, fmt, hash, ops};
3940
use thiserror::Error;
4041

@@ -47,6 +48,25 @@ pub enum Error {
4748
ParseUrl(String),
4849
}
4950

51+
/// Defines the type of the host.
52+
#[derive(Debug, Clone, PartialEq, Eq)]
53+
pub enum HostType {
54+
Domain = 0,
55+
IPV4 = 1,
56+
IPV6 = 2,
57+
}
58+
59+
impl From<c_uint> for HostType {
60+
fn from(value: c_uint) -> Self {
61+
match value {
62+
0 => HostType::Domain,
63+
1 => HostType::IPV4,
64+
2 => HostType::IPV6,
65+
_ => HostType::Domain,
66+
}
67+
}
68+
}
69+
5070
/// A parsed URL struct according to WHATWG URL specification.
5171
#[derive(Eq, Clone)]
5272
pub struct Url {
@@ -114,6 +134,11 @@ impl Url {
114134
}
115135
}
116136

137+
/// Returns the type of the host such as default, ipv4 or ipv6.
138+
pub fn host_type(&self) -> HostType {
139+
HostType::from(unsafe { ffi::ada_get_url_host_type(self.url) })
140+
}
141+
117142
/// Return the origin of this URL
118143
///
119144
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-origin)
@@ -699,6 +724,8 @@ mod test {
699724
assert!(out.has_search());
700725
assert!(out.has_hash());
701726
assert!(out.has_password());
727+
728+
assert_eq!(out.host_type(), HostType::Domain);
702729
}
703730

704731
#[test]

0 commit comments

Comments
 (0)