1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use vec1::{Vec1, Size0Error};
use internals::grammar::encoded_word::EncodedWordContext;
use internals::error::EncodingError;
use internals::encoder::{EncodingWriter, EncodableInHeader};
use ::{HeaderTryFrom, HeaderTryInto};
use ::error::ComponentCreationError;
use ::data::Input;
use super::utils::text_partition::{ Partition, partition };
use super::word::{ Word, do_encode_word };
use super::{ CFWS, FWS };
#[derive( Debug, Clone, Eq, PartialEq, Hash )]
pub struct Phrase(
pub Vec1<Word> );
impl Phrase {
pub fn new<T: HeaderTryInto<Input>>(input: T) -> Result<Self, ComponentCreationError> {
let input = input.try_into()?;
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())
)?;
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 );
}
words.push( word );
},
Partition::SPACE( _gap ) => {
last_gap = Some( CFWS::SingleFws( FWS ) )
}
}
}
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 );
}
Ok( Phrase( words ) )
}
}
impl<'a> HeaderTryFrom<&'a str> for Phrase {
fn try_from(input: &'a str) -> Result<Self, ComponentCreationError> {
Phrase::new(input)
}
}
impl HeaderTryFrom<String> for Phrase {
fn try_from(input: String) -> Result<Self, ComponentCreationError> {
Phrase::new(input)
}
}
impl HeaderTryFrom<Input> for Phrase {
fn try_from(input: Input) -> Result<Self, ComponentCreationError> {
Phrase::new(input)
}
}
impl EncodableInHeader for Phrase {
fn encode(&self, heandle: &mut EncodingWriter) -> Result<(), EncodingError> {
for word in self.0.iter() {
do_encode_word( &*word, heandle, Some( EncodedWordContext::Phrase ) )?;
}
Ok( () )
}
fn boxed_clone(&self) -> Box<EncodableInHeader> {
Box::new(self.clone())
}
}
#[cfg(test)]
mod test {
use ::HeaderTryFrom;
use super::Phrase;
ec_test!{ simple, {
Phrase::try_from("simple think")?
} => ascii => [
Text "simple",
MarkFWS,
Text " think"
]}
ec_test!{ with_encoding, {
Phrase::try_from(" hm nääds encoding")?
} => ascii => [
MarkFWS,
Text " hm",
MarkFWS,
Text " =?utf8?Q?n=C3=A4=C3=A4ds?=",
MarkFWS,
Text " encoding"
]}
}