pub struct ExtValue<'a> { /* private fields */ }Expand description
A validated extended value.
§Examples
let v = parse_net::ExtValue::parse(b"UTF-8''na%C3%AFve.txt")?;
let mut buf = [0u8; 16];
assert_eq!(v.decode_into(&mut buf)?, "na\u{ef}ve.txt");Implementations§
Source§impl<'a> ExtValue<'a>
impl<'a> ExtValue<'a>
Sourcepub fn parse(value: &'a [u8]) -> Result<Self, ParseError>
pub fn parse(value: &'a [u8]) -> Result<Self, ParseError>
Parses an extended value.
The charset is UTF-8 or ISO-8859-1 (case-insensitive); the
language is empty or letters, digits, and -; the value is
attr-char bytes and %XX triples.
§Errors
ErrorKind::InvalidCharset (no ', an empty or malformed charset,
a malformed language); ErrorKind::UnsupportedCharset;
ErrorKind::InvalidPercentEncoding; ErrorKind::InvalidByte, or a
control kind, for a byte outside the value grammar.
§Examples
use parse_net::{ErrorKind, ExtValue};
assert!(ExtValue::parse(b"UTF-8''a%20b").is_ok());
assert_eq!(ExtValue::parse(b"UTF-8'a%20b").unwrap_err().kind(), ErrorKind::InvalidCharset);
assert_eq!(ExtValue::parse(b"Shift_JIS''x").unwrap_err().kind(), ErrorKind::UnsupportedCharset);
assert_eq!(ExtValue::parse(b"UTF-8''a%2").unwrap_err().kind(), ErrorKind::InvalidPercentEncoding);Sourcepub const fn charset(&self) -> Charset
pub const fn charset(&self) -> Charset
The declared charset.
§Examples
let v = parse_net::ExtValue::parse(b"utf-8''x")?;
assert_eq!(v.charset(), parse_net::Charset::Utf8);Sourcepub const fn language(&self) -> &'a [u8] ⓘ
pub const fn language(&self) -> &'a [u8] ⓘ
The language tag, possibly empty, as sent.
§Examples
use parse_net::ExtValue;
assert_eq!(ExtValue::parse(b"UTF-8'de-CH'Gr%C3%BCezi")?.language(), b"de-CH");
assert!(ExtValue::parse(b"UTF-8''Gr%C3%BCezi")?.language().is_empty());Sourcepub const fn encoded(&self) -> &'a [u8] ⓘ
pub const fn encoded(&self) -> &'a [u8] ⓘ
The value-chars, still percent-encoded.
§Examples
let v = parse_net::ExtValue::parse(b"UTF-8''a%20b")?;
assert_eq!(v.encoded(), b"a%20b");Sourcepub fn max_decoded_len(&self) -> usize
pub fn max_decoded_len(&self) -> usize
An upper bound on the length decode_into
writes: exact for UTF-8, doubled for ISO-8859-1.
§Examples
let v = parse_net::ExtValue::parse(b"ISO-8859-1''%E9t%E9")?;
assert_eq!(v.max_decoded_len(), 6);
let mut buf = [0u8; 6];
assert_eq!(v.decode_into(&mut buf)?.len(), 5);Sourcepub fn decode_into<'b>(&self, out: &'b mut [u8]) -> Result<&'b str, ParseError>
pub fn decode_into<'b>(&self, out: &'b mut [u8]) -> Result<&'b str, ParseError>
Decodes into out and returns the text: UTF-8 is validated
(overlong forms and surrogates refused), ISO-8859-1 is transcoded.
A decoded control character (C0, DEL, or C1, such as %0D%0A or
%00) is refused: never copy such a value into another header or a
log.
§Errors
ErrorKind::InvalidUtf8, ErrorKind::ControlInDecoded,
ErrorKind::BufferTooSmall. Nothing is written on error.
§Examples
use parse_net::{ErrorKind, ExtValue};
let mut buf = [0u8; 8];
assert_eq!(ExtValue::parse(b"ISO-8859-1''%A3%205")?.decode_into(&mut buf)?, "\u{a3} 5");
let crlf = ExtValue::parse(b"UTF-8''a%0D%0Ab")?;
assert_eq!(crlf.decode_into(&mut buf).unwrap_err().kind(), ErrorKind::ControlInDecoded);