core:url
URL parsing and construction for special schemes (WHATWG URL Standard).
Supports http, https, ws, and wss schemes.
Synopsis
assert Url::parse("https://wado.dev/docs?q=synopsis") matches { Ok(url) && url.scheme == "https"
&& url.host == "wado.dev"
&& url.path == "/docs"
&& url.query_get("q") matches { Some(v) && v == "synopsis" } };
Functions
pub fn percent_encode(input: String) -> String
Percent-encodes a string. Encodes all characters except unreserved characters (A-Z, a-z, 0-9, '-', '.', '_', '~') per RFC 3986 §2.3.
pub fn percent_decode(input: String) -> Result<String, ParseError>
Decodes percent-encoded sequences (%XX). Returns Err if the input contains invalid percent-encoding or invalid UTF-8.
pub fn parse_query(query: String) -> TreeMap<String, String>
Parses a query string into key-value pairs (insertion-order preserved). Input should not include the leading "?". Handles "+" as space and decodes percent-encoding. Duplicate keys use last-value-wins semantics.
pub fn format_query(params: TreeMap<String, String>) -> String
Formats key-value pairs into a query string (insertion-order preserved). Output does not include the leading "?". Keys and values are percent-encoded; spaces become "+".
Structs
pub struct Url
A parsed URL with a special scheme (http, https, ws, wss).
Fields store raw (percent-encoded) values. Use percent_decode to get
decoded forms where needed.
scheme: String
The scheme, lowercase. One of: "http", "https", "ws", "wss".
username: String
The username component. Empty string if absent.
password: String
The password component. Empty string if absent.
host: String
The host. Always present for special schemes. IPv6 addresses are stored without brackets (e.g., "::1" not "[::1]").
port: Option<u16>
The port number. None means the default port for the scheme.
path: String
The path component. Always starts with "/". At minimum "/".
query: Option<String>
The query string without the leading "?". None if absent.
fragment: Option<String>
The fragment without the leading "#". None if absent.
pub fn parse(input: String) -> Result<Url, ParseError>
Parses a URL string. Only special schemes (http, https, ws, wss) are accepted.
pub fn from_parts(scheme: String, authority: String, path_with_query: Option<String>, fragment: Option<String>) -> Url
Constructs a Url from its scheme, authority, path-with-query, and
fragment. This constructor is infallible: any scheme is accepted,
a malformed authority is stored as-is in host, and missing or
empty paths default to "/".
Use to_string() to obtain the serialized URL.
pub fn query_params(&self) -> TreeMap<String, String>
Returns the query parameters as a TreeMap. Percent-encoded values
are decoded; "+" becomes " ". Returns an empty map if there is no
query component.
pub fn to_string(&self) -> String
Serializes the URL back to a string.
pub fn authority(&self) -> String
Returns the authority: [userinfo@]host[:port].
pub fn origin(&self) -> String
Returns the origin: scheme://host[:port] (excludes credentials).
pub fn path_with_query(&self) -> String
Returns path with query: /path[?query]. Matches wasi:http path_with_query.
pub fn effective_port(&self) -> u16
Returns the effective port (explicit port or default for the scheme).
pub fn resolve(&self, reference: String) -> Result<Url, ParseError>
Resolves a relative URL reference against this URL as the base. Follows RFC 3986 §5.
pub fn query_pairs(&self) -> TreeMap<String, String>
Parses the query string into a TreeMap (insertion-order preserved).
pub fn query_get(&self, key: String) -> Option<String>
Returns the value for the given query parameter key.
pub fn query_get_all(&self, key: String) -> List<String>
Returns all values for the given query parameter key. Computed from the raw query string to preserve duplicate keys.
pub fn with_query_pairs(&self, params: TreeMap<String, String>) -> Url
Returns a new Url with the query string replaced by the given params.
impl Display for Url
fn fmt(&self, f: &mut Formatter)
impl Serialize for Url
fn serialize<S: Serializer>(&self, s: &mut S) -> Result<(), SerializeError>
impl Deserialize for Url
fn deserialize<D: Deserializer>(d: &mut D) -> Result<Url, DeserializeError>
Variants
pub variant ParseError
Error type for URL parsing failures.
