Crate faup_rs

Crate faup_rs 

Source
Expand description

§faup-rs: Fast URL Parser for Rust

A high-performance, zero-allocation URL parser for Rust that handles:

  • Hostnames (with subdomains, custom TLDs, and IDNs)
  • IPv4/IPv6 addresses
  • User credentials (username/password)
  • Ports, paths, queries, and fragments
  • UTF-8 and URL-encoded characters

§Features

Zero-allocation parsing: Borrows input strings where possible

Public Suffix List (PSL): Correctly identifies domain suffixes

Custom TLDs: Extendable via the CUSTOM_TLDS constant

Comprehensive error handling: Clear, actionable error types

UTF-8 support: Full Unicode handling for all URL components

§Installation

Add to your Cargo.toml:

[dependencies]
faup-rs = "0.1"

§Usage

§Basic Parsing

use faup_rs::Url;

let url = Url::parse("https://user:pass@sub.example.com:8080/path?query=value#fragment").unwrap();
assert_eq!(url.scheme(), "https");
assert_eq!(url.host().to_string(), "sub.example.com");
assert_eq!(url.port(), Some(8080));
assert_eq!(url.path(), Some("/path"));
assert_eq!(url.query(), Some("query=value"));
assert_eq!(url.fragment(), Some("fragment"));

§Hostname Components

use faup_rs::{Url, Host};

let url = Url::parse("https://sub.example.co.uk").unwrap();
if let Host::Hostname(hostname) = url.host() {
    assert_eq!(hostname.full_name(), "sub.example.co.uk");
    assert_eq!(hostname.suffix(), Some("co.uk"));
    assert_eq!(hostname.domain(), Some("example.co.uk"));
    assert_eq!(hostname.subdomain(), Some("sub"));
}

§IP Addresses

use faup_rs::Url;

let url = Url::parse("http://[::1]").unwrap();
assert!(matches!(url.host(), faup_rs::Host::Ip(ip) if ip.is_loopback()));

§User Info (UTF-8 Support)

use faup_rs::Url;

let url = Url::parse("https://用户:密码@example.com").unwrap();
let user_info = url.userinfo().unwrap();
assert_eq!(user_info.username(), "用户");
assert_eq!(user_info.password(), Some("密码"));

§Custom TLDs

use faup_rs::Url;

let url = Url::parse("http://example.b32.i2p").unwrap();
assert_eq!(url.suffix(), Some("b32.i2p"));

§Examples

§Real-World URLs

use faup_rs::Url;

let urls = [
    "https://www.example.co.uk",
    "http://sub.domain.example.com/path/to/page",
    "https://例子.测试",
    "http://toaster.dyrøy.no",
    "http://full.custom-tld.test.b32.i2p",
];
for url_str in urls {
    let url = Url::parse(url_str).unwrap();
    println!("Parsed: {}", url);
}

§License

This project is licensed under the GNU General Public License v3.0 (GPLv3)..

Structs§

Hostname
Represents a parsed hostname with its components (subdomain, domain, and suffix).
Url
A parsed URL with support for hostnames, IPv4/IPv6 addresses, userinfo, ports, paths, queries, and fragments.
UserInfo
Represents user information (username and password) in a URL.

Enums§

Error
Host
Represents the host component of a URL, which can be either a hostname or an IP address.
Rule