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
use std::borrow::Cow;
use imap_proto::{MailboxDatum, Response};
use crate::types::ResponseData;
rental! {
pub mod rents {
use super::*;
#[rental(debug, covariant)]
pub struct Name {
response: Box<ResponseData>,
inner: InnerName<'response>,
}
}
}
#[derive(PartialEq, Eq, Debug)]
pub struct InnerName<'a> {
attributes: Vec<NameAttribute<'a>>,
delimiter: Option<&'a str>,
name: &'a str,
}
pub use rents::Name;
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum NameAttribute<'a> {
NoInferiors,
NoSelect,
Marked,
Unmarked,
Custom(Cow<'a, str>),
}
impl NameAttribute<'static> {
fn system(s: &str) -> Option<Self> {
match s {
"\\Noinferiors" => Some(NameAttribute::NoInferiors),
"\\Noselect" => Some(NameAttribute::NoSelect),
"\\Marked" => Some(NameAttribute::Marked),
"\\Unmarked" => Some(NameAttribute::Unmarked),
_ => None,
}
}
}
impl<'a> From<String> for NameAttribute<'a> {
fn from(s: String) -> Self {
if let Some(f) = NameAttribute::system(&s) {
f
} else {
NameAttribute::Custom(Cow::Owned(s))
}
}
}
impl<'a> From<&'a str> for NameAttribute<'a> {
fn from(s: &'a str) -> Self {
if let Some(f) = NameAttribute::system(s) {
f
} else {
NameAttribute::Custom(Cow::Borrowed(s))
}
}
}
impl Name {
pub(crate) fn from_mailbox_data(resp: ResponseData) -> Self {
Name::new(Box::new(resp), |response| match response.parsed() {
Response::MailboxData(MailboxDatum::List {
flags,
delimiter,
name,
}) => InnerName {
attributes: flags.iter().map(|s| NameAttribute::from(*s)).collect(),
delimiter: *delimiter,
name,
},
_ => panic!("cannot construct from non mailbox data"),
})
}
pub fn attributes(&self) -> &[NameAttribute<'_>] {
&self.suffix().attributes[..]
}
pub fn delimiter(&self) -> Option<&str> {
self.suffix().delimiter
}
pub fn name(&self) -> &str {
self.suffix().name
}
}