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
use std::collections::HashMap;
use chrono::{DateTime, Utc};
#[derive(Debug, PartialEq)]
pub struct Message {
pub id: Option<usize>,
pub size: usize,
pub subject: Option<String>,
pub sender: Option<String>,
pub recipients: Vec<String>,
pub created_at: DateTime<Utc>,
pub typ: String,
pub parts: Vec<MessagePart>,
pub charset: String,
pub source: Vec<u8>,
}
impl Message {
pub fn plain(&self) -> Option<&MessagePart> {
return self.parts.iter().find(|&p| p.typ == "text/plain");
}
pub fn html(&self) -> Option<&MessagePart> {
return self
.parts
.iter()
.find(|&p| p.typ == "text/html" || p.typ == "application/xhtml+xml");
}
}
#[derive(Debug, PartialEq)]
pub struct MessagePart {
pub cid: String,
pub typ: String,
pub filename: String,
pub size: usize,
pub charset: String,
pub body: Vec<u8>,
pub is_attachment: bool,
}
pub struct MessageRepository {
last_insert_id: usize,
messages: HashMap<usize, Message>,
}
impl MessageRepository {
pub fn new() -> Self {
MessageRepository {
last_insert_id: 0,
messages: HashMap::new(),
}
}
pub fn persist(&mut self, mut message: Message) {
let id = self.last_insert_id + 1;
self.last_insert_id += 1;
message.id = Some(id);
self.messages.insert(id, message);
}
pub fn find_all(&self) -> Vec<&Message> {
self.messages.values().collect()
}
pub fn find(&self, id: usize) -> Option<&Message> {
self.messages.get(&id)
}
pub fn delete_all(&mut self) {
self.messages.clear();
self.last_insert_id = 0;
}
pub fn delete(&mut self, id: usize) -> Option<Message> {
self.messages.remove(&id)
}
}