Struct PctString

Source
pub struct PctString(/* private fields */);
Expand description

Owned, mutable percent-encoded string.

This is the equivalent of String for percent-encoded strings. It implements Deref to PctStr meaning that all methods on PctStr slices are available on PctString values as well.

Implementations§

Source§

impl PctString

Source

pub fn new<B: Into<Vec<u8>>>( bytes: B, ) -> Result<Self, InvalidPctString<Vec<u8>>>

Create a new owned percent-encoded string.

The input string is checked for correct percent-encoding. If the test fails, a InvalidPctString error is returned.

Source

pub fn from_string(string: String) -> Result<Self, InvalidPctString<String>>

Examples found in repository?
examples/string.rs (line 9)
5fn main() -> Result<(), InvalidPctString<String>> {
6	// [`PctString`] is the equivalent of [`String`] for
7	// percent-encoded strings.
8	// The data is owned by `pct_string`.
9	let pct_string = PctString::from_string("Hello%20World%21".to_string())?;
10
11	// You can compare percent-encoded strings with a regular string.
12	assert!(pct_string == "Hello World!");
13
14	// The underlying string is percent-encoded.
15	assert!(pct_string.as_str() == "Hello%20World%21");
16
17	// You can get a reference to the string as a [`PctStr`].
18	assert!(
19		pct_string.as_pct_str()
20			== PctStr::new("Hello%20World%21").map_err(InvalidPctString::into_owned)?
21	);
22
23	// Just as a regular string, you can iterate over the
24	// encoded characters of `pct_str` with [`PctString::chars`].
25	for c in pct_string.chars() {
26		println!("{}", c);
27	}
28
29	// You can decode the string and every remove percent-encoded characters
30	// with the [`PctStr::decode`] method.
31	let decoded_string: String = pct_string.decode();
32	println!("{}", decoded_string);
33
34	Ok(())
35}
Source

pub unsafe fn new_unchecked<B: Into<Vec<u8>>>(bytes: B) -> Self

Creates a new owned percent-encoded string without validation.

§Safety

The input string must be correctly percent-encoded.

Source

pub fn encode<E: Encoder>( src: impl Iterator<Item = char>, encoder: E, ) -> PctString

Encode a string into a percent-encoded string.

This function takes an Encoder instance to decide which character of the string must be encoded.

Note that the character % will always be encoded regardless of the provided Encoder.

§Example
use pct_str::{PctString, URIReserved};

let pct_string = PctString::encode("Hello World!".chars(), URIReserved);
println!("{}", pct_string.as_str()); // => Hello World%21
Examples found in repository?
examples/encode.rs (line 18)
13fn main() {
14	// You can encode any string into a percent-encoded string
15	// using the [`PctString::encode`] function.
16	// It takes a `char` iterator and a [`Encoder`] instance deciding which
17	// characters to encode.
18	let pct_string = PctString::encode("Hello World!".chars(), URIReserved);
19	// [`URIReserved`] is a predefined encoder for URI-reserved characters.
20	assert_eq!(pct_string.as_str(), "Hello World%21");
21
22	// You can create your own encoder by implementing the [`Encoder`] trait.
23	let pct_string = PctString::encode("Hello World!".chars(), CustomEncoder);
24	println!("{}", pct_string.as_str());
25	assert_eq!(pct_string.as_str(), "%48ello %57orld%21");
26
27	// You can also use any function implementing `Fn(char) -> bool`.
28	let pct_string = PctString::encode("Hello World!".chars(), char::is_uppercase);
29	assert_eq!(pct_string.as_str(), "%48ello %57orld!");
30}
Source

pub fn as_pct_str(&self) -> &PctStr

Return this string as a borrowed percent-encoded string slice.

Examples found in repository?
examples/string.rs (line 19)
5fn main() -> Result<(), InvalidPctString<String>> {
6	// [`PctString`] is the equivalent of [`String`] for
7	// percent-encoded strings.
8	// The data is owned by `pct_string`.
9	let pct_string = PctString::from_string("Hello%20World%21".to_string())?;
10
11	// You can compare percent-encoded strings with a regular string.
12	assert!(pct_string == "Hello World!");
13
14	// The underlying string is percent-encoded.
15	assert!(pct_string.as_str() == "Hello%20World%21");
16
17	// You can get a reference to the string as a [`PctStr`].
18	assert!(
19		pct_string.as_pct_str()
20			== PctStr::new("Hello%20World%21").map_err(InvalidPctString::into_owned)?
21	);
22
23	// Just as a regular string, you can iterate over the
24	// encoded characters of `pct_str` with [`PctString::chars`].
25	for c in pct_string.chars() {
26		println!("{}", c);
27	}
28
29	// You can decode the string and every remove percent-encoded characters
30	// with the [`PctStr::decode`] method.
31	let decoded_string: String = pct_string.decode();
32	println!("{}", decoded_string);
33
34	Ok(())
35}
Source

pub fn into_string(self) -> String

Return the internal string of the PctString, consuming it

Source

pub fn into_bytes(self) -> Vec<u8>

Methods from Deref<Target = PctStr>§

Source

pub fn len(&self) -> usize

Length of the decoded string (character count).

Computed in linear time. This is different from the byte length, which can be retrieved using value.as_bytes().len().

Source

pub fn is_empty(&self) -> bool

Checks if the string is empty.

Source

pub fn as_bytes(&self) -> &[u8]

Returns the underlying percent-encoding bytes.

Source

pub fn as_str(&self) -> &str

Get the underlying percent-encoded string slice.

Examples found in repository?
examples/encode.rs (line 20)
13fn main() {
14	// You can encode any string into a percent-encoded string
15	// using the [`PctString::encode`] function.
16	// It takes a `char` iterator and a [`Encoder`] instance deciding which
17	// characters to encode.
18	let pct_string = PctString::encode("Hello World!".chars(), URIReserved);
19	// [`URIReserved`] is a predefined encoder for URI-reserved characters.
20	assert_eq!(pct_string.as_str(), "Hello World%21");
21
22	// You can create your own encoder by implementing the [`Encoder`] trait.
23	let pct_string = PctString::encode("Hello World!".chars(), CustomEncoder);
24	println!("{}", pct_string.as_str());
25	assert_eq!(pct_string.as_str(), "%48ello %57orld%21");
26
27	// You can also use any function implementing `Fn(char) -> bool`.
28	let pct_string = PctString::encode("Hello World!".chars(), char::is_uppercase);
29	assert_eq!(pct_string.as_str(), "%48ello %57orld!");
30}
More examples
Hide additional examples
examples/str.rs (line 16)
5fn main() -> Result<(), InvalidPctString<&'static str>> {
6	// [`PctStr`] is the equivalent of [`str`] for percent-encoded strings.
7	let buffer = "Hello%20World%21";
8	// It is just a reference to `buffer`.
9	// It can fail if `buffer` is not a valid percent-encoded string.
10	let pct_str = PctStr::new(buffer)?;
11
12	// You can compare percent-encoded strings with a regular string.
13	assert!(pct_str == "Hello World!"); // => true
14
15	// The underlying string is unchanged.
16	assert!(pct_str.as_str() == "Hello%20World%21"); // => true
17
18	// Just as a regular string, you can iterate over the
19	// encoded characters of `pct_str` with [`PctStr::chars`].
20	for c in pct_str.chars() {
21		print!("{}", c);
22	}
23	// => Hello World!
24
25	println!("");
26
27	// You can decode the string and every remove percent-encoded characters
28	// with the [`PctStr::decode`] method.
29	let decoded_string: String = pct_str.decode();
30	println!("{}", decoded_string);
31	// => Hello World!
32
33	Ok(())
34}
examples/string.rs (line 15)
5fn main() -> Result<(), InvalidPctString<String>> {
6	// [`PctString`] is the equivalent of [`String`] for
7	// percent-encoded strings.
8	// The data is owned by `pct_string`.
9	let pct_string = PctString::from_string("Hello%20World%21".to_string())?;
10
11	// You can compare percent-encoded strings with a regular string.
12	assert!(pct_string == "Hello World!");
13
14	// The underlying string is percent-encoded.
15	assert!(pct_string.as_str() == "Hello%20World%21");
16
17	// You can get a reference to the string as a [`PctStr`].
18	assert!(
19		pct_string.as_pct_str()
20			== PctStr::new("Hello%20World%21").map_err(InvalidPctString::into_owned)?
21	);
22
23	// Just as a regular string, you can iterate over the
24	// encoded characters of `pct_str` with [`PctString::chars`].
25	for c in pct_string.chars() {
26		println!("{}", c);
27	}
28
29	// You can decode the string and every remove percent-encoded characters
30	// with the [`PctStr::decode`] method.
31	let decoded_string: String = pct_string.decode();
32	println!("{}", decoded_string);
33
34	Ok(())
35}
Source

pub fn chars(&self) -> Chars<'_>

Iterate over the encoded characters of the string.

Examples found in repository?
examples/str.rs (line 20)
5fn main() -> Result<(), InvalidPctString<&'static str>> {
6	// [`PctStr`] is the equivalent of [`str`] for percent-encoded strings.
7	let buffer = "Hello%20World%21";
8	// It is just a reference to `buffer`.
9	// It can fail if `buffer` is not a valid percent-encoded string.
10	let pct_str = PctStr::new(buffer)?;
11
12	// You can compare percent-encoded strings with a regular string.
13	assert!(pct_str == "Hello World!"); // => true
14
15	// The underlying string is unchanged.
16	assert!(pct_str.as_str() == "Hello%20World%21"); // => true
17
18	// Just as a regular string, you can iterate over the
19	// encoded characters of `pct_str` with [`PctStr::chars`].
20	for c in pct_str.chars() {
21		print!("{}", c);
22	}
23	// => Hello World!
24
25	println!("");
26
27	// You can decode the string and every remove percent-encoded characters
28	// with the [`PctStr::decode`] method.
29	let decoded_string: String = pct_str.decode();
30	println!("{}", decoded_string);
31	// => Hello World!
32
33	Ok(())
34}
More examples
Hide additional examples
examples/string.rs (line 25)
5fn main() -> Result<(), InvalidPctString<String>> {
6	// [`PctString`] is the equivalent of [`String`] for
7	// percent-encoded strings.
8	// The data is owned by `pct_string`.
9	let pct_string = PctString::from_string("Hello%20World%21".to_string())?;
10
11	// You can compare percent-encoded strings with a regular string.
12	assert!(pct_string == "Hello World!");
13
14	// The underlying string is percent-encoded.
15	assert!(pct_string.as_str() == "Hello%20World%21");
16
17	// You can get a reference to the string as a [`PctStr`].
18	assert!(
19		pct_string.as_pct_str()
20			== PctStr::new("Hello%20World%21").map_err(InvalidPctString::into_owned)?
21	);
22
23	// Just as a regular string, you can iterate over the
24	// encoded characters of `pct_str` with [`PctString::chars`].
25	for c in pct_string.chars() {
26		println!("{}", c);
27	}
28
29	// You can decode the string and every remove percent-encoded characters
30	// with the [`PctStr::decode`] method.
31	let decoded_string: String = pct_string.decode();
32	println!("{}", decoded_string);
33
34	Ok(())
35}
Source

pub fn bytes(&self) -> Bytes<'_>

Iterate over the encoded bytes of the string.

Source

pub fn decode(&self) -> String

Decoding.

Return the string with the percent-encoded characters decoded.

Examples found in repository?
examples/str.rs (line 29)
5fn main() -> Result<(), InvalidPctString<&'static str>> {
6	// [`PctStr`] is the equivalent of [`str`] for percent-encoded strings.
7	let buffer = "Hello%20World%21";
8	// It is just a reference to `buffer`.
9	// It can fail if `buffer` is not a valid percent-encoded string.
10	let pct_str = PctStr::new(buffer)?;
11
12	// You can compare percent-encoded strings with a regular string.
13	assert!(pct_str == "Hello World!"); // => true
14
15	// The underlying string is unchanged.
16	assert!(pct_str.as_str() == "Hello%20World%21"); // => true
17
18	// Just as a regular string, you can iterate over the
19	// encoded characters of `pct_str` with [`PctStr::chars`].
20	for c in pct_str.chars() {
21		print!("{}", c);
22	}
23	// => Hello World!
24
25	println!("");
26
27	// You can decode the string and every remove percent-encoded characters
28	// with the [`PctStr::decode`] method.
29	let decoded_string: String = pct_str.decode();
30	println!("{}", decoded_string);
31	// => Hello World!
32
33	Ok(())
34}
More examples
Hide additional examples
examples/string.rs (line 31)
5fn main() -> Result<(), InvalidPctString<String>> {
6	// [`PctString`] is the equivalent of [`String`] for
7	// percent-encoded strings.
8	// The data is owned by `pct_string`.
9	let pct_string = PctString::from_string("Hello%20World%21".to_string())?;
10
11	// You can compare percent-encoded strings with a regular string.
12	assert!(pct_string == "Hello World!");
13
14	// The underlying string is percent-encoded.
15	assert!(pct_string.as_str() == "Hello%20World%21");
16
17	// You can get a reference to the string as a [`PctStr`].
18	assert!(
19		pct_string.as_pct_str()
20			== PctStr::new("Hello%20World%21").map_err(InvalidPctString::into_owned)?
21	);
22
23	// Just as a regular string, you can iterate over the
24	// encoded characters of `pct_str` with [`PctString::chars`].
25	for c in pct_string.chars() {
26		println!("{}", c);
27	}
28
29	// You can decode the string and every remove percent-encoded characters
30	// with the [`PctStr::decode`] method.
31	let decoded_string: String = pct_string.decode();
32	println!("{}", decoded_string);
33
34	Ok(())
35}

Trait Implementations§

Source§

impl AsRef<[u8]> for PctString

Source§

fn as_ref(&self) -> &[u8]

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

impl AsRef<PctStr> for PctString

Source§

fn as_ref(&self) -> &PctStr

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

impl AsRef<str> for PctString

Source§

fn as_ref(&self) -> &str

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

impl Borrow<[u8]> for PctString

Source§

fn borrow(&self) -> &[u8]

Immutably borrows from an owned value. Read more
Source§

impl Borrow<PctStr> for PctString

Source§

fn borrow(&self) -> &PctStr

Immutably borrows from an owned value. Read more
Source§

impl Borrow<str> for PctString

Source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
Source§

impl Debug for PctString

Source§

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

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

impl Deref for PctString

Source§

type Target = PctStr

The resulting type after dereferencing.
Source§

fn deref(&self) -> &PctStr

Dereferences the value.
Source§

impl Display for PctString

Source§

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

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

impl FromStr for PctString

Source§

type Err = InvalidPctString<String>

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

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

Parses a string s to return a value of this type. Read more
Source§

impl Hash for PctString

Source§

fn hash<H: Hasher>(&self, hasher: &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 PartialEq<&str> for PctString

Source§

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

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

const 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 PartialEq<PctStr> for PctString

Source§

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

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

const 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 PartialEq<PctString> for PctStr

Source§

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

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

const 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 PartialEq<str> for PctString

Source§

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

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

const 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 PartialEq for PctString

Source§

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

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

const 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 PartialOrd<PctStr> for PctString

Source§

fn partial_cmp(&self, other: &PctStr) -> 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 PartialOrd<PctString> for PctStr

Source§

fn partial_cmp(&self, other: &PctString) -> 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 PartialOrd for PctString

Source§

fn partial_cmp(&self, other: &PctString) -> 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<'a> TryFrom<&'a str> for PctString

Source§

type Error = InvalidPctString<String>

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

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

Performs the conversion.
Source§

impl TryFrom<String> for PctString

Source§

type Error = InvalidPctString<String>

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

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

Performs the conversion.
Source§

impl Eq for PctString

Auto Trait Implementations§

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> 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

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

Source§

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.