Skip to main content

imapprotolens/
types.rs

1#![allow(clippy::needless_lifetimes)]
2
3use std::borrow::Cow;
4use std::collections::HashMap;
5use std::ops::RangeInclusive;
6
7pub mod acls;
8pub use acls::*;
9
10fn to_owned_cow<T: ?Sized + ToOwned>(c: Cow<'_, T>) -> Cow<'static, T> {
11    Cow::Owned(c.into_owned())
12}
13
14#[derive(Clone, Debug, Eq, PartialEq)]
15pub struct Request<'a>(pub Cow<'a, [u8]>, pub Cow<'a, [u8]>);
16
17#[derive(Copy, Clone, Debug, Eq, PartialEq)]
18pub enum AttrMacro {
19    All,
20    Fast,
21    Full,
22}
23
24#[derive(Debug, Eq, PartialEq)]
25#[non_exhaustive]
26pub enum Response<'a> {
27    Capabilities(Vec<Capability<'a>>),
28    Continue {
29        code: Option<ResponseCode<'a>>,
30        information: Option<Cow<'a, str>>,
31    },
32    Done {
33        tag: RequestId,
34        status: Status,
35        code: Option<ResponseCode<'a>>,
36        information: Option<Cow<'a, str>>,
37    },
38    Data {
39        status: Status,
40        code: Option<ResponseCode<'a>>,
41        information: Option<Cow<'a, str>>,
42    },
43    Expunge(u32),
44    Vanished {
45        earlier: bool,
46        uids: Vec<std::ops::RangeInclusive<u32>>,
47    },
48    Fetch(u32, Vec<AttributeValue<'a>>),
49    MailboxData(MailboxDatum<'a>),
50    Quota(Quota<'a>),
51    QuotaRoot(QuotaRoot<'a>),
52    Id(Option<HashMap<Cow<'a, str>, Cow<'a, str>>>),
53    Acl(Acl<'a>),
54    ListRights(ListRights<'a>),
55    MyRights(MyRights<'a>),
56}
57
58impl<'a> Response<'a> {
59    pub fn from_bytes(buf: &'a [u8]) -> crate::ParseResult<'a> {
60        crate::parser::parse_response(buf)
61    }
62
63    pub fn into_owned(self) -> Response<'static> {
64        match self {
65            Response::Capabilities(capabilities) => Response::Capabilities(
66                capabilities
67                    .into_iter()
68                    .map(Capability::into_owned)
69                    .collect(),
70            ),
71            Response::Continue { code, information } => Response::Continue {
72                code: code.map(ResponseCode::into_owned),
73                information: information.map(to_owned_cow),
74            },
75            Response::Done {
76                tag,
77                status,
78                code,
79                information,
80            } => Response::Done {
81                tag,
82                status,
83                code: code.map(ResponseCode::into_owned),
84                information: information.map(to_owned_cow),
85            },
86            Response::Data {
87                status,
88                code,
89                information,
90            } => Response::Data {
91                status,
92                code: code.map(ResponseCode::into_owned),
93                information: information.map(to_owned_cow),
94            },
95            Response::Expunge(seq) => Response::Expunge(seq),
96            Response::Vanished { earlier, uids } => Response::Vanished { earlier, uids },
97            Response::Fetch(seq, attrs) => Response::Fetch(
98                seq,
99                attrs.into_iter().map(AttributeValue::into_owned).collect(),
100            ),
101            Response::MailboxData(datum) => Response::MailboxData(datum.into_owned()),
102            Response::Quota(quota) => Response::Quota(quota.into_owned()),
103            Response::QuotaRoot(quota_root) => Response::QuotaRoot(quota_root.into_owned()),
104            Response::Id(map) => Response::Id(map.map(|m| {
105                m.into_iter()
106                    .map(|(k, v)| (to_owned_cow(k), to_owned_cow(v)))
107                    .collect()
108            })),
109            Response::Acl(acl_list) => Response::Acl(acl_list.into_owned()),
110            Response::ListRights(rights) => Response::ListRights(rights.into_owned()),
111            Response::MyRights(rights) => Response::MyRights(rights.into_owned()),
112        }
113    }
114}
115
116#[derive(Debug, Eq, PartialEq)]
117pub enum Status {
118    Ok,
119    No,
120    Bad,
121    PreAuth,
122    Bye,
123}
124
125#[derive(Debug, Eq, PartialEq)]
126#[non_exhaustive]
127pub enum ResponseCode<'a> {
128    Alert,
129    BadCharset(Option<Vec<Cow<'a, str>>>),
130    Capabilities(Vec<Capability<'a>>),
131    HighestModSeq(u64), // RFC 4551, section 3.1.1
132    Parse,
133    PermanentFlags(Vec<Cow<'a, str>>),
134    ReadOnly,
135    ReadWrite,
136    TryCreate,
137    UidNext(u32),
138    UidValidity(u32),
139    Unseen(u32),
140    AppendUid(u32, Vec<UidSetMember>),
141    CopyUid(u32, Vec<UidSetMember>, Vec<UidSetMember>),
142    UidNotSticky,
143    MetadataLongEntries(u64), // RFC 5464, section 4.2.1
144    MetadataMaxSize(u64),     // RFC 5464, section 4.3
145    MetadataTooMany,          // RFC 5464, section 4.3
146    MetadataNoPrivate,        // RFC 5464, section 4.3
147}
148
149#[derive(Clone, Debug, Eq, PartialEq)]
150pub enum UidSetMember {
151    UidRange(RangeInclusive<u32>),
152    Uid(u32),
153}
154
155impl From<RangeInclusive<u32>> for UidSetMember {
156    fn from(x: RangeInclusive<u32>) -> Self {
157        UidSetMember::UidRange(x)
158    }
159}
160
161impl From<u32> for UidSetMember {
162    fn from(x: u32) -> Self {
163        UidSetMember::Uid(x)
164    }
165}
166
167impl<'a> ResponseCode<'a> {
168    pub fn into_owned(self) -> ResponseCode<'static> {
169        match self {
170            ResponseCode::Alert => ResponseCode::Alert,
171            ResponseCode::BadCharset(v) => {
172                ResponseCode::BadCharset(v.map(|vs| vs.into_iter().map(to_owned_cow).collect()))
173            }
174            ResponseCode::Capabilities(v) => {
175                ResponseCode::Capabilities(v.into_iter().map(Capability::into_owned).collect())
176            }
177            ResponseCode::HighestModSeq(v) => ResponseCode::HighestModSeq(v),
178            ResponseCode::Parse => ResponseCode::Parse,
179            ResponseCode::PermanentFlags(v) => {
180                ResponseCode::PermanentFlags(v.into_iter().map(to_owned_cow).collect())
181            }
182            ResponseCode::ReadOnly => ResponseCode::ReadOnly,
183            ResponseCode::ReadWrite => ResponseCode::ReadWrite,
184            ResponseCode::TryCreate => ResponseCode::TryCreate,
185            ResponseCode::UidNext(v) => ResponseCode::UidNext(v),
186            ResponseCode::UidValidity(v) => ResponseCode::UidValidity(v),
187            ResponseCode::Unseen(v) => ResponseCode::Unseen(v),
188            ResponseCode::AppendUid(a, b) => ResponseCode::AppendUid(a, b),
189            ResponseCode::CopyUid(a, b, c) => ResponseCode::CopyUid(a, b, c),
190            ResponseCode::UidNotSticky => ResponseCode::UidNotSticky,
191            ResponseCode::MetadataLongEntries(v) => ResponseCode::MetadataLongEntries(v),
192            ResponseCode::MetadataMaxSize(v) => ResponseCode::MetadataMaxSize(v),
193            ResponseCode::MetadataTooMany => ResponseCode::MetadataTooMany,
194            ResponseCode::MetadataNoPrivate => ResponseCode::MetadataNoPrivate,
195        }
196    }
197}
198
199#[derive(Debug, Eq, PartialEq, Clone)]
200#[non_exhaustive]
201pub enum StatusAttribute {
202    HighestModSeq(u64), // RFC 4551
203    Messages(u32),
204    Recent(u32),
205    UidNext(u32),
206    UidValidity(u32),
207    Unseen(u32),
208}
209
210#[derive(Debug, Eq, PartialEq, Clone)]
211pub struct Metadata {
212    pub entry: String,
213    pub value: Option<String>,
214}
215
216#[derive(Debug, Eq, PartialEq, Clone)]
217#[non_exhaustive]
218pub enum MailboxDatum<'a> {
219    Exists(u32),
220    Flags(Vec<Cow<'a, str>>),
221    List {
222        name_attributes: Vec<NameAttribute<'a>>,
223        delimiter: Option<Cow<'a, str>>,
224        name: Cow<'a, str>,
225    },
226    Search(Vec<u32>),
227    Sort(Vec<u32>),
228    Status {
229        mailbox: Cow<'a, str>,
230        status: Vec<StatusAttribute>,
231    },
232    Recent(u32),
233    MetadataSolicited {
234        mailbox: Cow<'a, str>,
235        values: Vec<Metadata>,
236    },
237    MetadataUnsolicited {
238        mailbox: Cow<'a, str>,
239        values: Vec<Cow<'a, str>>,
240    },
241    GmailLabels(Vec<Cow<'a, str>>),
242    GmailMsgId(u64),
243}
244
245impl<'a> MailboxDatum<'a> {
246    pub fn into_owned(self) -> MailboxDatum<'static> {
247        match self {
248            MailboxDatum::Exists(seq) => MailboxDatum::Exists(seq),
249            MailboxDatum::Flags(flags) => {
250                MailboxDatum::Flags(flags.into_iter().map(to_owned_cow).collect())
251            }
252            MailboxDatum::List {
253                name_attributes,
254                delimiter,
255                name,
256            } => MailboxDatum::List {
257                name_attributes: name_attributes
258                    .into_iter()
259                    .map(|named_attribute| named_attribute.into_owned())
260                    .collect(),
261                delimiter: delimiter.map(to_owned_cow),
262                name: to_owned_cow(name),
263            },
264            MailboxDatum::Search(seqs) => MailboxDatum::Search(seqs),
265            MailboxDatum::Sort(seqs) => MailboxDatum::Sort(seqs),
266            MailboxDatum::Status { mailbox, status } => MailboxDatum::Status {
267                mailbox: to_owned_cow(mailbox),
268                status,
269            },
270            MailboxDatum::Recent(seq) => MailboxDatum::Recent(seq),
271            MailboxDatum::MetadataSolicited { mailbox, values } => {
272                MailboxDatum::MetadataSolicited {
273                    mailbox: to_owned_cow(mailbox),
274                    values,
275                }
276            }
277            MailboxDatum::MetadataUnsolicited { mailbox, values } => {
278                MailboxDatum::MetadataUnsolicited {
279                    mailbox: to_owned_cow(mailbox),
280                    values: values.into_iter().map(to_owned_cow).collect(),
281                }
282            }
283            MailboxDatum::GmailLabels(labels) => {
284                MailboxDatum::GmailLabels(labels.into_iter().map(to_owned_cow).collect())
285            }
286            MailboxDatum::GmailMsgId(msgid) => MailboxDatum::GmailMsgId(msgid),
287        }
288    }
289}
290
291#[derive(Debug, Eq, PartialEq, Hash)]
292pub enum Capability<'a> {
293    Imap4rev1,
294    Auth(Cow<'a, str>),
295    Atom(Cow<'a, str>),
296}
297
298impl<'a> Capability<'a> {
299    pub fn into_owned(self) -> Capability<'static> {
300        match self {
301            Capability::Imap4rev1 => Capability::Imap4rev1,
302            Capability::Auth(v) => Capability::Auth(to_owned_cow(v)),
303            Capability::Atom(v) => Capability::Atom(to_owned_cow(v)),
304        }
305    }
306}
307
308#[derive(Debug, Eq, PartialEq)]
309#[non_exhaustive]
310pub enum Attribute {
311    Body,
312    Envelope,
313    Flags,
314    InternalDate,
315    ModSeq, // RFC 4551, section 3.3.2
316    Rfc822,
317    Rfc822Size,
318    Rfc822Text,
319    Uid,
320    /// https://developers.google.com/gmail/imap/imap-extensions#access_to_gmail_labels_x-gm-labels
321    GmailLabels,
322    GmailMsgId,
323}
324
325#[derive(Debug, Eq, PartialEq)]
326pub enum MessageSection {
327    Header,
328    Mime,
329    Text,
330}
331
332#[derive(Debug, Eq, PartialEq)]
333pub enum SectionPath {
334    Full(MessageSection),
335    Part(Vec<u32>, Option<MessageSection>),
336}
337
338#[allow(clippy::large_enum_variant)]
339#[derive(Debug, Eq, PartialEq)]
340#[non_exhaustive]
341pub enum AttributeValue<'a> {
342    BodySection {
343        section: Option<SectionPath>,
344        index: Option<u32>,
345        data: Option<Cow<'a, [u8]>>,
346    },
347    BodyStructure(BodyStructure<'a>),
348    Envelope(Box<Envelope<'a>>),
349    Flags(Vec<Cow<'a, str>>),
350    InternalDate(Cow<'a, str>),
351    ModSeq(u64), // RFC 4551, section 3.3.2
352    Rfc822(Option<Cow<'a, [u8]>>),
353    Rfc822Header(Option<Cow<'a, [u8]>>),
354    Rfc822Size(u32),
355    Rfc822Text(Option<Cow<'a, [u8]>>),
356    Uid(u32),
357    /// https://developers.google.com/gmail/imap/imap-extensions#access_to_gmail_labels_x-gm-labels
358    GmailLabels(Vec<Cow<'a, str>>),
359    GmailMsgId(u64),
360}
361
362impl<'a> AttributeValue<'a> {
363    pub fn into_owned(self) -> AttributeValue<'static> {
364        match self {
365            AttributeValue::BodySection {
366                section,
367                index,
368                data,
369            } => AttributeValue::BodySection {
370                section,
371                index,
372                data: data.map(to_owned_cow),
373            },
374            AttributeValue::BodyStructure(body) => AttributeValue::BodyStructure(body.into_owned()),
375            AttributeValue::Envelope(e) => AttributeValue::Envelope(Box::new(e.into_owned())),
376            AttributeValue::Flags(v) => {
377                AttributeValue::Flags(v.into_iter().map(to_owned_cow).collect())
378            }
379            AttributeValue::InternalDate(v) => AttributeValue::InternalDate(to_owned_cow(v)),
380            AttributeValue::ModSeq(v) => AttributeValue::ModSeq(v),
381            AttributeValue::Rfc822(v) => AttributeValue::Rfc822(v.map(to_owned_cow)),
382            AttributeValue::Rfc822Header(v) => AttributeValue::Rfc822Header(v.map(to_owned_cow)),
383            AttributeValue::Rfc822Size(v) => AttributeValue::Rfc822Size(v),
384            AttributeValue::Rfc822Text(v) => AttributeValue::Rfc822Text(v.map(to_owned_cow)),
385            AttributeValue::Uid(v) => AttributeValue::Uid(v),
386            AttributeValue::GmailLabels(v) => {
387                AttributeValue::GmailLabels(v.into_iter().map(to_owned_cow).collect())
388            }
389            AttributeValue::GmailMsgId(v) => AttributeValue::GmailMsgId(v),
390        }
391    }
392}
393
394#[allow(clippy::large_enum_variant)]
395#[derive(Debug, Eq, PartialEq)]
396#[non_exhaustive]
397pub enum AttributeValue2<'a> {
398    BodySection {
399        section: Option<SectionPath>,
400        index: Option<u32>,
401        literal_size: Option<usize>,
402        data: Option<&'a [u8]>,
403    },
404    BodyStructure(BodyStructure<'a>),
405    Rfc822Header(Option<usize>),
406    Rfc822Text(Option<usize>),
407    Ignored,
408}
409
410#[allow(clippy::large_enum_variant)]
411#[derive(Debug, Eq, PartialEq)]
412pub enum BodyStructure<'a> {
413    Basic {
414        common: BodyContentCommon<'a>,
415        other: BodyContentSinglePart<'a>,
416        extension: Option<BodyExtension<'a>>,
417    },
418    Text {
419        common: BodyContentCommon<'a>,
420        other: BodyContentSinglePart<'a>,
421        lines: u32,
422        extension: Option<BodyExtension<'a>>,
423    },
424    Message {
425        common: BodyContentCommon<'a>,
426        other: BodyContentSinglePart<'a>,
427        envelope: Envelope<'a>,
428        body: Box<BodyStructure<'a>>,
429        lines: u32,
430        extension: Option<BodyExtension<'a>>,
431    },
432    Multipart {
433        common: BodyContentCommon<'a>,
434        bodies: Vec<BodyStructure<'a>>,
435        extension: Option<BodyExtension<'a>>,
436    },
437}
438
439impl<'a> BodyStructure<'a> {
440    pub fn into_owned(self) -> BodyStructure<'static> {
441        match self {
442            BodyStructure::Basic {
443                common,
444                other,
445                extension,
446            } => BodyStructure::Basic {
447                common: common.into_owned(),
448                other: other.into_owned(),
449                extension: extension.map(|v| v.into_owned()),
450            },
451            BodyStructure::Text {
452                common,
453                other,
454                lines,
455                extension,
456            } => BodyStructure::Text {
457                common: common.into_owned(),
458                other: other.into_owned(),
459                lines,
460                extension: extension.map(|v| v.into_owned()),
461            },
462            BodyStructure::Message {
463                common,
464                other,
465                envelope,
466                body,
467                lines,
468                extension,
469            } => BodyStructure::Message {
470                common: common.into_owned(),
471                other: other.into_owned(),
472                envelope: envelope.into_owned(),
473                body: Box::new(body.into_owned()),
474                lines,
475                extension: extension.map(|v| v.into_owned()),
476            },
477            BodyStructure::Multipart {
478                common,
479                bodies,
480                extension,
481            } => BodyStructure::Multipart {
482                common: common.into_owned(),
483                bodies: bodies.into_iter().map(|v| v.into_owned()).collect(),
484                extension: extension.map(|v| v.into_owned()),
485            },
486        }
487    }
488}
489
490#[derive(Debug, Eq, PartialEq)]
491pub struct BodyContentCommon<'a> {
492    pub ty: ContentType<'a>,
493    pub disposition: Option<ContentDisposition<'a>>,
494    pub language: Option<Vec<Cow<'a, str>>>,
495    pub location: Option<Cow<'a, str>>,
496}
497
498impl<'a> BodyContentCommon<'a> {
499    pub fn into_owned(self) -> BodyContentCommon<'static> {
500        BodyContentCommon {
501            ty: self.ty.into_owned(),
502            disposition: self.disposition.map(|v| v.into_owned()),
503            language: self
504                .language
505                .map(|v| v.into_iter().map(to_owned_cow).collect()),
506            location: self.location.map(to_owned_cow),
507        }
508    }
509}
510
511#[derive(Debug, Eq, PartialEq)]
512pub struct BodyContentSinglePart<'a> {
513    pub id: Option<Cow<'a, str>>,
514    pub md5: Option<Cow<'a, str>>,
515    pub description: Option<Cow<'a, str>>,
516    pub transfer_encoding: ContentEncoding<'a>,
517    pub octets: u32,
518}
519
520impl<'a> BodyContentSinglePart<'a> {
521    pub fn into_owned(self) -> BodyContentSinglePart<'static> {
522        BodyContentSinglePart {
523            id: self.id.map(to_owned_cow),
524            md5: self.md5.map(to_owned_cow),
525            description: self.description.map(to_owned_cow),
526            transfer_encoding: self.transfer_encoding.into_owned(),
527            octets: self.octets,
528        }
529    }
530}
531
532#[derive(Debug, Eq, PartialEq)]
533pub struct ContentType<'a> {
534    pub ty: Cow<'a, str>,
535    pub subtype: Cow<'a, str>,
536    pub params: BodyParams<'a>,
537}
538
539impl<'a> ContentType<'a> {
540    pub fn into_owned(self) -> ContentType<'static> {
541        ContentType {
542            ty: to_owned_cow(self.ty),
543            subtype: to_owned_cow(self.subtype),
544            params: body_param_owned(self.params),
545        }
546    }
547}
548
549#[derive(Debug, Eq, PartialEq)]
550pub struct ContentDisposition<'a> {
551    pub ty: Cow<'a, str>,
552    pub params: BodyParams<'a>,
553}
554
555impl<'a> ContentDisposition<'a> {
556    pub fn into_owned(self) -> ContentDisposition<'static> {
557        ContentDisposition {
558            ty: to_owned_cow(self.ty),
559            params: body_param_owned(self.params),
560        }
561    }
562}
563
564#[derive(Debug, Eq, PartialEq, Clone)]
565pub enum ContentEncoding<'a> {
566    SevenBit,
567    EightBit,
568    Binary,
569    Base64,
570    QuotedPrintable,
571    Other(Cow<'a, str>),
572}
573
574impl<'a> ContentEncoding<'a> {
575    pub fn into_owned(self) -> ContentEncoding<'static> {
576        match self {
577            ContentEncoding::SevenBit => ContentEncoding::SevenBit,
578            ContentEncoding::EightBit => ContentEncoding::EightBit,
579            ContentEncoding::Binary => ContentEncoding::Binary,
580            ContentEncoding::Base64 => ContentEncoding::Base64,
581            ContentEncoding::QuotedPrintable => ContentEncoding::QuotedPrintable,
582            ContentEncoding::Other(v) => ContentEncoding::Other(to_owned_cow(v)),
583        }
584    }
585}
586
587#[derive(Debug, Eq, PartialEq)]
588pub enum BodyExtension<'a> {
589    Num(u32),
590    Str(Option<Cow<'a, str>>),
591    List(Vec<BodyExtension<'a>>),
592}
593
594impl<'a> BodyExtension<'a> {
595    pub fn into_owned(self) -> BodyExtension<'static> {
596        match self {
597            BodyExtension::Num(v) => BodyExtension::Num(v),
598            BodyExtension::Str(v) => BodyExtension::Str(v.map(to_owned_cow)),
599            BodyExtension::List(v) => {
600                BodyExtension::List(v.into_iter().map(|v| v.into_owned()).collect())
601            }
602        }
603    }
604}
605
606pub type BodyParams<'a> = Option<Vec<(Cow<'a, str>, Cow<'a, str>)>>;
607
608fn body_param_owned(v: BodyParams<'_>) -> BodyParams<'static> {
609    v.map(|v| {
610        v.into_iter()
611            .map(|(k, v)| (to_owned_cow(k), to_owned_cow(v)))
612            .collect()
613    })
614}
615
616/// An RFC 2822 envelope
617///
618/// See https://datatracker.ietf.org/doc/html/rfc2822#section-3.6 for more details.
619#[derive(Debug, Eq, PartialEq)]
620pub struct Envelope<'a> {
621    pub date: Option<Cow<'a, [u8]>>,
622    pub subject: Option<Cow<'a, [u8]>>,
623    /// Author of the message; mailbox responsible for writing the message
624    pub from: Option<Vec<Address<'a>>>,
625    /// Mailbox of the agent responsible for the message's transmission
626    pub sender: Option<Vec<Address<'a>>>,
627    /// Mailbox that the author of the message suggests replies be sent to
628    pub reply_to: Option<Vec<Address<'a>>>,
629    pub to: Option<Vec<Address<'a>>>,
630    pub cc: Option<Vec<Address<'a>>>,
631    pub bcc: Option<Vec<Address<'a>>>,
632    pub in_reply_to: Option<Cow<'a, [u8]>>,
633    pub message_id: Option<Cow<'a, [u8]>>,
634}
635
636impl<'a> Envelope<'a> {
637    pub fn into_owned(self) -> Envelope<'static> {
638        Envelope {
639            date: self.date.map(to_owned_cow),
640            subject: self.subject.map(to_owned_cow),
641            from: self
642                .from
643                .map(|v| v.into_iter().map(|v| v.into_owned()).collect()),
644            sender: self
645                .sender
646                .map(|v| v.into_iter().map(|v| v.into_owned()).collect()),
647            reply_to: self
648                .reply_to
649                .map(|v| v.into_iter().map(|v| v.into_owned()).collect()),
650            to: self
651                .to
652                .map(|v| v.into_iter().map(|v| v.into_owned()).collect()),
653            cc: self
654                .cc
655                .map(|v| v.into_iter().map(|v| v.into_owned()).collect()),
656            bcc: self
657                .bcc
658                .map(|v| v.into_iter().map(|v| v.into_owned()).collect()),
659            in_reply_to: self.in_reply_to.map(to_owned_cow),
660            message_id: self.message_id.map(to_owned_cow),
661        }
662    }
663}
664
665#[derive(Debug, Eq, PartialEq)]
666pub struct Address<'a> {
667    pub name: Option<Cow<'a, [u8]>>,
668    pub adl: Option<Cow<'a, [u8]>>,
669    pub mailbox: Option<Cow<'a, [u8]>>,
670    pub host: Option<Cow<'a, [u8]>>,
671}
672
673impl<'a> Address<'a> {
674    pub fn into_owned(self) -> Address<'static> {
675        Address {
676            name: self.name.map(to_owned_cow),
677            adl: self.adl.map(to_owned_cow),
678            mailbox: self.mailbox.map(to_owned_cow),
679            host: self.host.map(to_owned_cow),
680        }
681    }
682}
683
684#[derive(Clone, Debug, Eq, PartialEq)]
685pub struct RequestId(pub String);
686
687impl RequestId {
688    pub fn as_bytes(&self) -> &[u8] {
689        self.0.as_bytes()
690    }
691}
692
693#[derive(Clone, Copy, Debug, Eq, PartialEq)]
694pub enum State {
695    NotAuthenticated,
696    Authenticated,
697    Selected,
698    Logout,
699}
700
701// Body Structure
702
703pub struct BodyFields<'a> {
704    pub param: BodyParams<'a>,
705    pub id: Option<Cow<'a, str>>,
706    pub description: Option<Cow<'a, str>>,
707    pub transfer_encoding: ContentEncoding<'a>,
708    pub octets: u32,
709}
710
711impl<'a> BodyFields<'a> {
712    pub fn into_owned(self) -> BodyFields<'static> {
713        BodyFields {
714            param: body_param_owned(self.param),
715            id: self.id.map(to_owned_cow),
716            description: self.description.map(to_owned_cow),
717            transfer_encoding: self.transfer_encoding.into_owned(),
718            octets: self.octets,
719        }
720    }
721}
722
723pub struct BodyExt1Part<'a> {
724    pub md5: Option<Cow<'a, str>>,
725    pub disposition: Option<ContentDisposition<'a>>,
726    pub language: Option<Vec<Cow<'a, str>>>,
727    pub location: Option<Cow<'a, str>>,
728    pub extension: Option<BodyExtension<'a>>,
729}
730
731impl<'a> BodyExt1Part<'a> {
732    pub fn into_owned(self) -> BodyExt1Part<'static> {
733        BodyExt1Part {
734            md5: self.md5.map(to_owned_cow),
735            disposition: self.disposition.map(|v| v.into_owned()),
736            language: self
737                .language
738                .map(|v| v.into_iter().map(to_owned_cow).collect()),
739            location: self.location.map(to_owned_cow),
740            extension: self.extension.map(|v| v.into_owned()),
741        }
742    }
743}
744
745pub struct BodyExtMPart<'a> {
746    pub param: BodyParams<'a>,
747    pub disposition: Option<ContentDisposition<'a>>,
748    pub language: Option<Vec<Cow<'a, str>>>,
749    pub location: Option<Cow<'a, str>>,
750    pub extension: Option<BodyExtension<'a>>,
751}
752
753impl<'a> BodyExtMPart<'a> {
754    pub fn into_owned(self) -> BodyExtMPart<'static> {
755        BodyExtMPart {
756            param: body_param_owned(self.param),
757            disposition: self.disposition.map(|v| v.into_owned()),
758            language: self
759                .language
760                .map(|v| v.into_iter().map(to_owned_cow).collect()),
761            location: self.location.map(to_owned_cow),
762            extension: self.extension.map(|v| v.into_owned()),
763        }
764    }
765}
766
767/// The name attributes are returned as part of a LIST response described in
768/// [RFC 3501 section 7.2.2](https://tools.ietf.org/html/rfc3501#section-7.2.2).
769///
770/// This enumeration additional includes values from the extension Special-Use
771/// Mailboxes [RFC 6154 section 2](https://tools.ietf.org/html/rfc6154#section-2).
772#[derive(Debug, Eq, PartialEq, Clone)]
773#[non_exhaustive]
774pub enum NameAttribute<'a> {
775    /// From [RFC 3501 section 7.2.2](https://tools.ietf.org/html/rfc3501#section-7.2.2):
776    ///
777    /// > It is not possible for any child levels of hierarchy to exist
778    /// > under this name; no child levels exist now and none can be
779    /// > created in the future.
780    NoInferiors,
781    /// From [RFC 3501 section 7.2.2](https://tools.ietf.org/html/rfc3501#section-7.2.2):
782    ///
783    /// > It is not possible to use this name as a selectable mailbox.
784    NoSelect,
785    /// From [RFC 3501 section 7.2.2](https://tools.ietf.org/html/rfc3501#section-7.2.2):
786    ///
787    /// > The mailbox has been marked "interesting" by the server; the
788    /// > mailbox probably contains messages that have been added since
789    /// > the last time the mailbox was selected.
790    Marked,
791    /// From [RFC 3501 section 7.2.2](https://tools.ietf.org/html/rfc3501#section-7.2.2):
792    ///
793    /// > The mailbox does not contain any additional messages since the
794    /// > last time the mailbox was selected.
795    Unmarked,
796    /// From [RFC 6154 section 2](https://tools.ietf.org/html/rfc6154#section-2):
797    ///
798    /// > This mailbox presents all messages in the user's message store.
799    /// > Implementations MAY omit some messages, such as, perhaps, those
800    /// > in \Trash and \Junk.  When this special use is supported, it is
801    /// > almost certain to represent a virtual mailbox.
802    All,
803    /// From [RFC 6154 section 2](https://tools.ietf.org/html/rfc6154#section-2):
804    ///
805    /// > This mailbox is used to archive messages.  The meaning of an
806    /// > "archival" mailbox is server-dependent; typically, it will be
807    /// > used to get messages out of the inbox, or otherwise keep them
808    /// > out of the user's way, while still making them accessible.
809    Archive,
810    /// From [RFC 6154 section 2](https://tools.ietf.org/html/rfc6154#section-2):
811    ///
812    /// > This mailbox is used to hold draft messages -- typically,
813    /// > messages that are being composed but have not yet been sent.  In
814    /// > some server implementations, this might be a virtual mailbox,
815    /// > containing messages from other mailboxes that are marked with
816    /// > the "\Draft" message flag.  Alternatively, this might just be
817    /// > advice that a client put drafts here.
818    Drafts,
819    /// From [RFC 6154 section 2](https://tools.ietf.org/html/rfc6154#section-2):
820    ///
821    /// > This mailbox presents all messages marked in some way as
822    /// > "important".  When this special use is supported, it is likely
823    /// > to represent a virtual mailbox collecting messages (from other
824    /// > mailboxes) that are marked with the "\Flagged" message flag.
825    Flagged,
826    /// From [RFC 6154 section 2](https://tools.ietf.org/html/rfc6154#section-2):
827    ///
828    /// > This mailbox is where messages deemed to be junk mail are held.
829    /// > Some server implementations might put messages here
830    /// > automatically.  Alternatively, this might just be advice to a
831    /// > client-side spam filter.
832    Junk,
833    /// From [RFC 6154 section 2](https://tools.ietf.org/html/rfc6154#section-2):
834    ///
835    /// > This mailbox is used to hold copies of messages that have been
836    /// > sent.  Some server implementations might put messages here
837    /// > automatically.  Alternatively, this might just be advice that a
838    /// > client save sent messages here.
839    Sent,
840    /// From [RFC 6154 section 2](https://tools.ietf.org/html/rfc6154#section-2)
841    ///
842    /// > This mailbox is used to hold messages that have been deleted or
843    /// > marked for deletion.  In some server implementations, this might
844    /// > be a virtual mailbox, containing messages from other mailboxes
845    /// > that are marked with the "\Deleted" message flag.
846    /// > Alternatively, this might just be advice that a client that
847    /// > chooses not to use the IMAP "\Deleted" model should use this as
848    /// > its trash location.  In server implementations that strictly
849    /// > expect the IMAP "\Deleted" model, this special use is likely not
850    /// > to be supported.
851    Trash,
852    /// A name attribute not defined in [RFC 3501 section 7.2.2](https://tools.ietf.org/html/rfc3501#section-7.2.2)
853    /// or any supported extension.
854    Extension(Cow<'a, str>),
855}
856
857impl<'a> NameAttribute<'a> {
858    pub fn into_owned(self) -> NameAttribute<'static> {
859        match self {
860            // RFC 3501
861            NameAttribute::NoInferiors => NameAttribute::NoInferiors,
862            NameAttribute::NoSelect => NameAttribute::NoSelect,
863            NameAttribute::Marked => NameAttribute::Marked,
864            NameAttribute::Unmarked => NameAttribute::Unmarked,
865            // RFC 6154
866            NameAttribute::All => NameAttribute::All,
867            NameAttribute::Archive => NameAttribute::Archive,
868            NameAttribute::Drafts => NameAttribute::Drafts,
869            NameAttribute::Flagged => NameAttribute::Flagged,
870            NameAttribute::Junk => NameAttribute::Junk,
871            NameAttribute::Sent => NameAttribute::Sent,
872            NameAttribute::Trash => NameAttribute::Trash,
873            // Extensions not supported by this crate
874            NameAttribute::Extension(s) => NameAttribute::Extension(to_owned_cow(s)),
875        }
876    }
877}
878
879// IMAP4 QUOTA extension (rfc2087)
880
881/// https://tools.ietf.org/html/rfc2087#section-3
882#[derive(Debug, Eq, PartialEq, Hash, Clone)]
883pub enum QuotaResourceName<'a> {
884    /// Sum of messages' RFC822.SIZE, in units of 1024 octets
885    Storage,
886    /// Number of messages
887    Message,
888    Atom(Cow<'a, str>),
889}
890
891impl<'a> QuotaResourceName<'a> {
892    pub fn into_owned(self) -> QuotaResourceName<'static> {
893        match self {
894            QuotaResourceName::Message => QuotaResourceName::Message,
895            QuotaResourceName::Storage => QuotaResourceName::Storage,
896            QuotaResourceName::Atom(v) => QuotaResourceName::Atom(to_owned_cow(v)),
897        }
898    }
899}
900
901/// 5.1. QUOTA Response (https://tools.ietf.org/html/rfc2087#section-5.1)
902#[derive(Debug, Eq, PartialEq, Hash, Clone)]
903pub struct QuotaResource<'a> {
904    pub name: QuotaResourceName<'a>,
905    /// current usage of the resource
906    pub usage: u64,
907    /// resource limit
908    pub limit: u64,
909}
910
911impl<'a> QuotaResource<'a> {
912    pub fn into_owned(self) -> QuotaResource<'static> {
913        QuotaResource {
914            name: self.name.into_owned(),
915            usage: self.usage,
916            limit: self.limit,
917        }
918    }
919}
920
921/// 5.1. QUOTA Response (https://tools.ietf.org/html/rfc2087#section-5.1)
922#[derive(Debug, Eq, PartialEq, Hash, Clone)]
923pub struct Quota<'a> {
924    /// quota root name
925    pub root_name: Cow<'a, str>,
926    pub resources: Vec<QuotaResource<'a>>,
927}
928
929impl<'a> Quota<'a> {
930    pub fn into_owned(self) -> Quota<'static> {
931        Quota {
932            root_name: to_owned_cow(self.root_name),
933            resources: self.resources.into_iter().map(|r| r.into_owned()).collect(),
934        }
935    }
936}
937
938/// 5.2. QUOTAROOT Response (https://tools.ietf.org/html/rfc2087#section-5.2)
939#[derive(Debug, Eq, PartialEq, Hash, Clone)]
940pub struct QuotaRoot<'a> {
941    /// mailbox name
942    pub mailbox_name: Cow<'a, str>,
943    /// zero or more quota root names
944    pub quota_root_names: Vec<Cow<'a, str>>,
945}
946
947impl<'a> QuotaRoot<'a> {
948    pub fn into_owned(self) -> QuotaRoot<'static> {
949        QuotaRoot {
950            mailbox_name: to_owned_cow(self.mailbox_name),
951            quota_root_names: self
952                .quota_root_names
953                .into_iter()
954                .map(to_owned_cow)
955                .collect(),
956        }
957    }
958}
959
960#[cfg(test)]
961mod tests {
962    use super::*;
963
964    /// Tests that the [`NameAttribute::into_owned`] method returns the
965    /// same value (the ownership should only change).
966    #[test]
967    fn test_name_attribute_into_owned() {
968        let name_attributes = [
969            // RFC 3501
970            NameAttribute::NoInferiors,
971            NameAttribute::NoSelect,
972            NameAttribute::Marked,
973            NameAttribute::Unmarked,
974            // RFC 6154
975            NameAttribute::All,
976            NameAttribute::Archive,
977            NameAttribute::Drafts,
978            NameAttribute::Flagged,
979            NameAttribute::Junk,
980            NameAttribute::Sent,
981            NameAttribute::Trash,
982            // Extensions not supported by this crate
983            NameAttribute::Extension(Cow::Borrowed("Foobar")),
984        ];
985
986        for name_attribute in name_attributes {
987            let owned_name_attribute = name_attribute.clone().into_owned();
988            assert_eq!(name_attribute, owned_name_attribute);
989        }
990    }
991}