document Header trait, CowHeader and header value encoding

This commit is contained in:
2026-10-10 20:18:36 +02:00
parent a0e14b180b
commit 4860823103
2 changed files with 170 additions and 0 deletions
+21
View File
@@ -28,6 +28,27 @@ fn encode(encode_buf: &mut String, writer: &mut EmailWriter) {
encode_buf.clear();
}
/// Encode `value` as the value of the header named `key`, ready to be written out.
///
/// Walks the value word by word. A word made entirely of allowed bytes is written as it is, with a
/// line break and indentation inserted when the line would grow too long (RFC 5322 folding).
/// Consecutive words containing anything else are collected and RFC 2047-encoded together, so a
/// value mixing plain and non-ASCII text keeps its plain parts readable.
///
/// `key` itself is not part of the result: only its length matters, for the length of the first
/// line.
///
/// # Examples
///
/// ```
/// use email_message::header::encode_header_value;
///
/// // Plain text is left alone.
/// assert_eq!(encode_header_value("Subject", "a test"), "a test");
///
/// // Only the words that need it are encoded.
/// assert_eq!(encode_header_value("Subject", "Grüße"), "=?utf-8?b?R3LDvMOfZQ==?=");
/// ```
pub fn encode_header_value(key: &str, value: &str) -> String {
let mut encoded_value = String::new();
let line_len = key.len() + ": ".len();