summaryrefslogtreecommitdiffstats
path: root/headers/src/header_components/mailbox_list.rs
blob: 68a9a30cd5a72f8cfdab77c8d8ec3cdb18c13377 (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use std::iter::IntoIterator;
use vec1::Vec1;
use soft_ascii_string::SoftAsciiChar;

use internals::error::EncodingError;
use internals::encoder::{EncodableInHeader, EncodingWriter};
use ::{ HeaderTryFrom, HeaderTryInto};
use ::error::ComponentCreationError;

use super::Mailbox;

#[derive(Debug, Hash, Eq, PartialEq, Clone)]
pub struct OptMailboxList( pub Vec<Mailbox> );

#[derive(Debug, Hash, Eq, PartialEq, Clone)]
pub struct MailboxList( pub Vec1<Mailbox> );

impl MailboxList {
    pub fn from_single( m: Mailbox ) -> Self {
        MailboxList( Vec1::new( m ) )
    }
}

impl IntoIterator for MailboxList {
    type Item = <Vec1<Mailbox> as IntoIterator>::Item;
    type IntoIter = <Vec1<Mailbox> as IntoIterator>::IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}



impl EncodableInHeader for  OptMailboxList {

    fn encode(&self, handle: &mut EncodingWriter) -> Result<(), EncodingError> {
       encode_list( self.0.iter(), handle )
    }

    fn boxed_clone(&self) -> Box<EncodableInHeader> {
        Box::new(self.clone())
    }
}

//impl HeaderTryFrom<Mailbox> for OptMailboxList {
//    fn try_from( mbox: Mailbox ) -> Result<Self> {
//        Ok( OptMailboxList( vec![ mbox ] ) )
//    }
//}

//impl<T> HeaderTryFrom<T> for MailboxList
//    where T: HeaderTryInto<Mailbox>
//{
//    fn try_from( mbox: T ) -> Result<Self> {
//        let mbox = mbox.try_into()?;
//        Ok( MailboxList( Vec1::new( mbox ) ) )
//    }
//}

//TODO-RUST-RFC: allow conflicting wildcard implementations if priority is specified
// if done then we can implement it for IntoIterator instead of Vec and slice
impl<T> HeaderTryFrom<Vec<T>> for MailboxList
    where T: HeaderTryInto<Mailbox>
{
    fn try_from(vec: Vec<T>) -> Result<Self, ComponentCreationError> {
        try_from_into_iter( vec )
    }
}

fn try_from_into_iter<IT>( mboxes: IT ) -> Result<MailboxList, ComponentCreationError>
    where IT: IntoIterator, IT::Item: HeaderTryInto<Mailbox>
{
    let mut iter = mboxes.into_iter();
    let mut vec = if let Some( first) = iter.next() {
        Vec1::new( first.try_into()? )
    } else {
        //TODO chain vec1 Size0Error
        return Err(ComponentCreationError::new("MailboxList"));
    };
    for mbox in iter {
        vec.push( mbox.try_into()? );
    }
    Ok( MailboxList( vec ) )
}

macro_rules! impl_header_try_from_array {
    (_MBoxList 0) => ();
    (_MBoxList $len:tt) => (
        impl<T> HeaderTryFrom<[T; $len]> for MailboxList
            where T: HeaderTryInto<Mailbox>
        {
            fn try_from( vec: [T; $len] ) -> Result<Self, ComponentCreationError> {
                //due to only supporting arrays halfheartedly for now
                let heapified: Box<[T]> = Box::new(vec);
                let vecified: Vec<_> = heapified.into();
                try_from_into_iter( vecified )
            }
        }
    );
    (_OptMBoxList $len:tt) => (
        impl<T> HeaderTryFrom<[T; $len]> for OptMailboxList
            where T: HeaderTryInto<Mailbox>
        {
            fn try_from( vec: [T; $len] ) -> Result<Self, ComponentCreationError> {
                let heapified: Box<[T]> = Box::new(vec);
                let vecified: Vec<_> = heapified.into();
                let mut out = Vec::new();
                for ele in vecified.into_iter() {
                    out.push( ele.try_into()? );
                }
                Ok( OptMailboxList( out ) )
            }
        }
    );
    ($($len:tt)*) => ($(
        impl_header_try_from_array!{ _MBoxList $len }
        impl_header_try_from_array!{ _OptMBoxList $len }
    )*);
}

impl_header_try_from_array! {
     0  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
}

//TODO also implement for phrase list
macro_rules! impl_header_try_from_tuple {
    (_MBoxList []) => (
        compiler_error!("mailbox list needs at last one element")
    );
    (_MBoxList [ $($vs:ident),* ]) => (
        impl< $($vs),* > HeaderTryFrom<( $($vs,)* )> for MailboxList
            where $($vs: HeaderTryInto<Mailbox>),*
        {
            #[allow(non_snake_case)]
            fn try_from( ($($vs,)*): ($($vs,)*) ) -> Result<Self, ComponentCreationError> {
                // we use the type names as variable names,
                // not nice but it works
                //let ($($vs),*) = src;
                let mut out = Vec::new();
                $(
                    let $vs = $vs.try_into()?;
                    out.push($vs);
                )*
                Ok( MailboxList(
                    //UNWRAP_SAFE: len 0 is not implemented with the macro
                    $crate::vec1::Vec1::from_vec(out).unwrap()
                ) )
            }
        }
    );
    (_OptMBoxList [$($vs:ident),*]) => (
        impl< $($vs),* > HeaderTryFrom<( $($vs,)* )> for OptMailboxList
            where $($vs: HeaderTryInto<Mailbox>),*
        {
            #[allow(non_snake_case)]
            fn try_from( ($($vs,)*): ($($vs,)*) ) -> Result<Self, ComponentCreationError> {
                // we use the type names as variable names,
                // not nice but it works
                //let ($($vs),*) = src;
                let mut out = Vec::new();
                $(
                    let $vs = $vs.try_into()?;
                    out.push($vs);
                )*
                Ok( OptMailboxList( out ) )
            }
        }
    );
    ([]) => ();
    ([$first_vs:ident $(, $vs:ident)*]) => (
        impl_header_try_from_tuple!{ _MBoxList [$first_vs $(, $vs)* ] }
        impl_header_try_from_tuple!{ _OptMBoxList [$first_vs $(, $vs)*] }
        impl_header_try_from_tuple!{ [$($vs),*] }
    );
}

impl_header_try_from_tuple! {
    [
        A0,  A1,  A2,  A3,  A4,  A5,  A6,  A7,
        A8,  A9,  A10, A11, A12, A13, A14, A15,
        A16, A17, A18, A19, A20, A21, A22, A23,
        A24, A25, A26, A27, A28, A29, A30, A31
    ]
}

impl<T> HeaderTryFrom<Vec<T>> for OptMailboxList
    where T: HeaderTryInto<Mailbox>
{
    fn try_from(vec: Vec<T>) -> Result<Self, ComponentCreationError> {
        let mut out = Vec::new();
        for ele in vec.into_iter() {
            out.push( ele.try_into()? );
        }
        Ok( OptMailboxList( out ) )
    }
}

impl EncodableInHeader for  MailboxList {

    fn encode(&self, handle: &mut EncodingWriter) -> Result<(), EncodingError> {
        encode_list( self.0.iter(), handle )
    }

    fn boxed_clone(&self) -> Box<EncodableInHeader> {
        Box::new(self.clone())
    }
}

fn encode_list<'a, I>(list_iter: I, handle: &mut EncodingWriter) -> Result<(), EncodingError>
    where I: Iterator<Item=&'a Mailbox>
{
    sep_for!{ mailbox in list_iter;
        sep {
            handle.write_char( SoftAsciiChar::from_unchecked(',') )?;
            handle.write_fws();
        };
        mailbox.encode( handle )?;
    }
    Ok( () )
}

deref0!{ +mut OptMailboxList => Vec<Mailbox> }
deref0!{ +mut MailboxList => Vec1<Mailbox> }

#[cfg(test)]
mod test {
    use ::header_components::{ Mailbox, <