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

In functions using ada_owned_string, return ada_owned_string instead of &str #67

Merged
merged 4 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ impl AsRef<str> for ada_owned_string {
}
}

impl Drop for ada_owned_string {
fn drop(&mut self) {
// @note This is needed because ada_free_owned_string accepts by value
let copy = ada_owned_string {
data: self.data,
length: self.length,
};
unsafe {
ada_free_owned_string(copy);
};
}
}

#[repr(C)]
pub struct ada_url_components {
pub protocol_end: u32,
Expand Down
30 changes: 14 additions & 16 deletions src/idna.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ impl Idna {
///
/// ```
/// use ada_url::Idna;
/// assert_eq!(Idna::unicode("xn--meagefactory-m9a.ca"), "meßagefactory.ca");
/// assert_eq!(Idna::unicode("xn--meagefactory-m9a.ca").as_ref(), "meßagefactory.ca");
/// ```
#[must_use]
pub fn unicode(input: &str) -> &str {
unsafe {
let out = ffi::ada_idna_to_unicode(input.as_ptr().cast(), input.len());
let slice = core::slice::from_raw_parts(out.data.cast(), out.length);
core::str::from_utf8_unchecked(slice)
}
pub fn unicode(input: &str) -> ffi::ada_owned_string {
pratikpc marked this conversation as resolved.
Show resolved Hide resolved
unsafe { ffi::ada_idna_to_unicode(input.as_ptr().cast(), input.len()) }
}

/// Process international domains according to the UTS #46 standard.
Expand All @@ -31,15 +27,11 @@ impl Idna {
///
/// ```
/// use ada_url::Idna;
/// assert_eq!(Idna::ascii("meßagefactory.ca"), "xn--meagefactory-m9a.ca");
/// assert_eq!(Idna::ascii("meßagefactory.ca").as_ref(), "xn--meagefactory-m9a.ca");
/// ```
#[must_use]
pub fn ascii(input: &str) -> &str {
unsafe {
let out = ffi::ada_idna_to_ascii(input.as_ptr().cast(), input.len());
let slice = core::slice::from_raw_parts(out.data.cast(), out.length);
core::str::from_utf8_unchecked(slice)
}
pub fn ascii(input: &str) -> ffi::ada_owned_string {
pratikpc marked this conversation as resolved.
Show resolved Hide resolved
unsafe { ffi::ada_idna_to_ascii(input.as_ptr().cast(), input.len()) }
}
}

Expand All @@ -49,11 +41,17 @@ mod tests {

#[test]
fn unicode_should_work() {
assert_eq!(Idna::unicode("xn--meagefactory-m9a.ca"), "meßagefactory.ca");
assert_eq!(
Idna::unicode("xn--meagefactory-m9a.ca").as_ref(),
"meßagefactory.ca"
);
}

#[test]
fn ascii_should_work() {
assert_eq!(Idna::ascii("meßagefactory.ca"), "xn--meagefactory-m9a.ca");
assert_eq!(
Idna::ascii("meßagefactory.ca").as_ref(),
"xn--meagefactory-m9a.ca"
);
}
}
12 changes: 4 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,11 @@ impl Url {
/// use ada_url::Url;
///
/// let url = Url::parse("blob:https://example.com/foo", None).expect("Invalid URL");
/// assert_eq!(url.origin(), "https://example.com");
/// assert_eq!(url.origin().as_ref(), "https://example.com");
/// ```
#[must_use]
pub fn origin(&self) -> &str {
unsafe {
let out = ffi::ada_get_origin(self.0);
let slice = core::slice::from_raw_parts(out.data.cast(), out.length);
core::str::from_utf8_unchecked(slice)
}
pub fn origin(&self) -> ffi::ada_owned_string {
unsafe { ffi::ada_get_origin(self.0) }
}

/// Return the parsed version of the URL with all components.
Expand Down Expand Up @@ -949,7 +945,7 @@ mod test {
None,
)
.expect("Should have parsed a simple url");
assert_eq!(out.origin(), "https://google.com:9090");
assert_eq!(out.origin().as_ref(), "https://google.com:9090");
assert_eq!(
out.href(),
"https://username:[email protected]:9090/search?query#hash"
Expand Down
Loading