Skip to main content

Word

Enum Word 

Source
pub enum Word<'a> {
    Token(&'a [u8]),
    Quoted(Quoted<'a>),
}
Expand description

token / quoted-string: the value form of parameters and directives.

A token and a quoted string with the same content compare equal, as RFC 9110 section 5.6.6 requires: eq_bytes and eq_ignore_ascii_case compare unescaped content. The derived PartialEq compares the forms structurally.

§Examples

use parse_net::text;

let (a, _) = text::parse_word(b"utf-8")?;
let (b, _) = text::parse_word(b"\"UTF-8\"")?;
assert!(a.eq_ignore_ascii_case(b"UTF-8") && b.eq_ignore_ascii_case(b"utf-8"));
assert_eq!(b.as_str(), Some("UTF-8"));

Variants§

§

Token(&'a [u8])

A token.

§

Quoted(Quoted<'a>)

A quoted string.

Implementations§

Source§

impl<'a> Word<'a>

Source

pub const fn as_plain(&self) -> Option<&'a [u8]>

The token, or the quoted content when it has no quoted pair.

§Examples
use parse_net::text::parse_word;

assert_eq!(parse_word(b"gzip")?.0.as_plain(), Some(&b"gzip"[..]));
assert_eq!(parse_word(b"\"a\\\"b\"")?.0.as_plain(), None);
Source

pub fn as_str(&self) -> Option<&'a str>

as_plain when it is valid UTF-8.

§Examples
use parse_net::text::parse_word;

assert_eq!(parse_word(b"\"caf\xc3\xa9\"")?.0.as_str(), Some("caf\u{e9}"));
assert_eq!(parse_word(b"\"\xff\"")?.0.as_str(), None);
Source

pub fn eq_bytes(&self, other: &[u8]) -> bool

Whether the unescaped value equals other.

§Examples
use parse_net::text::parse_word;

assert!(parse_word(b"\"a b\"")?.0.eq_bytes(b"a b"));
assert!(parse_word(b"gzip")?.0.eq_bytes(b"gzip") && !parse_word(b"gzip")?.0.eq_bytes(b"GZIP"));
Source

pub fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool

Whether the unescaped value equals other ignoring ASCII case.

§Examples
use parse_net::text::parse_word;

assert!(parse_word(b"GZIP")?.0.eq_ignore_ascii_case(b"gzip"));
assert!(parse_word(b"\"Br\"")?.0.eq_ignore_ascii_case(b"br"));
Source

pub fn unescape_into<'b>( &self, out: &'b mut [u8], ) -> Result<&'b [u8], ParseError>

Writes the unescaped value into out and returns the written part.

§Errors

ErrorKind::BufferTooSmall; nothing is written.

§Examples
use parse_net::{text::parse_word, ErrorKind};

let (w, _) = parse_word(b"\"a\\\"b\"")?;
let mut buf = [0u8; 8];
assert_eq!(w.unescape_into(&mut buf)?, b"a\"b");
assert_eq!(w.unescape_into(&mut [0u8; 2]).unwrap_err().kind(), ErrorKind::BufferTooSmall);
Source

pub fn len(&self) -> usize

The length of the unescaped value.

§Examples
use parse_net::text::parse_word;

assert_eq!(parse_word(b"\"a\\\"b\"")?.0.len(), 3);
assert_eq!(parse_word(b"gzip")?.0.len(), 4);
Source

pub fn is_empty(&self) -> bool

Whether the unescaped value is empty (only a quoted string can be).

§Examples
use parse_net::text::parse_word;

assert!(parse_word(b"\"\"")?.0.is_empty());
assert!(!parse_word(b"x")?.0.is_empty());

Trait Implementations§

Source§

impl<'a> Clone for Word<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for Word<'a>

Source§

impl<'a> Debug for Word<'a>

Source§

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

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

impl<'a> Eq for Word<'a>

Source§

impl<'a> Hash for Word<'a>

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<'a> PartialEq for Word<'a>

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<'a> StructuralPartialEq for Word<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Word<'a>

§

impl<'a> RefUnwindSafe for Word<'a>

§

impl<'a> Send for Word<'a>

§

impl<'a> Sync for Word<'a>

§

impl<'a> Unpin for Word<'a>

§

impl<'a> UnsafeUnpin for Word<'a>

§

impl<'a> UnwindSafe for Word<'a>

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.