Struct fluent_uri::enc::EStr

source ·
#[repr(transparent)]
pub struct EStr { /* private fields */ }
Expand description

Percent-encoded string slices.

Examples

Parse key-value pairs from a query string into a hash map:

use std::collections::HashMap;
use fluent_uri::enc::EStr;

let query = "name=%E5%BC%A0%E4%B8%89&speech=%C2%A1Ol%C3%A9!";
let map: HashMap<_, _> = EStr::new(query)
    .split('&')
    .filter_map(|pair| pair.split_once('='))
    .map(|(k, v)| (k.decode(), v.decode()))
    .filter_map(|(k, v)| k.into_string().ok().zip(v.into_string().ok()))
    .collect();
assert_eq!(map["name"], "张三");
assert_eq!(map["speech"], "¡Olé!");

Implementations§

source§

impl EStr

source

pub const fn new(s: &str) -> &EStr

Converts a string slice to an EStr.

Panics

Panics if the string is not properly encoded.

source

pub fn as_str(&self) -> &str

Yields the underlying string slice.

source

pub fn decode(&self) -> Decode<'_>

Decodes the EStr.

Examples
use fluent_uri::enc::EStr;

let dec = EStr::new("%C2%BF").decode();
assert_eq!(dec.as_bytes(), &[0xc2, 0xbf]);
assert_eq!(dec.into_string()?, "¿");
source

pub fn split(&self, delim: char) -> Split<'_>

Returns an iterator over subslices of the EStr separated by the given delimiter.

Panics

Panics if the delimiter is not a reserved character.

Examples
use fluent_uri::enc::EStr;

assert!(EStr::new("a,b,c").split(',').eq(["a", "b", "c"]));
assert!(EStr::new(",").split(',').eq(["", ""]));
source

pub fn split_once(&self, delim: char) -> Option<(&EStr, &EStr)>

Splits the EStr on the first occurrence of the given delimiter and returns prefix before delimiter and suffix after delimiter.

Returns None if the delimiter is not found.

Panics

Panics if the delimiter is not a reserved character.

Examples
use fluent_uri::enc::EStr;

let (k, v) = EStr::new("key=value").split_once('=').unwrap();
assert_eq!(k, "key");
assert_eq!(v, "value");

assert!(EStr::new("abc").split_once(';').is_none());

Trait Implementations§

source§

impl AsRef<[u8]> for EStr

source§

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

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

impl AsRef<str> for EStr

source§

fn as_ref(&self) -> &str

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

impl Borrow<str> for &EStr

source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
source§

impl Debug for EStr

source§

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

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

impl Default for &EStr

source§

fn default() -> &'static EStr

Creates an empty EStr.

source§

impl Display for EStr

source§

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

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

impl Hash for EStr

source§

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

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

impl PartialEq<EStr> for EStr

Implements equality comparisons on EStrs.

EStrs are compared by their byte values. Percent-encoding normalization is not performed prior to comparison.

source§

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

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

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

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

impl PartialEq<EStr> for str

source§

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

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

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

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

impl PartialEq<str> for EStr

source§

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

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

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

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

impl Eq for EStr

Auto Trait Implementations§

§

impl RefUnwindSafe for EStr

§

impl Send for EStr

§

impl !Sized for EStr

§

impl Sync for EStr

§

impl Unpin for EStr

§

impl UnwindSafe for EStr

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

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

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more