iri_string/lib.rs
1//! String types for [RFC 3987 Internationalized Resource Identifiers (IRIs)][RFC 3987] and
2//! [RFC 3986 Uniform Resource Identifiers (URIs)][RFC 3986].
3//!
4//! Note that this crate does not have any extra knowledge about protocols.
5//! Comparisons between IRI strings by `PartialEq` and `Eq` is implemented as [simple string
6//! comparison](https://tools.ietf.org/html/rfc3986#section-6.2.1).
7//! You should implement by yourself or use another crate to use such extra knowledge to compare
8//! IRIs / URIs.
9//!
10//! # Capability
11//!
12//! This crate provides many features for IRIs / URIs.
13//!
14//! ## String types
15//!
16//! [`types` module][`types`] module provides various string types for IRIs and URIs.
17//! The borrowed string types are unsized slice types (such as `[u8]` and `str`)
18//! and not a sized struct, so they are highly interoperable with for example
19//! `Cow` and `Rc`. Conversions between `&str` and borrwed IRI string types are easy.
20//!
21//! ## Resolvers
22//!
23//! [`resolve` module][`resolve`] provides IRI / URI references resolver.
24//! However, you are recommended to use methods of string types such as
25//! [`RiReferenceStr::resolve_against()`] or [`RiRelativeStr::resolve_against()`]
26//! if you don't intend to resolve multiple IRIs against the same base.
27//!
28//! ## Validators
29//!
30//! Validator functions are provided from [`validate` module][`validate`].
31//!
32//! ## Percent encoding
33//!
34//! [`percent_encode` module][`percent_encode`] provides a converter to encode
35//! user-provided string into percent-encoded one (if syntax requires so).
36//!
37//! ## IRI builder
38//!
39//! [`build` module][`build`] provides IRI builder.
40//!
41//! ## URI template (RFC 6570)
42//!
43//! [`template` module][`template`] provides an RFC 6570 URI Template processor.
44//!
45//! # Feature flags
46//!
47//! ## `std` and `alloc` support
48//!
49//! This crate supports `no_std` usage.
50//!
51//! * `alloc` feature:
52//! + Std library or `alloc` crate is required.
53//! + This feature enables types and functions which require memory allocation,
54//! e.g. `types::IriString` and `types::IriRelativeStr::resolve_against()`.
55//! * `std` feature (**enabled by default**):
56//! + Std library is required.
57//! + This automatically enables `alloc` feature.
58//! + The feature let the crate utilize std-specific stuff, such as `std::error::Error` trait.
59//! * With neither of them:
60//! + The crate can be used in `no_std` environment.
61//!
62//! ## Other features
63//!
64//! * `serde`
65//! + Enables serde support.
66//! + Implement `Serailize` and `Deserialize` traits for IRI / URI types.
67//! * `memchr`
68//! + Enables faster internal character search.
69//!
70//! # Rationale
71//!
72//! ## `foo:`, `foo:/`, `foo://`, `foo:///`, `foo:////`, ... are valid IRIs
73//!
74//! All of these are valid IRIs.
75//! (On the other hand, all of them are invalid as relative IRI reference, because they don't
76//! match `relative-part` rule, especially `path-noscheme`, as the first path component of the
77//! relative path contains a colon.)
78//!
79//! * `foo:`
80//! + Decomposed to `<scheme="foo">:<path-empty="">`.
81//! * `foo:/`
82//! + Decomposed to `<scheme="foo">:<path-absolute="/">`.
83//! * `foo://`
84//! + Decomposed to `<scheme="foo">://<authority=""><path-absolute="">`.
85//! * `foo:///`
86//! + Decomposed to `<scheme="foo">://<authority=""><path-absolute="/">`.
87//! * `foo:////`
88//! + Decomposed to `<scheme="foo">://<authority=""><path-absolute="//">`.
89//! * `foo://///`
90//! + Decomposed to `<scheme="foo">://<authority=""><path-absolute="///">`.
91//!
92//! RFC 3986 says that "if authority is absent, path cannot start with `//`".
93//!
94//! > When authority is present, the path must either be empty or begin with a slash ("/")
95//! > character. When authority is not present, the path cannot begin with two slash characters
96//! > ("//").
97//! >
98//! > --- [RFC 3986, section 3. Syntax Components](https://tools.ietf.org/html/rfc3986#section-3).
99//!
100//! > If a URI contains an authority component, then the path component must either be empty or
101//! > begin with a slash ("/") character. If a URI does not contain an authority component, then the
102//! > path cannot begin with two slash characters ("//").
103//! >
104//! > --- [RFC 3986, section 3.3. Path](https://tools.ietf.org/html/rfc3986#section-3.3)
105//!
106//! We should interpret them as "if `authority` rule is completely unused (i.e. does not match any
107//! strings **including empty string**), path cannot start with `//`".
108//! In other words, we should consider this as **explaining the ABNF of `hier-part` rule**
109//! (especially why it does not use `path` rule), but **not adding extra restriction to the rule
110//! written in ABNF**.
111//!
112//! This restriction is necessary to remove ambiguity in decomposition of some strings.
113//! For example, it is natural to decompose `foo://` to `<scheme="foo">:<path="//">` or
114//! `<scheme="foo">://<authority=""><path="">`.
115//! The restriction, **which is already encoded to the ABNF rule**, tells us to always decompose to
116//! the latter form, rather than the former one.
117//!
118//! Readers of the spec might be confused by "when authority is **present**" and "if a URI
119//! **contains** an authority component, which is unclear.
120//! However, based on the interpretation above, we should consider authority part with empty string
121//! as satisfying the condition "authority is **present**".
122//!
123//! ## IRI resolution can fail
124//!
125//! For some inputs, resulting string of IRI normalization and resolution can be syntactically
126//! correct but semantically wrong. In such cases, the normalizer and resolver provided by this
127//! crate do not silently "fix" the IRI by non-standard processing, but just
128//! fail by returning `Err(_)`.
129//!
130//! For details, see the documentation of [`normalize`] module.
131//!
132//! [RFC 3986]: https://tools.ietf.org/html/rfc3986
133//! [RFC 3987]: https://tools.ietf.org/html/rfc3987
134//! [`RiReferenceStr::resolve_against()`]: `types::RiReferenceStr::resolve_against`
135//! [`RiRelativeStr::resolve_against()`]: `types::RiRelativeStr::resolve_against`
136#![warn(missing_docs)]
137#![warn(unsafe_op_in_unsafe_fn)]
138#![warn(clippy::missing_docs_in_private_items)]
139#![warn(clippy::undocumented_unsafe_blocks)]
140#![cfg_attr(not(feature = "std"), no_std)]
141#![cfg_attr(docsrs, feature(doc_auto_cfg))]
142
143#[cfg(feature = "alloc")]
144extern crate alloc;
145
146pub mod build;
147pub mod components;
148pub mod convert;
149pub mod format;
150pub mod mask_password;
151pub mod normalize;
152pub(crate) mod parser;
153pub mod percent_encode;
154pub(crate) mod raw;
155pub mod resolve;
156pub mod spec;
157pub mod template;
158pub mod types;
159pub mod validate;