iri_string::percent_encode

Struct PercentEncoded

source
pub struct PercentEncoded<T, S> { /* private fields */ }
Expand description

A proxy to percent-encode a string.

Type aliases PercentEncodedForIri and PercentEncodedForUri are provided. You can use them to make the expression simpler, for example write PercentEncodedForUri::from_path(foo) instead of PercentEncoded::<_, UriSpec>::from_path(foo).

Implementations§

source§

impl<T: Display, S: Spec> PercentEncoded<T, S>

source

pub fn from_reg_name(raw: T) -> Self

Creates an encoded string from a raw reg-name (i.e. hostname or domain).

§Examples
use iri_string::percent_encode::PercentEncoded;
use iri_string::spec::UriSpec;

let raw = "alpha.\u{03B1}.example.com";
let encoded = "alpha.%CE%B1.example.com";
assert_eq!(
    PercentEncoded::<_, UriSpec>::from_reg_name(raw).to_string(),
    encoded
);
source

pub fn from_user(raw: T) -> Self

Creates an encoded string from a raw user name (inside userinfo component).

§Examples
use iri_string::percent_encode::PercentEncoded;
use iri_string::spec::UriSpec;

let raw = "user:\u{03B1}";
// The first `:` will be interpreted as a delimiter, so colons will be escaped.
let encoded = "user%3A%CE%B1";
assert_eq!(
    PercentEncoded::<_, UriSpec>::from_user(raw).to_string(),
    encoded
);
source

pub fn from_password(raw: T) -> Self

Creates an encoded string from a raw user name (inside userinfo component).

§Examples
use iri_string::percent_encode::PercentEncoded;
use iri_string::spec::UriSpec;

let raw = "password:\u{03B1}";
// The first `:` will be interpreted as a delimiter, and the colon
// inside the password will be the first one if the user name is empty,
// so colons will be escaped.
let encoded = "password%3A%CE%B1";
assert_eq!(
    PercentEncoded::<_, UriSpec>::from_password(raw).to_string(),
    encoded
);
source

pub fn from_path_segment(raw: T) -> Self

Creates an encoded string from a raw path segment.

§Examples
use iri_string::percent_encode::PercentEncoded;
use iri_string::spec::UriSpec;

let raw = "alpha/\u{03B1}?#";
// Note that `/` is encoded to `%2F`.
let encoded = "alpha%2F%CE%B1%3F%23";
assert_eq!(
    PercentEncoded::<_, UriSpec>::from_path_segment(raw).to_string(),
    encoded
);
source

pub fn from_path(raw: T) -> Self

Creates an encoded string from a raw path.

§Examples
use iri_string::percent_encode::PercentEncoded;
use iri_string::spec::UriSpec;

let raw = "alpha/\u{03B1}?#";
// Note that `/` is NOT percent encoded.
let encoded = "alpha/%CE%B1%3F%23";
assert_eq!(
    PercentEncoded::<_, UriSpec>::from_path(raw).to_string(),
    encoded
);
source

pub fn from_query(raw: T) -> Self

Creates an encoded string from a raw query.

§Examples
use iri_string::percent_encode::PercentEncoded;
use iri_string::spec::UriSpec;

let raw = "alpha/\u{03B1}?#";
let encoded = "alpha/%CE%B1?%23";
assert_eq!(
    PercentEncoded::<_, UriSpec>::from_query(raw).to_string(),
    encoded
);
source

pub fn from_fragment(raw: T) -> Self

Creates an encoded string from a raw fragment.

§Examples
use iri_string::percent_encode::PercentEncoded;
use iri_string::spec::UriSpec;

let raw = "alpha/\u{03B1}?#";
let encoded = "alpha/%CE%B1?%23";
assert_eq!(
    PercentEncoded::<_, UriSpec>::from_fragment(raw).to_string(),
    encoded
);
source

pub fn unreserve(raw: T) -> Self

Creates a string consists of only unreserved string and percent-encoded triplets.

§Examples
use iri_string::percent_encode::PercentEncoded;
use iri_string::spec::UriSpec;

let unreserved = "%a0-._~\u{03B1}";
let unreserved_encoded = "%25a0-._~%CE%B1";
assert_eq!(
    PercentEncoded::<_, UriSpec>::unreserve(unreserved).to_string(),
    unreserved_encoded
);

let reserved = ":/?#[]@ !$&'()*+,;=";
let reserved_encoded =
    "%3A%2F%3F%23%5B%5D%40%20%21%24%26%27%28%29%2A%2B%2C%3B%3D";
assert_eq!(
    PercentEncoded::<_, UriSpec>::unreserve(reserved).to_string(),
    reserved_encoded
);
source

pub fn characters(raw: T) -> Self

Percent-encodes characters only if they cannot appear anywhere in an IRI reference.

% character will be always encoded. In other words, this conversion is not aware of percent-encoded triplets.

Note that this encoding process does not guarantee that the resulting string is a valid IRI reference.

§Examples
use iri_string::percent_encode::PercentEncoded;
use iri_string::spec::UriSpec;

let unreserved = "%a0-._~\u{03B1}";
let unreserved_encoded = "%25a0-._~%CE%B1";
assert_eq!(
    PercentEncoded::<_, UriSpec>::characters(unreserved).to_string(),
    unreserved_encoded
);

let reserved = ":/?#[]@ !$&'()*+,;=";
// Note that `%20` cannot appear directly in an IRI reference.
let expected = ":/?#[]@%20!$&'()*+,;=";
assert_eq!(
    PercentEncoded::<_, UriSpec>::characters(reserved).to_string(),
    expected
);

Trait Implementations§

source§

impl<T: Clone, S: Clone> Clone for PercentEncoded<T, S>

source§

fn clone(&self) -> PercentEncoded<T, S>

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: Debug, S: Debug> Debug for PercentEncoded<T, S>

source§

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

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

impl<T: Display, S: Spec> Display for PercentEncoded<T, S>

source§

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

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

impl<T: Copy, S: Copy> Copy for PercentEncoded<T, S>

Auto Trait Implementations§

§

impl<T, S> Freeze for PercentEncoded<T, S>
where T: Freeze,

§

impl<T, S> RefUnwindSafe for PercentEncoded<T, S>
where T: RefUnwindSafe,

§

impl<T, S> Send for PercentEncoded<T, S>
where T: Send,

§

impl<T, S> Sync for PercentEncoded<T, S>
where T: Sync,

§

impl<T, S> Unpin for PercentEncoded<T, S>
where T: Unpin,

§

impl<T, S> UnwindSafe for PercentEncoded<T, S>
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> ToStringFallible for T
where T: Display,

source§

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

Available on crate feature alloc only.

ToString::to_string, but without panic on OOM.

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.