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
pub mod email;
pub mod file;
pub mod http;
pub mod twitter;
pub use self::email::Email;
pub use self::file::File;
pub use self::http::Http;
pub use self::twitter::Twitter;
use crate::entry::Entry;
use crate::error::source::EmailError;
use crate::error::source::Error as SourceError;
use crate::read_filter::ReadFilter;
use std::sync::Arc;
use tokio::sync::RwLock;
#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
pub enum Source {
WithSharedReadFilter {
rf: Option<Arc<RwLock<ReadFilter>>>,
kind: WithSharedRF,
},
WithCustomReadFilter(WithCustomRF),
}
#[derive(Debug)]
pub struct WithSharedRF(Vec<WithSharedRFKind>);
#[derive(Debug)]
pub enum WithSharedRFKind {
File(File),
Http(Http),
Twitter(Twitter),
}
#[derive(Debug)]
pub enum WithCustomRF {
Email(Email),
}
impl Source {
pub async fn get(
&mut self,
) -> Result<Vec<Entry>, SourceError> {
match self {
Source::WithSharedReadFilter { kind, .. } => kind.get().await,
Source::WithCustomReadFilter(x) => x.get().await,
}
}
}
impl WithSharedRF {
pub fn new(sources: Vec<WithSharedRFKind>) -> Result<Self, SourceError> {
match sources.len() {
0 => return Err(SourceError::EmptySourceList),
1 => (),
_ => {
for variants in sources.windows(2) {
use std::mem::discriminant as disc;
if disc(&variants[0]) != disc(&variants[1]) {
return Err(SourceError::SourceListHasDifferentVariants);
}
}
}
}
Ok(Self(sources))
}
pub async fn get(&mut self) -> Result<Vec<Entry>, SourceError> {
use WithSharedRFKind as K;
let mut entries = Vec::new();
for s in &mut self.0 {
entries.extend(match s {
K::Http(x) => vec![x.get().await?],
K::Twitter(x) => x.get().await?,
K::File(x) => vec![x.get().await?],
});
}
Ok(entries)
}
}
impl WithCustomRF {
pub async fn get(&mut self) -> Result<Vec<Entry>, SourceError> {
Ok(match self {
Self::Email(x) => x.get().await?,
})
}
#[allow(clippy::missing_errors_doc)]
pub async fn mark_as_read(&mut self, id: &str) -> Result<(), SourceError> {
match self {
Self::Email(x) => x
.mark_as_read(id)
.await
.map_err(|e| Box::new(EmailError::Imap(e)))?,
};
Ok(())
}
#[allow(clippy::ptr_arg)]
pub fn remove_read(&self, _entries: &mut Vec<Entry>) {
match self {
Self::Email(_) => (), }
}
}
impl TryFrom<Vec<WithSharedRFKind>> for WithSharedRF {
type Error = SourceError;
fn try_from(value: Vec<WithSharedRFKind>) -> Result<Self, Self::Error> {
Self::new(value)
}
}
impl std::ops::Deref for WithSharedRF {
type Target = [WithSharedRFKind];
fn deref(&self) -> &Self::Target {
self.0.as_slice()
}
}