Struct RiString

struct RiString<S> { ... }

An owned string of an absolute IRI possibly with fragment part.

This corresponds to IRI rule in RFC 3987 (and URI rule in RFC 3986). The rule for IRI is scheme ":" ihier-part [ "?" iquery ] [ "#" ifragment ]. In other words, this is RiAbsoluteString with fragment part allowed.

For details, see the document for RiStr.

Enabled by alloc or std feature.

Implementations

impl RiString<IriSpec>

fn encode_to_uri_inline(self: &mut Self)

Percent-encodes the IRI into a valid URI that identifies the equivalent resource.

After the encode, the IRI is also a valid URI.

If you want a new URI string rather than modifying the IRI string, or if you need more precise control over memory allocation and buffer handling, use [encode_to_uri]IriStr::encode_to_uri method.

Panics

Panics if the memory allocation failed.

Examples

# use iri_string::validate::Error;
#[cfg(feature = "alloc")] {
use iri_string::types::IriString;

let mut iri = IriString::try_from("http://example.com/?alpha=\u{03B1}")?;
iri.encode_to_uri_inline();
assert_eq!(iri, "http://example.com/?alpha=%CE%B1");
# }
# Ok::<_, Error>(())
fn try_encode_to_uri_inline(self: &mut Self) -> Result<(), TryReserveError>

Percent-encodes the IRI into a valid URI that identifies the equivalent resource.

After the encode, the IRI is also a valid URI.

If you want a new URI string rather than modifying the IRI string, or if you need more precise control over memory allocation and buffer handling, use [encode_to_uri]IriStr::encode_to_uri method.

Examples

# use iri_string::validate::Error;
#[cfg(feature = "alloc")] {
use iri_string::types::IriString;

let mut iri = IriString::try_from("http://example.com/?alpha=\u{03B1}")?;
iri.try_encode_to_uri_inline()
    .expect("failed to allocate memory");
assert_eq!(iri, "http://example.com/?alpha=%CE%B1");
# }
# Ok::<_, Error>(())
fn encode_into_uri(self: Self) -> UriString

Percent-encodes the IRI into a valid URI that identifies the equivalent resource.

If you want a new URI string rather than modifying the IRI string, or if you need more precise control over memory allocation and buffer handling, use [encode_to_uri]IriStr::encode_to_uri method.

Examples

# use iri_string::validate::Error;
#[cfg(feature = "alloc")] {
use iri_string::types::{IriString, UriString};

let iri = IriString::try_from("http://example.com/?alpha=\u{03B1}")?;
// Type annotation here is not necessary.
let uri: UriString = iri.encode_into_uri();
assert_eq!(uri, "http://example.com/?alpha=%CE%B1");
# }
# Ok::<_, Error>(())
fn try_encode_into_uri(self: Self) -> Result<UriString, TryReserveError>

Percent-encodes the IRI into a valid URI that identifies the equivalent resource.

If you want a new URI string rather than modifying the IRI string, or if you need more precise control over memory allocation and buffer handling, use [encode_to_uri]IriStr::encode_to_uri method.

Examples

# use iri_string::validate::Error;
#[cfg(feature = "alloc")] {
use iri_string::types::{IriString, UriString};

let iri = IriString::try_from("http://example.com/?alpha=\u{03B1}")?;
// Type annotation here is not necessary.
let uri: UriString = iri.try_encode_into_uri()
    .expect("failed to allocate memory");
assert_eq!(uri, "http://example.com/?alpha=%CE%B1");
# }
# Ok::<_, Error>(())
fn try_into_uri(self: Self) -> Result<UriString, IriString>

Converts an IRI into a URI without modification, if possible.

Examples

# use iri_string::validate::Error;
use iri_string::types::{IriString, UriString};

let ascii_iri = IriString::try_from("http://example.com/?alpha=%CE%B1")?;
assert_eq!(
    ascii_iri.try_into_uri().map(|uri| uri.to_string()),
    Ok("http://example.com/?alpha=%CE%B1".to_string())
);

let nonascii_iri = IriString::try_from("http://example.com/?alpha=\u{03B1}")?;
assert_eq!(
    nonascii_iri.try_into_uri().map_err(|iri| iri.to_string()),
    Err("http://example.com/?alpha=\u{03B1}".to_string())
);
# Ok::<_, Error>(())

impl<S: Spec> RiString<S>

fn into_absolute_and_fragment(self: Self) -> (RiAbsoluteString<S>, Option<RiFragmentString<S>>)

Splits the IRI into an absolute IRI part and a fragment part.

A leading # character is truncated if the fragment part exists.

Examples

use std::convert::TryFrom;
# use iri_string::{spec::IriSpec, types::{IriFragmentString, IriString}, validate::Error};
let iri = "foo://bar/baz?qux=quux#corge".parse::<IriString>()?;
let (absolute, fragment) = iri.into_absolute_and_fragment();
let fragment_expected = IriFragmentString::try_from("corge".to_owned())
    .map_err(|e| e.validation_error())?;
assert_eq!(absolute, "foo://bar/baz?qux=quux");
assert_eq!(fragment, Some(fragment_expected));
# Ok::<_, Error>(())

use std::convert::TryFrom;
# use iri_string::{spec::IriSpec, types::{IriFragmentString, IriString}, validate::Error};
let iri = "foo://bar/baz?qux=quux#".parse::<IriString>()?;
let (absolute, fragment) = iri.into_absolute_and_fragment();
let fragment_expected = IriFragmentString::try_from("".to_owned())
    .map_err(|e| e.validation_error())?;
assert_eq!(absolute, "foo://bar/baz?qux=quux");
assert_eq!(fragment, Some(fragment_expected));
# Ok::<_, Error>(())
use std::convert::TryFrom;
# use iri_string::{spec::IriSpec, types::IriString, validate::Error};
let iri = "foo://bar/baz?qux=quux".parse::<IriString>()?;
let (absolute, fragment) = iri.into_absolute_and_fragment();
assert_eq!(absolute, "foo://bar/baz?qux=quux");
assert_eq!(fragment, None);
# Ok::<_, Error>(())
fn into_absolute(self: Self) -> RiAbsoluteString<S>

Strips the fragment part if exists, and returns an RiAbsoluteString.

Examples

# use iri_string::{spec::IriSpec, types::IriString, validate::Error};
let iri = "foo://bar/baz?qux=quux#corge".parse::<IriString>()?;
assert_eq!(iri.into_absolute(), "foo://bar/baz?qux=quux");
# Ok::<_, Error>(())
# use iri_string::{spec::IriSpec, types::IriString, validate::Error};
let iri = "foo://bar/baz?qux=quux".parse::<IriString>()?;
assert_eq!(iri.into_absolute(), "foo://bar/baz?qux=quux");
# Ok::<_, Error>(())
fn set_fragment(self: &mut Self, fragment: Option<&RiFragmentStr<S>>)

Sets the fragment part to the given string.

Removes fragment part (and following # character) if None is given.

fn remove_password_inline(self: &mut Self)

Removes the password completely (including separator colon) from self even if it is empty.

Examples

# use iri_string::validate::Error;
# #[cfg(feature = "alloc")] {
use iri_string::types::IriString;

let mut iri = IriString::try_from("http://user:password@example.com/path?query")?;
iri.remove_password_inline();
assert_eq!(iri, "http://user@example.com/path?query");
# }
# Ok::<_, Error>(())

Even if the password is empty, the password and separator will be removed.

# use iri_string::validate::Error;
# #[cfg(feature = "alloc")] {
use iri_string::types::IriString;

let mut iri = IriString::try_from("http://user:@example.com/path?query")?;
iri.remove_password_inline();
assert_eq!(iri, "http://user@example.com/path?query");
# }
# Ok::<_, Error>(())
fn remove_nonempty_password_inline(self: &mut Self)

Replaces the non-empty password in self to the empty password.

This leaves the separator colon if the password part was available.

Examples

# use iri_string::validate::Error;
# #[cfg(feature = "alloc")] {
use iri_string::types::IriString;

let mut iri = IriString::try_from("http://user:password@example.com/path?query")?;
iri.remove_nonempty_password_inline();
assert_eq!(iri, "http://user:@example.com/path?query");
# }
# Ok::<_, Error>(())

If the password is empty, it is left as is.

# use iri_string::validate::Error;
# #[cfg(feature = "alloc")] {
use iri_string::types::IriString;

let mut iri = IriString::try_from("http://user:@example.com/path?query")?;
iri.remove_nonempty_password_inline();
assert_eq!(iri, "http://user:@example.com/path?query");
# }
# Ok::<_, Error>(())

impl<S: crate::spec::Spec> RiString<S>

unsafe fn new_unchecked(s: String) -> Self

Creates a new string without validation.

This does not validate the given string, so it is caller's responsibility to ensure the given string is valid.

Safety

The given string must be syntactically valid as Self type. If not, any use of the returned value or the call of this function itself may result in undefined behavior.

fn shrink_to_fit(self: &mut Self)

Shrinks the capacity of the inner buffer to match its length.

fn capacity(self: &Self) -> usize

Returns the internal buffer capacity in bytes.

fn as_slice(self: &Self) -> &RiStr<S>

Returns the borrowed IRI string slice.

This is equivalent to &*self.

impl<P, T> Receiver for RiString<S>

impl<S> Freeze for RiString<S>

impl<S> RefUnwindSafe for RiString<S>

impl<S> Send for RiString<S>

impl<S> Sync for RiString<S>

impl<S> Unpin for RiString<S>

impl<S> UnsafeUnpin for RiString<S>

impl<S> UnwindSafe for RiString<S>

impl<S: Spec> From for RiString<S>

fn from(v: &Normalized<'_, RiStr<S>>) -> Self

impl<S: Spec> From for RiString<S>

fn from(v: Normalized<'_, RiStr<S>>) -> Self

impl<S: Spec> From for RiString<S>

fn from(builder: &Built<'_, RiStr<S>>) -> Self

impl<S: Spec> From for RiString<S>

fn from(builder: Built<'_, RiStr<S>>) -> Self

impl<S: Spec, C: Context> TryFrom for RiString<S>

fn try_from(v: Expanded<'_, S, C>) -> Result<Self, <Self as >::Error>

impl<S: crate::spec::Spec> AsRef for RiString<S>

fn as_ref(self: &Self) -> &RiStr<S>

impl<S: crate::spec::Spec> AsRef for RiString<S>

fn as_ref(self: &Self) -> &str

impl<S: crate::spec::Spec> AsRef for RiString<S>

fn as_ref(self: &Self) -> &RiReferenceStr<S>

impl<S: crate::spec::Spec> Borrow for RiString<S>

fn borrow(self: &Self) -> &RiStr<S>

impl<S: crate::spec::Spec> Borrow for RiString<S>

fn borrow(self: &Self) -> &str

impl<S: crate::spec::Spec> Clone for RiString<S>

fn clone(self: &Self) -> Self

impl<S: crate::spec::Spec> Debug for RiString<S>

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

impl<S: crate::spec::Spec> Deref for RiString<S>

fn deref(self: &Self) -> &RiStr<S>

impl<S: crate::spec::Spec> Display for RiString<S>

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

impl<S: crate::spec::Spec> Eq for RiString<S>

impl<S: crate::spec::Spec> From for RiString<S>

fn from(s: &RiStr<S>) -> Self

impl<S: crate::spec::Spec> From for RiString<S>

fn from(s: RiAbsoluteString<S>) -> RiString<S>

impl<S: crate::spec::Spec> FromStr for RiString<S>

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

impl<S: crate::spec::Spec> Hash for RiString<S>

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

impl<S: crate::spec::Spec> Ord for RiString<S>

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

impl<S: crate::spec::Spec> PartialEq for RiString<S>

fn eq(self: &Self, o: &String) -> bool

impl<S: crate::spec::Spec> PartialEq for RiString<S>

fn eq(self: &Self, o: &Cow<'_, str>) -> bool

impl<S: crate::spec::Spec> PartialEq for RiString<S>

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

impl<S: crate::spec::Spec> PartialEq for RiString<S>

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

impl<S: crate::spec::Spec> PartialOrd for RiString<S>

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

impl<S: crate::spec::Spec> PartialOrd for RiString<S>

fn partial_cmp(self: &Self, o: &Cow<'_, str>) -> Option<Ordering>

impl<S: crate::spec::Spec> PartialOrd for RiString<S>

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

impl<S: crate::spec::Spec> PartialOrd for RiString<S>

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

impl<S: crate::spec::Spec> TryFrom for RiString<S>

fn try_from(s: RiReferenceString<S>) -> Result<Self, <Self as >::Error>

impl<S: crate::spec::Spec> TryFrom for RiString<S>

fn try_from(s: &str) -> Result<Self, <Self as >::Error>

impl<S: crate::spec::Spec> TryFrom for RiString<S>

fn try_from(bytes: &[u8]) -> Result<Self, <Self as >::Error>

impl<S: crate::spec::Spec> TryFrom for RiString<S>

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

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiString<S>

fn eq(self: &Self, o: &RiReferenceString<T>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiString<S>

fn eq(self: &Self, o: &Cow<'_, RiReferenceStr<T>>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiString<S>

fn eq(self: &Self, o: &&RiReferenceStr<T>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiString<S>

fn eq(self: &Self, o: &RiReferenceStr<T>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiString<S>

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

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiString<T>

fn eq(self: &Self, o: &Cow<'_, RiAbsoluteStr<S>>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiString<T>

fn eq(self: &Self, o: &Cow<'_, RiStr<S>>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiString<T>

fn eq(self: &Self, o: &RiAbsoluteString<S>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiString<T>

fn eq(self: &Self, o: &&RiAbsoluteStr<S>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiString<T>

fn eq(self: &Self, o: &&RiStr<S>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiString<T>

fn eq(self: &Self, o: &RiAbsoluteStr<S>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialEq for RiString<T>

fn eq(self: &Self, o: &RiStr<S>) -> bool

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiString<S>

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

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiString<S>

fn partial_cmp(self: &Self, o: &RiReferenceString<T>) -> Option<Ordering>

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiString<S>

fn partial_cmp(self: &Self, o: &Cow<'_, RiReferenceStr<T>>) -> Option<Ordering>

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiString<S>

fn partial_cmp(self: &Self, o: &&RiReferenceStr<T>) -> Option<Ordering>

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiString<S>

fn partial_cmp(self: &Self, o: &RiReferenceStr<T>) -> Option<Ordering>

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiString<T>

fn partial_cmp(self: &Self, o: &&RiAbsoluteStr<S>) -> Option<Ordering>

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiString<T>

fn partial_cmp(self: &Self, o: &&RiStr<S>) -> Option<Ordering>

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiString<T>

fn partial_cmp(self: &Self, o: &RiAbsoluteStr<S>) -> Option<Ordering>

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiString<T>

fn partial_cmp(self: &Self, o: &RiStr<S>) -> Option<Ordering>

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiString<T>

fn partial_cmp(self: &Self, o: &Cow<'_, RiAbsoluteStr<S>>) -> Option<Ordering>

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiString<T>

fn partial_cmp(self: &Self, o: &Cow<'_, RiStr<S>>) -> Option<Ordering>

impl<S: crate::spec::Spec, T: crate::spec::Spec> PartialOrd for RiString<T>

fn partial_cmp(self: &Self, o: &RiAbsoluteString<S>) -> Option<Ordering>

impl<T> Any for RiString<S>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for RiString<S>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for RiString<S>

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

impl<T> CloneToUninit for RiString<S>

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for RiString<S>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for RiString<S>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> ToString for RiString<S>

fn to_string(self: &Self) -> String

impl<T> ToStringFallible for RiString<S>

fn try_to_string(self: &Self) -> Result<String, TryReserveError>

[ToString::to_string]alloc::string::ToString::to_string, but without panic on OOM.

impl<T, U> Into for RiString<S>

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for RiString<S>

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

impl<T, U> TryInto for RiString<S>

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