1use std::collections::HashMap;
2use std::fs::File;
3use std::io::{Read, Write};
4use std::path::Path;
5use serde::{Serialize, Deserialize};
6use serde_json;
7
8#[derive(Serialize, Deserialize, Clone)]
9pub struct EmailAccount {
10 email: String,
11 password: String,
12 services: HashMap<String, bool>,
13}
14
15impl EmailAccount {
16 pub fn new(email: String, password: String) -> EmailAccount {
17 EmailAccount {
18 email,
19 password,
20 services: HashMap::new(),
21 }
22 }
23
24 pub fn add_service(&mut self, service: String) {
25 self.services.insert(service, true);
26 }
27
28 pub fn remove_service(&mut self, service: String) {
29 self.services.remove(&service);
30 }
31
32 pub fn get_services(&self) -> Vec<String> {
33 self.services.keys().map(|service| service.clone()).collect()
34 }
35
36 pub fn get_email(&self) -> String {
37 self.email.clone()
38 }
39
40 pub fn get_password(&self) -> String {
41 self.password.clone()
42 }
43
44}
45
46#[derive(Serialize, Deserialize)]
47pub struct EmailManager {
48 accounts: HashMap<String, EmailAccount>,
49}
50
51impl EmailManager {
52 pub fn new() -> EmailManager {
53 EmailManager {
54 accounts: HashMap::new(),
55 }
56 }
57
58 pub fn load(path: &Path) -> EmailManager {
59 if path.exists() {
60 let mut file = File::open(path).unwrap();
61 let mut data = String::new();
62 file.read_to_string(&mut data).unwrap();
63 serde_json::from_str(&data).unwrap()
64 } else {
65 EmailManager::new()
66 }
67 }
68
69 pub fn save(&self, path: &Path) {
70 let data = serde_json::to_string(self).unwrap();
71 let mut file = File::create(path).unwrap();
72 file.write_all(data.as_bytes()).unwrap();
73 }
74
75 pub fn add_account(&mut self, email: String, password: String) {
76 self.accounts.insert(email.clone(), EmailAccount::new(email, password));
77 }
78
79 pub fn remove_account(&mut self, email: String) {
80 self.accounts.remove(&email);
81 }
82
83 pub fn get_account(&mut self, email: String) -> Option<&mut EmailAccount> {
84 self.accounts.get_mut(&email)
85 }
86
87 pub fn get_accounts(&self) -> Vec<EmailAccount> {
88 self.accounts.values().map(|account| account.clone()).collect()
89 }
90
91 pub fn get_email_without_service(&self, service: String) -> Vec<EmailAccount> {
92 self.accounts.iter().filter_map(|(_, account)| {
93 if account.services.get(&service).is_none() {
94 Some(account.clone())
95 } else {
96 None
97 }
98 }).collect()
99 }
100}
101
102
103
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[test]
110 fn test_email_account() {
111 let mut account = EmailAccount::new("123456@gmail.com".to_string(), "password".to_string());
112 account.add_service("keeta".to_string());
113 account.add_service("gmail".to_string());
114 account.remove_service("gmail".to_string());
115
116 assert_eq!(account.email, "123456@gmail.com");
117 assert_eq!(account.password, "password");
118 assert_eq!(account.services.get("keeta"), Some(&true));
119 assert_eq!(account.services.get("gmail"), None);
120 }
121
122 #[test]
123 fn test_email_manager() {
124 let mut manager = EmailManager::new();
125 manager.add_account("123456@gmail.com".to_string(), "password".to_string() );
126 manager.add_account("654321@outlook.com".to_string(), "password".to_string() );
127 manager.get_account("123456@gmail.com".to_string())
128 .unwrap()
129 .add_service("keeta".to_string());
130 manager.get_account("654321@outlook.com".to_string())
131 .unwrap()
132 .add_service("gmail".to_string());
133
134 assert_eq!(manager.accounts.len(), 2);
135 assert_eq!(manager.accounts.get("123456@gmail.com").unwrap().services.get("keeta"), Some(&true));
136
137 manager.save(Path::new("test.json"));
138 let manager2 = EmailManager::load(Path::new("test.json"));
139 assert_eq!(manager2.accounts.len(), 2);
140 assert_eq!(manager2.accounts.get("123456@gmail.com").unwrap().services.get("keeta"), Some(&true));
141
142 let accounts = manager2.get_email_without_service("keeta".to_string());
143 assert_eq!(accounts.len(), 1);
144 assert!(accounts.iter().all(|account| account.email == "654321@outlook.com"));
145
146 }
147
148}