pub struct Iri<T> { /* private fields */ }Expand description
An IRI.
See the crate-level documentation for an explanation of the above term(s).
§Variants
Two variants of Iri are available:
Iri<&str> (borrowed) and Iri<String> (owned).
Iri<&'a str>
outputs references with lifetime 'a where possible
(thanks to borrow-or-share):
use fluent_uri::Iri;
// Keep a reference to the path after dropping the `Iri`.
let path = Iri::parse("foo:bar")?.path();
assert_eq!(path, "bar");§Comparison
Iris
are compared lexicographically
by their byte values. Normalization is not performed prior to comparison.
§Examples
Parse and extract components from an IRI:
use fluent_uri::{
component::{Host, Scheme},
encoding::EStr,
Iri,
};
const SCHEME_FOO: &Scheme = Scheme::new_or_panic("foo");
let s = "foo://user@example.com:8042/over/there?name=ferret#nose";
let iri = Iri::parse(s)?;
assert_eq!(iri.scheme(), SCHEME_FOO);
let auth = iri.authority().unwrap();
assert_eq!(auth.as_str(), "user@example.com:8042");
assert_eq!(auth.userinfo().unwrap(), "user");
assert_eq!(auth.host(), "example.com");
assert!(matches!(auth.host_parsed(), Host::RegName(name) if name == "example.com"));
assert_eq!(auth.port().unwrap(), "8042");
assert_eq!(auth.port_to_u16(), Ok(Some(8042)));
assert_eq!(iri.path(), "/over/there");
assert_eq!(iri.query().unwrap(), "name=ferret");
assert_eq!(iri.fragment().unwrap(), "nose");Parse into and convert between
Iri<&str> and Iri<String>:
use fluent_uri::Iri;
let s = "http://example.com/";
// Parse into a `Iri<&str>` from a string slice.
let iri: Iri<&str> = Iri::parse(s)?;
// Parse into a `Iri<String>` from an owned string.
let iri_owned: Iri<String> = Iri::parse(s.to_owned()).map_err(|e| e.strip_input())?;
// Convert a `Iri<&str>` to `Iri<String>`.
let iri_owned: Iri<String> = iri.to_owned();
// Borrow a `Iri<String>` as `Iri<&str>`.
let iri: Iri<&str> = iri_owned.borrow();Implementations§
Source§impl<T> Iri<T>
impl<T> Iri<T>
Sourcepub fn parse<I>(input: I) -> Result<Self, I::Err>where
I: Parse<Val = T>,
pub fn parse<I>(input: I) -> Result<Self, I::Err>where
I: Parse<Val = T>,
Parses an IRI from a string into an Iri.
The return type is
Result<Iri<&str>, ParseError>forI = &str;Result<Iri<String>, ParseError<String>>forI = String.
§Errors
Returns Err if the string does not match the
IRI ABNF rule from RFC 3987.
From a ParseError<String>, you may recover or strip the input
by calling into_input or strip_input on it.
Source§impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Iri<T>
impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Iri<T>
Sourcepub fn scheme(&'i self) -> &'o Scheme
pub fn scheme(&'i self) -> &'o Scheme
Returns the scheme component.
Note that the scheme component is case-insensitive.
See the documentation of Scheme for more details on comparison.
§Examples
use fluent_uri::{component::Scheme, Iri};
const SCHEME_HTTP: &Scheme = Scheme::new_or_panic("http");
let iri = Iri::parse("http://example.com/")?;
assert_eq!(iri.scheme(), SCHEME_HTTP);Sourcepub fn path(&'i self) -> &'o EStr<IPath>
pub fn path(&'i self) -> &'o EStr<IPath>
Returns the path component.
The path component is always present, although it may be empty.
The returned EStr slice has extension methods for the path component.
§Examples
use fluent_uri::Iri;
let iri = Iri::parse("http://example.com/")?;
assert_eq!(iri.path(), "/");
let iri = Iri::parse("mailto:user@example.com")?;
assert_eq!(iri.path(), "user@example.com");
let iri = Iri::parse("http://example.com")?;
assert_eq!(iri.path(), "");Source§impl<'i, 'o, T: Bos<str>> Iri<T>
impl<'i, 'o, T: Bos<str>> Iri<T>
Sourcepub fn normalize(&self) -> Iri<String>
pub fn normalize(&self) -> Iri<String>
Normalizes the IRI.
This method applies the syntax-based normalization described in Section 6.2.2 of RFC 3986 and Section 5.3.2 of RFC 3987, which is effectively equivalent to taking the following steps in order:
- Decode any percent-encoded octets that correspond to an allowed character which is not reserved.
- Uppercase the hexadecimal digits within all percent-encoded octets.
- Lowercase all ASCII characters within the scheme and the host except the percent-encoded octets.
- Turn any IPv6 literal address into its canonical form as per RFC 5952.
- If the port is empty, remove its
':'delimiter. - If
selfcontains a scheme and an absolute path, apply theremove_dot_segmentsalgorithm to the path, taking account of percent-encoded dot segments as described atUriRef::resolve_against. - If
selfcontains no authority and its path would start with"//", prepend"/."to the path.
This method is idempotent: self.normalize() equals self.normalize().normalize().
§Examples
use fluent_uri::Iri;
let iri = Iri::parse("eXAMPLE://a/./b/../b/%63/%7bfoo%7d")?;
assert_eq!(iri.normalize(), "example://a/b/c/%7Bfoo%7D");Checks whether an authority component is present.
§Examples
use fluent_uri::Iri;
assert!(Iri::parse("http://example.com/")?.has_authority());
assert!(!Iri::parse("mailto:user@example.com")?.has_authority());Sourcepub fn has_query(&self) -> bool
pub fn has_query(&self) -> bool
Checks whether a query component is present.
§Examples
use fluent_uri::Iri;
assert!(Iri::parse("http://example.com/?lang=en")?.has_query());
assert!(!Iri::parse("ftp://192.0.2.1/")?.has_query());Sourcepub fn has_fragment(&self) -> bool
pub fn has_fragment(&self) -> bool
Checks whether a fragment component is present.
§Examples
use fluent_uri::Iri;
assert!(Iri::parse("http://example.com/#usage")?.has_fragment());
assert!(!Iri::parse("ftp://192.0.2.1/")?.has_fragment());Sourcepub fn with_fragment(&self, opt: Option<&EStr<IFragment>>) -> Iri<String>
pub fn with_fragment(&self, opt: Option<&EStr<IFragment>>) -> Iri<String>
Creates a new IRI
by replacing the fragment component of self with the given one.
The fragment component is removed when opt.is_none().
§Examples
use fluent_uri::{encoding::EStr, Iri};
let iri = Iri::parse("http://example.com/")?;
assert_eq!(
iri.with_fragment(Some(EStr::new_or_panic("fragment"))),
"http://example.com/#fragment"
);
let iri = Iri::parse("http://example.com/#fragment")?;
assert_eq!(
iri.with_fragment(None),
"http://example.com/"
);Source§impl Iri<String>
impl Iri<String>
Sourcepub fn set_fragment(&mut self, opt: Option<&EStr<IFragment>>)
pub fn set_fragment(&mut self, opt: Option<&EStr<IFragment>>)
Replaces the fragment component of self with the given one.
The fragment component is removed when opt.is_none().
§Examples
use fluent_uri::{encoding::EStr, Iri};
let mut iri = Iri::parse("http://example.com/")?.to_owned();
iri.set_fragment(Some(EStr::new_or_panic("fragment")));
assert_eq!(iri, "http://example.com/#fragment");
iri.set_fragment(None);
assert_eq!(iri, "http://example.com/");Source§impl<T: Bos<str>> Iri<T>
impl<T: Bos<str>> Iri<T>
Sourcepub fn to_uri(&self) -> Uri<String>
pub fn to_uri(&self) -> Uri<String>
Converts the IRI to a URI by percent-encoding non-ASCII characters.
Punycode encoding is not performed during conversion.
§Examples
use fluent_uri::Iri;
let iri = Iri::parse("http://www.example.org/résumé.html").unwrap();
assert_eq!(iri.to_uri(), "http://www.example.org/r%C3%A9sum%C3%A9.html");
let iri = Iri::parse("http://résumé.example.org").unwrap();
assert_eq!(iri.to_uri(), "http://r%C3%A9sum%C3%A9.example.org");Trait Implementations§
Source§impl<'de> Deserialize<'de> for Iri<&'de str>
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Iri<&'de str>
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<'de> Deserialize<'de> for Iri<String>
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Iri<String>
serde only.