From 3cf2748c30b5bab6f7b307a488706e76c3188a28 Mon Sep 17 00:00:00 2001 From: Thomas Oettli Date: Sun, 11 Oct 2026 00:12:42 +0200 Subject: [PATCH] make encode_header_value and default_conditional crate-private --- src/header/encoding.rs | 14 ++------------ src/header/mod.rs | 10 +++++----- src/lib.rs | 8 ++++---- src/tests.rs | 9 +++++++++ 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/header/encoding.rs b/src/header/encoding.rs index 1e72a04..376eec4 100644 --- a/src/header/encoding.rs +++ b/src/header/encoding.rs @@ -38,18 +38,8 @@ fn encode(encode_buf: &mut String, writer: &mut EmailWriter) { /// `key` itself is not part of the result: only its length matters, for the length of the first /// line. /// -/// # Examples -/// -/// ``` -/// use cow_mail::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 { +/// Pinned by `test_encode_header_value` in `src/tests.rs`. +pub(crate) fn encode_header_value(key: &str, value: &str) -> String { let mut encoded_value = String::new(); let line_len = key.len() + ": ".len(); let mut writer = EmailWriter::new(&mut encoded_value, line_len, 0, false); diff --git a/src/header/mod.rs b/src/header/mod.rs index 60a44bb..a74550f 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -11,7 +11,7 @@ mod encoding; mod headers; -pub use encoding::encode_header_value; +pub(crate) use encoding::encode_header_value; pub(crate) use headers::build_headers; pub use headers::{HeaderMap, parse_headers}; @@ -53,8 +53,8 @@ use crate::{CowMailError, offset_in, parse_error}; pub trait Header<'a>: Clone + Display { /// A new header from a key and a value. /// - /// The value is RFC 2047-encoded and folded as needed — see [`encode_header_value`] — so pass - /// it as the text it should decode to, not pre-encoded. + /// The value is RFC 2047-encoded and folded as needed, so pass it as the text it should decode + /// to, not pre-encoded. /// /// # Panics /// @@ -211,8 +211,8 @@ pub trait Header<'a>: Clone + Display { } struct RFC2045HeaderValue { - pub value: T, - pub params: BTreeMap, + value: T, + params: BTreeMap, } impl Display for RFC2045HeaderValue { diff --git a/src/lib.rs b/src/lib.rs index 1e92580..9593da9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -238,7 +238,7 @@ impl ContentType { /// /// `text/plain` normally, but `message/rfc822` for a part of a `multipart/digest`, per RFC /// 2046 section 5.1.5. Pass whether the part sits in such a digest. - pub fn default_conditional(in_multipart_digest: bool) -> Self { + pub(crate) fn default_conditional(in_multipart_digest: bool) -> Self { match in_multipart_digest { true => mime::MESSAGE_RFC822, false => mime::TEXT_PLAIN, @@ -508,8 +508,8 @@ pub trait Mail<'a, T: Header<'a> + 'a>: HeaderMap<'a, T> { /// The content type to assume when the part has no `Content-Type` header. /// - /// Normally `text/plain; charset=us-ascii`, but `message/rfc822` inside a `multipart/digest`. - /// See [`ContentType::default_conditional`]. + /// Normally `text/plain; charset=us-ascii`, but `message/rfc822` inside a `multipart/digest`, + /// per RFC 2046 section 5.1.5. fn default_ctype(&self) -> ContentType; /// The body exactly as it sits in the message, still transfer-encoded. @@ -808,7 +808,7 @@ pub struct CowMail<'a, H: Header<'a>> { /// The content type to assume while this part has no `Content-Type` header. /// /// Set while parsing, since only then is it known whether the part sits in a - /// `multipart/digest`. See [`ContentType::default_conditional`]. + /// `multipart/digest` — which makes it `message/rfc822` rather than `text/plain`. pub default_ctype: ContentType, } diff --git a/src/tests.rs b/src/tests.rs index 55db65f..71271e8 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,4 +1,5 @@ //use std::io::{Read, Write}; +use crate::header::encode_header_value; use crate::{BodyType, CowHeader, CowMail, Header, HeaderMap, Mail, mime}; fn serialize<'a>(mail: &'a CowMail<'a, CowHeader<'a>>) -> Vec { @@ -1215,3 +1216,11 @@ fn test_make_mixed_is_case_insensitive() { assert_eq!(mail.subparts.len(), 1); assert_eq!(String::from_utf8_lossy(&serialize(&mail)), raw); } + +// Was a doc test on `encode_header_value` before it became crate-private. +// A word of allowed bytes is written as it stands; the rest is RFC 2047-encoded. +#[test] +fn test_encode_header_value() { + assert_eq!(encode_header_value("Subject", "a test"), "a test"); + assert_eq!(encode_header_value("Subject", "Grüße"), "=?utf-8?b?R3LDvMOfZQ==?="); +}