Debian Codegraph @

README.md

raw symbols SCIP
1 # fluent-uri
2
3 A generic URI/IRI handling library compliant with [RFC 3986] and [RFC 3987]. It is:
4
5 - **Fast:** Zero-copy parsing. Benchmarked to be highly performant.[^bench-res]
6 - **Easy:** Carefully designed and documented APIs. Handy percent-encoding utilities.
7 - **Correct:** Forbids unsafe code. Extensively fuzz-tested against other implementations.
8
9 [![crates.io](https://img.shields.io/crates/v/fluent-uri.svg)](https://crates.io/crates/fluent-uri)
10 [![build](https://img.shields.io/github/actions/workflow/status/yescallop/fluent-uri-rs/ci.yml
11 )](https://github.com/yescallop/fluent-uri-rs/actions/workflows/ci.yml)
12 [![license](https://img.shields.io/crates/l/fluent-uri.svg)](/LICENSE)
13
14 [Documentation](https://docs.rs/fluent-uri) | [Discussions](https://github.com/yescallop/fluent-uri-rs/discussions)
15
16 [RFC 3986]: https://datatracker.ietf.org/doc/html/rfc3986
17 [RFC 3987]: https://datatracker.ietf.org/doc/html/rfc3987
18 [^bench-res]: In [a benchmark](https://github.com/yescallop/fluent-uri-rs/blob/main/bench/benches/bench.rs)
19 on an Intel Core i5-11300H processor, `fluent-uri` parsed a 61-byte IRI
20 in ~85ns compared to ~125ns for `iref`, `iri-string`, and `oxiri`.
21
22 ## Terminology
23
24 A *[URI reference]* is either a *[URI]* or a *[relative reference]*. If it starts with a *[scheme]*
25 (like `http`, `ftp`, `mailto`, etc.) followed by a colon (`:`), it is a URI. For example,
26 `http://example.com/` and `mailto:[email protected]` are URIs. Otherwise, it is
27 a relative reference. For example, `//example.org/`, `/index.html`, `../`, `foo`,
28 `?bar`, and `#baz` are relative references.
29
30 An *[IRI]* (reference) is an internationalized version of URI (reference)
31 which may contain non-ASCII characters.
32
33 [URI reference]: https://datatracker.ietf.org/doc/html/rfc3986#section-4.1
34 [URI]: https://datatracker.ietf.org/doc/html/rfc3986#section-3
35 [IRI]: https://datatracker.ietf.org/doc/html/rfc3987#section-2
36 [relative reference]: https://datatracker.ietf.org/doc/html/rfc3986#section-4.2
37 [scheme]: https://datatracker.ietf.org/doc/html/rfc3986#section-3.1
38
39 ## Examples
40
41 - Parse and extract components from a URI:
42
43 ```rust
44 const SCHEME_FOO: &Scheme = Scheme::new_or_panic("foo");
45
46 let s = "foo://[email protected]:8042/over/there?name=ferret#nose";
47 let uri = Uri::parse(s)?;
48
49 assert_eq!(uri.scheme(), SCHEME_FOO);
50
51 let auth = uri.authority().unwrap();
52 assert_eq!(auth.as_str(), "[email protected]:8042");
53 assert_eq!(auth.userinfo().unwrap(), "user");
54 assert_eq!(auth.host(), "example.com");
55 assert!(matches!(auth.host_parsed(), Host::RegName(name) if name == "example.com"));
56 assert_eq!(auth.port().unwrap(), "8042");
57 assert_eq!(auth.port_to_u16(), Ok(Some(8042)));
58
59 assert_eq!(uri.path(), "/over/there");
60 assert_eq!(uri.query().unwrap(), "name=ferret");
61 assert_eq!(uri.fragment().unwrap(), "nose");
62 ```
63
64 - Build a URI using the builder pattern:
65
66 ```rust
67 const SCHEME_FOO: &Scheme = Scheme::new_or_panic("foo");
68
69 let uri = Uri::builder()
70 .scheme(SCHEME_FOO)
71 .authority_with(|b| {
72 b.userinfo(EStr::new_or_panic("user"))
73 .host(EStr::new_or_panic("example.com"))
74 .port(8042)
75 })
76 .path(EStr::new_or_panic("/over/there"))
77 .query(EStr::new_or_panic("name=ferret"))
78 .fragment(EStr::new_or_panic("nose"))
79 .build()
80 .unwrap();
81
82 assert_eq!(
83 uri.as_str(),
84 "foo://[email protected]:8042/over/there?name=ferret#nose"
85 );
86 ```
87
88 - Resolve a URI reference against a base URI:
89
90 ```rust
91 let base = Uri::parse("http://example.com/foo/bar")?;
92
93 let uri_ref = UriRef::parse("baz")?;
94 assert_eq!(uri_ref.resolve_against(&base).unwrap(), "http://example.com/foo/baz");
95
96 let uri_ref = UriRef::parse("../baz")?;
97 assert_eq!(uri_ref.resolve_against(&base).unwrap(), "http://example.com/baz");
98
99 let uri_ref = UriRef::parse("?baz")?;
100 assert_eq!(uri_ref.resolve_against(&base).unwrap(), "http://example.com/foo/bar?baz");
101 ```
102
103 - Normalize a URI:
104
105 ```rust
106 let uri = Uri::parse("eXAMPLE://a/./b/../b/%63/%7bfoo%7d")?;
107 assert_eq!(uri.normalize(), "example://a/b/c/%7Bfoo%7D");
108 ```
109
110 - `EStr` (Percent-encoded string slices):
111
112 All components in a URI that may be percent-encoded are parsed as `EStr`s,
113 which allows easy splitting and decoding:
114
115 ```rust
116 let s = "?name=%E5%BC%A0%E4%B8%89&speech=%C2%A1Ol%C3%A9%21";
117 let query = UriRef::parse(s).unwrap().query().unwrap();
118 let map: HashMap<_, _> = query
119 .split('&')
120 .map(|s| s.split_once('=').unwrap_or((s, EStr::EMPTY)))
121 .map(|(k, v)| (k.decode().to_string_lossy(), v.decode().to_string_lossy()))
122 .collect();
123 assert_eq!(map["name"], "张三");
124 assert_eq!(map["speech"], "¡Olé!");
125 ```
126
127 - `EString` (A percent-encoded, growable string):
128
129 You can encode key-value pairs to a query string and use it to build a URI reference:
130
131 ```rust
132 let pairs = [("name", "张三"), ("speech", "¡Olé!")];
133 let mut buf = EString::<Query>::new();
134 for (k, v) in pairs {
135 if !buf.is_empty() {
136 buf.push_byte(b'&');
137 }
138 buf.encode_str::<Data>(k);
139 buf.push_byte(b'=');
140 buf.encode_str::<Data>(v);
141 }
142
143 assert_eq!(buf, "name=%E5%BC%A0%E4%B8%89&speech=%C2%A1Ol%C3%A9%21");
144
145 let uri_ref = UriRef::builder()
146 .path(EStr::EMPTY)
147 .query(&buf)
148 .build()
149 .unwrap();
150 assert_eq!(uri_ref.as_str(), "?name=%E5%BC%A0%E4%B8%89&speech=%C2%A1Ol%C3%A9%21");
151 ```