fluent_uri

Struct Iri

source
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>

source

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> for I = &str;
  • Result<Iri<String>, ParseError<String>> for I = 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 Iri<String>

source

pub fn builder() -> Builder<Self, NonRefStart>

Creates a new builder for IRI.

source

pub fn borrow(&self) -> Iri<&str>

Borrows this Iri<String> as Iri<&str>.

source

pub fn into_string(self) -> String

Consumes this Iri<String> and yields the underlying String.

source§

impl Iri<&str>

source

pub fn to_owned(&self) -> Iri<String>

Creates a new Iri<String> by cloning the contents of this Iri<&str>.

source§

impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Iri<T>

source

pub fn as_str(&'i self) -> &'o str

Returns the IRI as a string slice.

source

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);
source

pub fn authority(&'i self) -> Option<IAuthority<'o>>

Returns the optional authority component.

§Examples
use fluent_uri::Iri;

let iri = Iri::parse("http://example.com/")?;
assert!(iri.authority().is_some());

let iri = Iri::parse("mailto:user@example.com")?;
assert!(iri.authority().is_none());
source

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

pub fn query(&'i self) -> Option<&'o EStr<IQuery>>

Returns the optional query component.

§Examples
use fluent_uri::{encoding::EStr, Iri};

let iri = Iri::parse("http://example.com/?lang=en")?;
assert_eq!(iri.query(), Some(EStr::new_or_panic("lang=en")));

let iri = Iri::parse("ftp://192.0.2.1/")?;
assert_eq!(iri.query(), None);
source

pub fn fragment(&'i self) -> Option<&'o EStr<IFragment>>

Returns the optional fragment component.

§Examples
use fluent_uri::{encoding::EStr, Iri};

let iri = Iri::parse("http://example.com/#usage")?;
assert_eq!(iri.fragment(), Some(EStr::new_or_panic("usage")));

let iri = Iri::parse("ftp://192.0.2.1/")?;
assert_eq!(iri.fragment(), None);
source§

impl<'i, 'o, T: Bos<str>> Iri<T>

source

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 self contains a scheme and an absolute path, apply the remove_dot_segments algorithm to the path, taking account of percent-encoded dot segments as described at UriRef::resolve_against.
  • If self contains 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");
source

pub fn has_authority(&self) -> bool

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());
source

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());
source

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());
source

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>

source

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>

source

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<T: Bos<str>> AsRef<str> for Iri<T>

source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<T: Bos<str>> Borrow<str> for Iri<T>

source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
source§

impl<T: Clone> Clone for Iri<T>

source§

fn clone(&self) -> Iri<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Bos<str>> Debug for Iri<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Value> Default for Iri<T>

source§

fn default() -> Self

Creates an empty IRI.

source§

impl<'de> Deserialize<'de> for Iri<&'de str>

Available on crate feature serde only.
source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<'de> Deserialize<'de> for Iri<String>

Available on crate feature serde only.
source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T: Bos<str>> Display for Iri<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> From<Iri<&'a str>> for &'a str

source§

fn from(value: Iri<&'a str>) -> &'a str

Equivalent to as_str.

source§

impl From<Iri<&str>> for Iri<String>

source§

fn from(value: Iri<&str>) -> Self

Equivalent to to_owned.

source§

impl<'a> From<Iri<String>> for String

source§

fn from(value: Iri<String>) -> String

Equivalent to into_string.

source§

impl<T: Bos<str>> From<Iri<T>> for IriRef<T>

source§

fn from(value: Iri<T>) -> Self

Consumes the Iri and creates a new IriRef with the same contents.

source§

impl<T: Bos<str>> From<Uri<T>> for Iri<T>

source§

fn from(value: Uri<T>) -> Self

Consumes the Uri and creates a new Iri with the same contents.

source§

impl FromStr for Iri<String>

source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Equivalent to Iri::parse(s).map(|r| r.to_owned()).

source§

type Err = ParseError

The associated error which can be returned from parsing.
source§

impl<T: Bos<str>> Hash for Iri<T>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<T: Bos<str>> Ord for Iri<T>

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
source§

impl<T: Bos<str>> PartialEq<&str> for Iri<T>

source§

fn eq(&self, other: &&str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Bos<str>> PartialEq<Iri<T>> for &str

source§

fn eq(&self, other: &Iri<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Bos<str>> PartialEq<Iri<T>> for str

source§

fn eq(&self, other: &Iri<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Bos<str>, U: Bos<str>> PartialEq<Iri<U>> for Iri<T>

source§

fn eq(&self, other: &Iri<U>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Bos<str>> PartialEq<str> for Iri<T>

source§

fn eq(&self, other: &str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Bos<str>> PartialOrd for Iri<T>

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T: Bos<str>> Serialize for Iri<T>

Available on crate feature serde only.
source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<'a> TryFrom<&'a str> for Iri<&'a str>

source§

fn try_from(value: &'a str) -> Result<Self, Self::Error>

Equivalent to parse.

source§

type Error = ParseError

The type returned in the event of a conversion error.
source§

impl<'a> TryFrom<Iri<&'a str>> for Uri<&'a str>

source§

fn try_from(value: Iri<&'a str>) -> Result<Self, Self::Error>

Converts the IRI to a URI if it is ASCII.

source§

type Error = ParseError

The type returned in the event of a conversion error.
source§

impl TryFrom<Iri<String>> for Uri<String>

source§

fn try_from(value: Iri<String>) -> Result<Self, Self::Error>

Converts the IRI to a URI if it is ASCII.

source§

type Error = ParseError<Iri<String>>

The type returned in the event of a conversion error.
source§

impl<'a> TryFrom<IriRef<&'a str>> for Iri<&'a str>

source§

fn try_from(value: IriRef<&'a str>) -> Result<Self, Self::Error>

Converts the IRI reference to an IRI if it contains a scheme.

source§

type Error = ParseError

The type returned in the event of a conversion error.
source§

impl TryFrom<IriRef<String>> for Iri<String>

source§

fn try_from(value: IriRef<String>) -> Result<Self, Self::Error>

Converts the IRI reference to an IRI if it contains a scheme.

source§

type Error = ParseError<IriRef<String>>

The type returned in the event of a conversion error.
source§

impl TryFrom<String> for Iri<String>

source§

fn try_from(value: String) -> Result<Self, Self::Error>

Equivalent to parse.

source§

type Error = ParseError<String>

The type returned in the event of a conversion error.
source§

impl<T: Copy> Copy for Iri<T>

source§

impl<T: Bos<str>> 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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,