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},
pct_enc::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.0)?;
// 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 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");Source§impl<T> Iri<T>
impl<T> Iri<T>
Source§impl<'i, 'o, T> Iri<T>where
T: BorrowOrShare<'i, 'o, str>,
impl<'i, 'o, T> Iri<T>where
T: BorrowOrShare<'i, 'o, str>,
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<T> Iri<T>
impl<T> Iri<T>
Sourcepub fn normalize(&self) -> Iri<String>
pub fn normalize(&self) -> Iri<String>
Normalizes the IRI.
This method applies syntax-based normalization described in Section 6.2.2 of RFC 3986 and Section 5.3.2 of RFC 3987, along with IPv6 address and default port normalization. This 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 or equals the scheme’s default, remove it along with the
':'delimiter. - If
selfhas 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
selfhas no authority and its path would start with"//", prepend"/."to the path.
This method is idempotent: self.normalize() equals self.normalize().normalize().
If you need to configure the behavior of normalization, consider using Normalizer instead.
§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 strip_fragment(&self) -> Iri<&str>
pub fn strip_fragment(&self) -> Iri<&str>
Returns a slice of this IRI with the fragment component removed.
§Examples
use fluent_uri::Iri;
let iri = Iri::parse("http://example.com/#fragment")?;
assert_eq!(iri.strip_fragment(), "http://example.com/");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::{pct_enc::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::{pct_enc::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/");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<Iri<&'de str>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Iri<&'de str>, <D as Deserializer<'de>>::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.Source§fn deserialize<D>(
deserializer: D,
) -> Result<Iri<String>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Iri<String>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl<T> Ord for Iri<T>
impl<T> Ord for Iri<T>
Source§impl<T> PartialOrd for Iri<T>
impl<T> PartialOrd for Iri<T>
Source§impl<T> Serialize for Iri<T>
Available on crate feature serde only.
impl<T> Serialize for Iri<T>
serde only.Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl<T> Copy for Iri<T>where
T: Copy,
impl<T> Eq for Iri<T>
Auto Trait Implementations§
impl<T> Freeze for Iri<T>where
T: Freeze,
impl<T> RefUnwindSafe for Iri<T>where
T: RefUnwindSafe,
impl<T> Send for Iri<T>where
T: Send,
impl<T> Sync for Iri<T>where
T: Sync,
impl<T> Unpin for Iri<T>where
T: Unpin,
impl<T> UnwindSafe for Iri<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.