| Crates.io | waterui-url |
| lib.rs | waterui-url |
| version | 0.2.1 |
| created_at | 2025-09-07 14:21:45.123153+00 |
| updated_at | 2025-12-14 11:30:23.701057+00 |
| description | A modern UI framework for Rust |
| homepage | |
| repository | https://github.com/water-rs/waterui |
| max_upload_size | |
| id | 1828169 |
| size | 62,558 |
Fast, zero-allocation URL parsing for WaterUI with compile-time validation and reactive fetching.
waterui-url provides a unified Url type that handles web URLs, local file paths, data URLs, and blob URLs with a consistent API. The library is designed for performance and safety, offering compile-time URL creation with validation, zero-allocation parsing using byte offsets, and seamless integration with WaterUI's reactive primitives.
Key features:
const fnUrl type for web URLs, local paths, data URLs, and blob URLsstd feature for Path support)nami signalsAdd this to your Cargo.toml:
[dependencies]
waterui-url = "0.1.0"
For file path support (requires std):
[dependencies]
waterui-url = { version = "0.1.0", features = ["std"] }
use waterui_url::Url;
// Compile-time URL creation with validation
const API_ENDPOINT: Url = Url::new("https://api.example.com/v1/users");
const LOCAL_ASSET: Url = Url::new("/assets/logo.png");
fn main() {
// Runtime parsing
let url: Url = "https://example.com/api?key=value".parse().unwrap();
println!("Host: {:?}", url.host()); // Some("example.com")
println!("Path: {}", url.path()); // "/api"
println!("Query: {:?}", url.query()); // Some("key=value")
}
The Url struct is a unified type that automatically detects and parses different URL kinds:
http://, https://, ftp://, ws://, wss://, etc./home/user/file.txt) or relative (./images/photo.jpg)data:text/plain;base64,SGVsbG8=blob:https://example.com/uuidThe parser runs once during construction and stores component offsets as u16 byte ranges, making subsequent access to URL parts O(1) with no additional parsing.
URLs created with Url::new() in const contexts are validated at compile time. Malformed URLs cause compilation errors:
// This compiles fine
const VALID: Url = Url::new("https://example.com");
// This fails at compile time: "Web URL must have a host"
const INVALID: Url = Url::new("https://");
The fetch() method returns a reactive signal that can be watched for changes, integrating with WaterUI's reactive update system:
use waterui_url::Url;
let url = Url::parse("https://api.example.com/data.json").unwrap();
let fetched = url.fetch();
// Use with WaterUI's reactive primitives
use waterui_url::Url;
let url = Url::parse("https://user:pass@example.com:8080/api/v1?id=123#section").unwrap();
assert_eq!(url.scheme(), Some("https"));
assert_eq!(url.host(), Some("example.com"));
assert_eq!(url.port(), Some(8080));
assert_eq!(url.path(), "/api/v1");
assert_eq!(url.query(), Some("id=123"));
assert_eq!(url.fragment(), Some("section"));
assert_eq!(url.authority(), Some("user:pass@example.com:8080"));
use waterui_url::Url;
let absolute = Url::new("/home/user/documents/file.pdf");
assert!(absolute.is_local());
assert!(absolute.is_absolute());
assert_eq!(absolute.extension(), Some("pdf"));
assert_eq!(absolute.filename(), Some("file.pdf"));
let relative = Url::new("./images/photo.jpg");
assert!(relative.is_relative());
assert_eq!(relative.extension(), Some("jpg"));
use waterui_url::Url;
let data_url = Url::from_data("image/png", b"PNG binary data here...");
assert!(data_url.is_data());
assert_eq!(data_url.scheme(), Some("data"));
// URL is automatically base64-encoded
println!("{}", data_url); // data:image/png;base64,UE5HIGJpbmFyeSBkYXRhIGhlcmUuLi4=
use waterui_url::Url;
let base = Url::new("https://cdn.example.com/assets/");
let joined = base.join("images/logo.png");
assert_eq!(joined.as_str(), "https://cdn.example.com/assets/images/logo.png");
// Handles file paths too
let base_file = Url::new("https://example.com/path/file.html");
let joined_file = base_file.join("other.html");
assert_eq!(joined_file.as_str(), "https://example.com/path/other.html");
Url::new(url: &'static str) -> Url - Create URL at compile time (const fn)Url::parse(url: impl AsRef<str>) -> Option<Url> - Parse web URL at runtimeUrl::from_file_path(path: impl AsRef<Path>) -> Url - Create from file path (requires std feature)Url::from_file_path_str(path: impl Into<Str>) -> Url - Create from path stringUrl::from_data(mime_type: &str, data: &[u8]) -> Url - Create base64-encoded data URLFromStr trait - Parse any URL type from stringis_web() -> bool - Check if URL is a web URL (http/https/ftp/ws/wss)is_local() -> bool - Check if URL is a local file pathis_data() -> bool - Check if URL is a data URLis_blob() -> bool - Check if URL is a blob URLis_absolute() -> bool - Check if URL or path is absoluteis_relative() -> bool - Check if URL or path is relativeAll component accessors are O(1) operations using pre-parsed byte offsets:
scheme() -> Option<&str> - URL scheme (e.g., "https", "file", "data")host() -> Option<&str> - Host portion for web URLsport() -> Option<u16> - Port number for web URLs (parsed as integer)path() -> &str - Path componentquery() -> Option<&str> - Query string without '?'fragment() -> Option<&str> - Fragment without '#'authority() -> Option<&str> - Full authority section (user:pass@host:port)extension() -> Option<&str> - File extension if presentfilename() -> Option<&str> - Filename from pathjoin(&self, path: &str) -> Url - Join URL with relative pathfetch(&self) -> Fetched - Get reactive signal for URL contentto_file_path(&self) -> Option<PathBuf> - Convert to file path (requires std feature)as_str(&self) -> &str - Get URL as string sliceinto_string(self) -> String - Convert to owned Stringinner(&self) -> Str - Get the underlying Str valueAsRef<str> - Automatic conversion to &strDisplay - Format URL as stringThe crate supports the following Cargo features:
std (optional): Enables std::path::Path integration for from_file_path() and to_file_path() methods. Without this feature, the crate is no_std compatible.The Url type stores the original URL string once and references its components using Span structs containing u16 start/end offsets. This design:
The entire parsing implementation uses const-compatible operations (byte-level comparisons, loops with manual indexing) to enable compile-time URL creation and validation. The parser:
[::1]The parse() function returns Result<Url, ParseError> for runtime parsing. The new() const fn panics on invalid URLs, enabling compile-time validation in const contexts.
This crate is no_std compatible by default. Enable the std feature for:
Path/PathBuf integration via from_file_path() and to_file_path()std::error::Error trait implementation for ParseErrorWindows, macOS, and Linux are all supported. The parser correctly handles platform-specific path conventions (backslashes, drive letters on Windows).