diff options
Diffstat (limited to 'headers/src/header_components/phrase.rs')
-rw-r--r-- | headers/src/header_components/phrase.rs | 79 |
1 files changed, 35 insertions, 44 deletions
diff --git a/headers/src/header_components/phrase.rs b/headers/src/header_components/phrase.rs index 10f56c5..2f2eba7 100644 --- a/headers/src/header_components/phrase.rs +++ b/headers/src/header_components/phrase.rs @@ -1,16 +1,16 @@ -use vec1::{Vec1, Size0Error}; +use vec1::{Size0Error, Vec1}; -use internals::grammar::encoded_word::EncodedWordContext; +use internals::encoder::{EncodableInHeader, EncodingWriter}; use internals::error::EncodingError; -use internals::encoder::{EncodingWriter, EncodableInHeader}; +use internals::grammar::encoded_word::EncodedWordContext; -use ::{HeaderTryFrom, HeaderTryInto}; -use ::error::ComponentCreationError; -use ::data::Input; +use data::Input; +use error::ComponentCreationError; +use {HeaderTryFrom, HeaderTryInto}; -use super::utils::text_partition::{ Partition, partition }; -use super::word::{ Word, do_encode_word }; -use super::{ CFWS, FWS }; +use super::utils::text_partition::{partition, Partition}; +use super::word::{do_encode_word, Word}; +use super::{CFWS, FWS}; /// Represent a "phrase" as it for example is used in the `Mailbox` type for the display name. /// @@ -21,7 +21,7 @@ use super::{ CFWS, FWS }; /// change with some of the coming braking changes.** If you just create it using `try_from` /// or `new` changes should not affect you, but if you create it from a vec of `Word`'s things /// might be different. -#[derive( Debug, Clone, Eq, PartialEq, Hash )] +#[derive(Debug, Clone, Eq, PartialEq, Hash)] pub struct Phrase( //FIXME hide this away or at last turn it into a struct field, with next braking change. /// The "words" the phrase consist of. Be aware that this are words in the sense of the @@ -30,10 +30,10 @@ pub struct Phrase( /// just have a sequence of "human words" turned into word instances there will be /// no whitespace between the words. (From the point of the mail grammar a words do not /// have to have any boundaries between each other even if this leads to ambiguity) - pub Vec1<Word> ); + pub Vec1<Word>, +); impl Phrase { - /// Creates a `Phrase` instance from some arbitrary input. /// /// This method can be used with both `&str` and `String`. @@ -55,39 +55,36 @@ impl Phrase { //OPTIMIZE: words => shared, then turn partition into shares, too let mut last_gap = None; let mut words = Vec::new(); - let partitions = partition( input.as_str() ) - .map_err(|err| ComponentCreationError - ::from_parent(err, "Phrase") - .with_str_context(input.as_str()) - )?; + let partitions = partition(input.as_str()).map_err(|err| { + ComponentCreationError::from_parent(err, "Phrase").with_str_context(input.as_str()) + })?; for partition in partitions.into_iter() { match partition { - Partition::VCHAR( word ) => { - let mut word = Word::try_from( word )?; - if let Some( fws ) = last_gap.take() { - word.pad_left( fws ); + Partition::VCHAR(word) => { + let mut word = Word::try_from(word)?; + if let Some(fws) = last_gap.take() { + word.pad_left(fws); } - words.push( word ); - }, - Partition::SPACE( _gap ) => { + words.push(word); + } + Partition::SPACE(_gap) => { //FIMXE currently collapses WS (This will leave at last one WS!) - last_gap = Some( CFWS::SingleFws( FWS ) ) + last_gap = Some(CFWS::SingleFws(FWS)) } } } - let mut words = Vec1::try_from_vec(words) - .map_err( |_| ComponentCreationError - ::from_parent(Size0Error, "Phrase") + let mut words = Vec1::try_from_vec(words).map_err(|_| { + ComponentCreationError::from_parent(Size0Error, "Phrase") .with_str_context(input.as_str()) - )?; + })?; - if let Some( right_padding ) = last_gap { - words.last_mut().pad_right( right_padding ); + if let Some(right_padding) = last_gap { + words.last_mut().pad_right(right_padding); } - Ok( Phrase( words ) ) + Ok(Phrase(words)) } } @@ -109,18 +106,15 @@ impl HeaderTryFrom<Input> for Phrase { } } - - -impl EncodableInHeader for Phrase { - +impl EncodableInHeader for Phrase { //FEATURE_TODO(warn_on_bad_phrase): warn if the phrase contains chars it should not // but can contain due to encoding, e.g. ascii CTL's fn encode(&self, heandle: &mut EncodingWriter) -> Result<(), EncodingError> { for word in self.0.iter() { - do_encode_word( &*word, heandle, Some( EncodedWordContext::Phrase ) )?; + do_encode_word(&*word, heandle, Some(EncodedWordContext::Phrase))?; } - Ok( () ) + Ok(()) } fn boxed_clone(&self) -> Box<EncodableInHeader> { @@ -130,10 +124,10 @@ impl EncodableInHeader for Phrase { #[cfg(test)] mod test { - use ::HeaderTryFrom; use super::Phrase; + use HeaderTryFrom; - ec_test!{ simple, { + ec_test! { simple, { Phrase::try_from("simple think")? } => ascii => [ Text "simple", @@ -141,7 +135,7 @@ mod test { Text " think" ]} - ec_test!{ with_encoding, { + ec_test! { with_encoding, { Phrase::try_from(" hm nääds encoding")? } => ascii => [ MarkFWS, @@ -152,6 +146,3 @@ mod test { Text " encoding" ]} } - - - |