Skip to main content

decode_phrase

Function decode_phrase 

Source
pub fn decode_phrase<'b>(
    value: &[u8],
    limits: &Limits,
    out: &'b mut [u8],
) -> Result<&'b str, ParseError>
Expand description

Decodes a phrase (RFC 5322 section 3.2.5), such as the display name parse_mailbox returns raw, into out and returns its text: comments are dropped, quoted strings lose their quotes and their quoted pairs are unescaped, and the words are joined by one space. RFC 2047 encoded words are decoded where section 5 allows them in a phrase: a whole atom between whitespace, never inside a quoted string; two encoded words with only whitespace between them are joined with none (section 6.2). Encoded words are read as decode_words reads them, and every byte of the text meets its rules: UTF-8, no control character but HTAB.

§Errors

ErrorKind::Empty for a phrase with no word (empty, or only comments and whitespace); ErrorKind::InvalidByte for a special such as , or < outside a quoted string or comment; a control kind for a control byte, other than HTAB and the CRLF of a fold; ErrorKind::Control for a C1 character; ErrorKind::InvalidUtf8; ErrorKind::UnterminatedQuote; ErrorKind::UnbalancedComment, ErrorKind::CommentTooDeep; the encoded-word errors of decode_words, ErrorKind::TooManyMembers beyond max_encoded_words among them; ErrorKind::BufferTooSmall. Nothing is written on error.

§Examples

use parse_net::{mail, Limits};

let m = mail::parse_mailbox(b"\"Doe, Jane\" (Sales) <jane@example.com>", &Limits::DEFAULT)?;
let mut buf = [0u8; 64];
let name = mail::decode_phrase(m.display_name.unwrap_or_default(), &Limits::DEFAULT, &mut buf)?;
assert_eq!(name, "Doe, Jane");

let name = mail::decode_phrase(b"=?UTF-8?Q?Ren=C3=A9e?= \"O'Neil\"", &Limits::DEFAULT, &mut buf)?;
assert_eq!(name, "Ren\u{e9}e O'Neil");