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

Improve error handling ergonomics (and docs around it) #62

Merged
merged 9 commits into from
Mar 21, 2024
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
33 changes: 11 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
## WHATWG URL parser for Rust
# WHATWG URL parser for Rust

Fast [WHATWG URL Specification](https://url.spec.whatwg.org) compliant URL parser for Rust.
Well-tested and widely used by Node.js since [Node 18](https://nodejs.org/en/blog/release/v18.17.0).

The Ada library passes the full range of tests from the specification, across a wide range of platforms (e.g., Windows, Linux, macOS).
It fully supports the relevant [Unicode Technical Standard](https://www.unicode.org/reports/tr46/#ToUnicode).

### Usage

Here is an example illustrating a common usage:

```Rust
use ada_url::Url;
fn main() {
let u = Url::parse("http://www.google:8080/love#drug", None).expect("bad url");
println!("port: {:?}", u.port());
println!("hash: {:?}", u.hash());
println!("pathname: {:?}", u.pathname());
println!("href: {:?}", u.href());
u.set_port("9999");
println!("href: {:?}", u.href());
}
```
## Usage

See [here](examples/simple.rs) for a usage example.
You can run it locally with `cargo run --example simple`.
Feel free to adjust it for exploring this crate further.

#### Features
### Features

**std:** Functionalities that require `std`.
This feature is enabled by default, set `no-default-features` to `true` if you want `no-std`.
Expand All @@ -38,7 +27,7 @@ Enabling this feature without `libc++` installed would cause compile error.

Ada is fast. The benchmark below shows **3.34 times** faster URL parsing compared to `url`

```
```text
parse/ada_url time: [2.0790 µs 2.0812 µs 2.0835 µs]
thrpt: [369.84 MiB/s 370.25 MiB/s 370.65 MiB/s]

Expand All @@ -65,9 +54,9 @@ parse/url time: [6.9266 µs 6.9677 µs 7.0199 µs]
| **[`Send`](https://doc.rust-lang.org/std/marker/trait.Send.html)** | Used to declare that the type can be transferred across thread boundaries. |
| **[`Sync`](https://doc.rust-lang.org/stable/std/marker/trait.Sync.html)** | Used to declare that the type is thread-safe. |

### Development
## Development

#### `justfile`
### `justfile`

The [`justfile`](./justfile) contains commands (called "recipes") that can be executed by [just](https://github.com/casey/just) for convenience.

Expand All @@ -83,7 +72,7 @@ just all
just all --skip=libcpp,serde
```

### License
## License

This code is made available under the Apache License 2.0 as well as the MIT license.

Expand Down
14 changes: 14 additions & 0 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use ada_url::Url;

fn main() {
let url = Url::parse("http://www.google:8080/love#drug", None).expect("bad url");

println!("port: {:?}", url.port());
println!("hash: {:?}", url.hash());
println!("pathname: {:?}", url.pathname());
println!("href: {:?}", url.href());

let mut url = url;
url.set_port(Some("9999")).expect("bad port");
println!("href: {:?}", url.href());
}
3 changes: 2 additions & 1 deletion src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ pub struct ada_string {
}

impl ada_string {
pub fn as_str(&self) -> &'static str {
#[must_use]
pub const fn as_str(&self) -> &'static str {
unsafe {
let slice = core::slice::from_raw_parts(self.data.cast(), self.length);
core::str::from_utf8_unchecked(slice)
Expand Down
4 changes: 3 additions & 1 deletion src/idna.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ffi;

/// IDNA struct implements the to_ascii and to_unicode functions from the Unicode Technical
/// IDNA struct implements the `to_ascii` and `to_unicode` functions from the Unicode Technical
/// Standard supporting a wide range of systems. It is suitable for URL parsing.
/// For more information, [read the specification](https://www.unicode.org/reports/tr46/#ToUnicode)
pub struct Idna {}
Expand All @@ -15,6 +15,7 @@ impl Idna {
/// use ada_url::Idna;
/// assert_eq!(Idna::unicode("xn--meagefactory-m9a.ca"), "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());
Expand All @@ -32,6 +33,7 @@ impl Idna {
/// use ada_url::Idna;
/// assert_eq!(Idna::ascii("meßagefactory.ca"), "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());
Expand Down
Loading
Loading