Skip to main content

eml_codec/
i18n.rs

1// Import the derive macro for ContainsUtf8
2pub use eml_codec_derives::ContainsUtf8;
3
4/// The `contains_utf8` function returns whether a value implementing this trait
5/// uses non-ascii UTF-8 text added by RFC 6532.
6///
7/// This is useful to distinguish between headers bodies or other tokens that
8/// require RFC 6532 and those that do not.
9pub trait ContainsUtf8 {
10    fn contains_utf8(&self) -> bool;
11}
12impl<T: ContainsUtf8> ContainsUtf8 for Option<T> {
13    fn contains_utf8(&self) -> bool {
14        match &self {
15            None => false,
16            Some(x) => x.contains_utf8(),
17        }
18    }
19}
20impl<T: ContainsUtf8> ContainsUtf8 for Box<T> {
21    fn contains_utf8(&self) -> bool {
22        <T as ContainsUtf8>::contains_utf8(self.as_ref())
23    }
24}
25impl<T: ContainsUtf8> ContainsUtf8 for Vec<T> {
26    fn contains_utf8(&self) -> bool {
27        self.iter().any(|x| x.contains_utf8())
28    }
29}
30impl<'a> ContainsUtf8 for std::borrow::Cow<'a, str> {
31    fn contains_utf8(&self) -> bool {
32        self.as_bytes().iter().any(|b| !b.is_ascii())
33    }
34}