iri_string::validate

Function iri

source
pub fn iri<S: Spec>(s: &str) -> Result<(), Error>
Expand description

Validates IRI.

This validator corresponds to RiStr and RiString types.

ยงExamples

This type can have an IRI (which is absolute, and may have fragment part).

use iri_string::{spec::UriSpec, validate::iri};
assert!(iri::<UriSpec>("https://user:pass@example.com:8080").is_ok());
assert!(iri::<UriSpec>("https://example.com/").is_ok());
assert!(iri::<UriSpec>("https://example.com/foo?bar=baz").is_ok());
assert!(iri::<UriSpec>("https://example.com/foo?bar=baz#qux").is_ok());
assert!(iri::<UriSpec>("foo:bar").is_ok());
assert!(iri::<UriSpec>("foo:").is_ok());
// `foo://.../` below are all allowed. See the crate documentation for detail.
assert!(iri::<UriSpec>("foo:/").is_ok());
assert!(iri::<UriSpec>("foo://").is_ok());
assert!(iri::<UriSpec>("foo:///").is_ok());
assert!(iri::<UriSpec>("foo:////").is_ok());
assert!(iri::<UriSpec>("foo://///").is_ok());

Relative IRI reference is not allowed.

use iri_string::{spec::UriSpec, validate::iri};
// This is relative path.
assert!(iri::<UriSpec>("foo/bar").is_err());
// `/foo/bar` is an absolute path, but it is authority-relative.
assert!(iri::<UriSpec>("/foo/bar").is_err());
// `//foo/bar` is termed "network-path reference",
// or usually called "protocol-relative reference".
assert!(iri::<UriSpec>("//foo/bar").is_err());
// Same-document reference is relative.
assert!(iri::<UriSpec>("#foo").is_err());
// Empty string is not a valid absolute IRI.
assert!(iri::<UriSpec>("").is_err());

Some characters and sequences cannot used in an IRI.

use iri_string::{spec::UriSpec, validate::iri};
// `<` and `>` cannot directly appear in an IRI.
assert!(iri::<UriSpec>("<not allowed>").is_err());
// Broken percent encoding cannot appear in an IRI.
assert!(iri::<UriSpec>("%").is_err());
assert!(iri::<UriSpec>("%GG").is_err());