diff options
Diffstat (limited to 'headers/src/header_components/unstructured.rs')
-rw-r--r-- | headers/src/header_components/unstructured.rs | 79 |
1 files changed, 35 insertions, 44 deletions
diff --git a/headers/src/header_components/unstructured.rs b/headers/src/header_components/unstructured.rs index 76d1da6..6d88a76 100644 --- a/headers/src/header_components/unstructured.rs +++ b/headers/src/header_components/unstructured.rs @@ -1,16 +1,16 @@ -use std::ops::{ Deref, DerefMut}; use std::fmt::{self, Display}; +use std::ops::{Deref, DerefMut}; use failure::Fail; use soft_ascii_string::SoftAsciiChar; -use internals::grammar::is_vchar; -use internals::error::{EncodingError, EncodingErrorKind}; -use internals::encoder::{EncodingWriter, EncodableInHeader}; +use data::Input; +use error::ComponentCreationError; use internals::bind::encoded_word::{EncodedWordEncoding, WriterWrapper}; -use ::{HeaderTryFrom, HeaderTryInto}; -use ::error::ComponentCreationError; -use ::data::Input; +use internals::encoder::{EncodableInHeader, EncodingWriter}; +use internals::error::{EncodingError, EncodingErrorKind}; +use internals::grammar::is_vchar; +use {HeaderTryFrom, HeaderTryInto}; use super::utils::text_partition::{partition, Partition}; @@ -41,47 +41,40 @@ impl DerefMut for Unstructured { } impl<T> HeaderTryFrom<T> for Unstructured - where T: HeaderTryInto<Input> +where + T: HeaderTryInto<Input>, { fn try_from(text: T) -> Result<Self, ComponentCreationError> { let text = text.try_into()?; - Ok( Unstructured { text }) + Ok(Unstructured { text }) } } - - -impl EncodableInHeader for Unstructured { - +impl EncodableInHeader for Unstructured { fn encode(&self, handle: &mut EncodingWriter) -> Result<(), EncodingError> { let text: &str = &*self.text; if text.len() == 0 { - return Ok( () ) + return Ok(()); } - let partitions = partition(text) - .map_err(|err| EncodingError - ::from(err.context(EncodingErrorKind::Malformed)) - .with_str_context(text) - )?; + let partitions = partition(text).map_err(|err| { + EncodingError::from(err.context(EncodingErrorKind::Malformed)).with_str_context(text) + })?; for block in partitions.into_iter() { match block { - Partition::VCHAR( data ) => { + Partition::VCHAR(data) => { let mail_type = handle.mail_type(); - handle.write_if(data, |s| - s.chars().all(|ch| is_vchar(ch, mail_type)) - ).handle_condition_failure(|handle| { - let encoding = EncodedWordEncoding::QuotedPrintable; - let mut writer = WriterWrapper::new( - encoding, - handle - ); - encoding.encode(data, &mut writer); - Ok(()) - })?; - }, - Partition::SPACE( data ) => { + handle + .write_if(data, |s| s.chars().all(|ch| is_vchar(ch, mail_type))) + .handle_condition_failure(|handle| { + let encoding = EncodedWordEncoding::QuotedPrintable; + let mut writer = WriterWrapper::new(encoding, handle); + encoding.encode(data, &mut writer); + Ok(()) + })?; + } + Partition::SPACE(data) => { let mut had_fws = false; for ch in data.chars() { if ch == '\r' || ch == '\n' { @@ -90,7 +83,7 @@ impl EncodableInHeader for Unstructured { handle.mark_fws_pos(); had_fws = true; } - handle.write_char( SoftAsciiChar::from_unchecked(ch) )?; + handle.write_char(SoftAsciiChar::from_unchecked(ch))?; } if !had_fws { // currently this can only happen if data only consists of '\r','\n' @@ -102,7 +95,6 @@ impl EncodableInHeader for Unstructured { } } } - } Ok(()) } @@ -112,7 +104,6 @@ impl EncodableInHeader for Unstructured { } } - #[cfg(test)] mod test { @@ -128,7 +119,7 @@ mod test { Text " case" ]} - ec_test!{ simple_utf8, { + ec_test! { simple_utf8, { Unstructured::try_from( "thüs sümple case" )? } => utf8 => [ Text "thüs", @@ -138,7 +129,7 @@ mod test { Text " case" ]} - ec_test!{ encoded_words, { + ec_test! { encoded_words, { Unstructured::try_from( "↑ ↓ ←→ bA" )? } => ascii => [ Text "=?utf8?Q?=E2=86=91?=", @@ -150,7 +141,7 @@ mod test { Text " bA" ]} - ec_test!{ eats_cr_lf, { + ec_test! { eats_cr_lf, { Unstructured::try_from( "a \rb\n c\r\n " )? } => ascii => [ Text "a", @@ -162,7 +153,7 @@ mod test { Text " " ]} - ec_test!{ at_last_one_fws, { + ec_test! { at_last_one_fws, { Unstructured::try_from( "a\rb\nc\r\n" )? } => ascii => [ Text "a", @@ -174,7 +165,7 @@ mod test { Text " " ]} - ec_test!{ kinda_keeps_wsp, { + ec_test! { kinda_keeps_wsp, { Unstructured::try_from("\t\ta b \t")? } => ascii => [ MarkFWS, @@ -185,14 +176,14 @@ mod test { Text " \t" ]} - ec_test!{ wsp_only_phrase, { + ec_test! { wsp_only_phrase, { Unstructured::try_from( " \t " )? } => ascii => [ MarkFWS, Text " \t " ]} - ec_test!{ long_mixed_input, { + ec_test! { long_mixed_input, { Unstructured::try_from("Subject: …. AAAAAAAAAAAAAAAAAAA….. AA…")? } => ascii => [ Text "Subject:", @@ -203,4 +194,4 @@ mod test { MarkFWS, Text " =?utf8?Q?AA=E2=80=A6?=" ]} -}
\ No newline at end of file +} |