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

Add url search params #57

Merged
merged 13 commits into from
Dec 3, 2024
134 changes: 134 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ pub struct ada_url {
_marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}

#[repr(C)]
pub struct ada_url_search_params {
_unused: [u8; 0],
_marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}

#[repr(C)]
pub struct ada_string {
pub data: *const c_char,
Expand Down Expand Up @@ -64,6 +70,38 @@ impl Drop for ada_owned_string {
}
}

/// Represents an std::vector<std::string>
anonrig marked this conversation as resolved.
Show resolved Hide resolved
#[repr(C)]
pub struct ada_strings {
_unused: [u8; 0],
_marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}

#[repr(C)]
pub struct ada_url_search_params_keys_iter {
_unused: [u8; 0],
_marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}

#[repr(C)]
pub struct ada_url_search_params_values_iter {
_unused: [u8; 0],
_marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}

#[repr(C)]
pub struct ada_url_search_params_entries_iter {
_unused: [u8; 0],
_marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}

/// Represents a key/value pair of strings
#[repr(C)]
pub struct ada_string_pair {
pub key: ada_string,
pub value: ada_string,
}

#[repr(C)]
pub struct ada_url_components {
pub protocol_end: u32,
Expand Down Expand Up @@ -143,6 +181,102 @@ extern "C" {
// IDNA methods
pub fn ada_idna_to_unicode(input: *const c_char, length: usize) -> ada_owned_string;
pub fn ada_idna_to_ascii(input: *const c_char, length: usize) -> ada_owned_string;

// URLSearchParams
pub fn ada_parse_search_params(
input: *const c_char,
length: usize,
) -> *mut ada_url_search_params;
pub fn ada_free_search_params(search_params: *mut ada_url_search_params);
pub fn ada_search_params_size(search_params: *mut ada_url_search_params) -> usize;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, on the -> usize bit here, this isn't wrong but it is something that's like, being debated. That is, if usize is size_t or not. Today, it's often used that way, but this can cause issues on platforms like CHERI. However, there's tons of code written like this, so anything that happens will have to deal with all of that... so I'm not saying you shouldn't do this, just wanted to clue you in on that as a general thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember reading about this. Nice catch. What's the "best" solution to use for usize?

pub fn ada_search_params_sort(search_params: *mut ada_url_search_params);
pub fn ada_search_params_to_string(
search_params: *mut ada_url_search_params,
) -> ada_owned_string;
pub fn ada_search_params_append(
search_params: *mut ada_url_search_params,
name: *const c_char,
name_length: usize,
value: *const c_char,
value_length: usize,
);
pub fn ada_search_params_set(
search_params: *mut ada_url_search_params,
name: *const c_char,
name_length: usize,
value: *const c_char,
value_length: usize,
);
pub fn ada_search_params_remove(
search_params: *mut ada_url_search_params,
name: *const c_char,
name_length: usize,
);
pub fn ada_search_params_remove_value(
search_params: *mut ada_url_search_params,
name: *const c_char,
name_length: usize,
value: *const c_char,
value_length: usize,
);
pub fn ada_search_params_has(
search_params: *mut ada_url_search_params,
name: *const c_char,
name_length: usize,
) -> bool;
pub fn ada_search_params_has_value(
search_params: *mut ada_url_search_params,
name: *const c_char,
name_length: usize,
value: *const c_char,
value_length: usize,
) -> bool;
pub fn ada_search_params_get(
search_params: *mut ada_url_search_params,
key: *const c_char,
key_length: usize,
) -> ada_string;
pub fn ada_search_params_get_all(
// not implemented
search_params: *mut ada_url_search_params,
key: *const c_char,
key_length: usize,
) -> *mut ada_strings;
pub fn ada_search_params_get_keys(
search_params: *mut ada_url_search_params,
) -> *mut ada_url_search_params_keys_iter;
pub fn ada_search_params_get_values(
search_params: *mut ada_url_search_params,
) -> *mut ada_url_search_params_values_iter;
pub fn ada_search_params_get_entries(
search_params: *mut ada_url_search_params,
) -> *mut ada_url_search_params_entries_iter;

pub fn ada_free_strings(strings: *mut ada_strings);
pub fn ada_strings_size(strings: *mut ada_strings) -> usize;
pub fn ada_strings_get(strings: *mut ada_strings, index: usize) -> ada_string;
pub fn ada_free_search_params_keys_iter(iter: *mut ada_url_search_params_keys_iter);
pub fn ada_search_params_keys_iter_next(
iter: *mut ada_url_search_params_keys_iter,
) -> ada_string;
pub fn ada_search_params_keys_iter_has_next(iter: *mut ada_url_search_params_keys_iter)
-> bool;

pub fn ada_free_search_params_values_iter(iter: *mut ada_url_search_params_values_iter);
pub fn ada_search_params_values_iter_next(
iter: *mut ada_url_search_params_values_iter,
) -> ada_string;
pub fn ada_search_params_values_iter_has_next(
iter: *mut ada_url_search_params_values_iter,
) -> bool;

pub fn ada_free_search_params_entries_iter(iter: *mut ada_url_search_params_entries_iter);
pub fn ada_search_params_entries_iter_next(
iter: *mut ada_url_search_params_entries_iter,
) -> ada_string_pair;
pub fn ada_search_params_entries_iter_has_next(
iter: *mut ada_url_search_params_entries_iter,
) -> bool;
}

#[cfg(test)]
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@

pub mod ffi;
mod idna;
mod url_search_params;
pub use idna::Idna;
pub use url_search_params::{
UrlSearchParams, UrlSearchParamsEntry, UrlSearchParamsEntryIterator,
UrlSearchParamsKeyIterator, UrlSearchParamsValueIterator,
};

#[cfg(feature = "std")]
extern crate std;
Expand Down
Loading
Loading