Skip to main content

Crate urly

Crate urly 

Source
Expand description

URL manipulation library with an API inspired by Python’s yarl.

Url is a cheaply cloneable URL reference stored in a single ByteString. Accessors return borrowed components with methods to decode them. General-purpose parsing normalizes URLs; Url::parse_target preserves unvalidated HTTP request-target components for later Url::validate_target and Url::normalize calls. Component setters normalize the components they replace, not the rest of a raw target.

use urly::Url;

let url = Url::from_static("https://example.com/api/v1?page=2");
assert_eq!(url.host(), Some("example.com"));
assert_eq!(url.port_or_known_default(), Some(443));

let users = &url / "users" / "john doe";
assert_eq!(users, "https://example.com/api/v1/users/john%20doe");
assert_eq!(users.path().segments().last().unwrap(), "john%20doe");

let mut url = users.join("../groups?id=1").unwrap();
url.extend_query_pairs([("sort", "name asc")]);
assert_eq!(url, "https://example.com/api/v1/groups?id=1&sort=name+asc");

let url = Url::builder()
    .scheme("http")
    .host("münchen.de")
    .path("/a b")
    .query_pair("q", "x&y")
    .build()
    .unwrap();
assert_eq!(url, "http://xn--mnchen-3ya.de/a%20b?q=x%26y");

§Features

  • http - conversions between Url and http::Uri
  • serde - Serialize and Deserialize for Url

Modules§

quoting
Percent-encoding inspired by Python’s yarl.

Structs§

Authority
URL authority: [userinfo "@"] host [":" port].
Builder
URL builder.
Fragment
Percent-encoded URL fragment, without the leading #.
InvalidUrl
An error returned when a URL or URL component is invalid.
InvalidUrlParts
An error returned by Url::from_parts.
Parts
Percent-encoded URL parts.
Path
Percent-encoded URL path.
PathAndQuery
Path with an optional query: path ["?" query].
Port
URL port.
Query
Percent-encoded URL query, without the leading ?.
QueryPairs
Iterator over decoded query pairs, see Query::pairs.
Scheme
URL scheme, e.g. https.
Segments
Iterator over percent-encoded path segments, see Path::segments.
Url
URL reference, normalized by the general-purpose parsers.
UserInfo
URL userinfo: user [":" password], percent-encoded.

Enums§

ErrorKind
The reason a URL or URL component is invalid.
Host
Parsed URL host.