iri_string::validate

Function fragment

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

Validates IRI fragment.

This validator corresponds to RiFragmentStr and RiFragmentString types.

Note that the first # character in an IRI is not a part of a fragment. For example, https://example.com/#foo has a fragment foo, not #foo.

ยงExamples

This type can have an IRI fragment. Note that the IRI foo://bar/baz#qux has the fragment qux, not #qux.

use iri_string::{spec::UriSpec, validate::fragment};
assert!(fragment::<UriSpec>("").is_ok());
assert!(fragment::<UriSpec>("foo").is_ok());
assert!(fragment::<UriSpec>("foo/bar").is_ok());
assert!(fragment::<UriSpec>("/foo/bar").is_ok());
assert!(fragment::<UriSpec>("//foo/bar").is_ok());
assert!(fragment::<UriSpec>("https://user:pass@example.com:8080").is_ok());
assert!(fragment::<UriSpec>("https://example.com/").is_ok());

Some characters and sequences cannot used in a fragment.

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