Some tweaks to MailStore trait

This commit is contained in:
Nathan Fisher 2023-05-28 00:18:15 -04:00
parent 2afbede154
commit 50644d2d68
2 changed files with 40 additions and 16 deletions

View file

@ -1,6 +1,6 @@
use crate::prelude::{Host, Mailbox, Mailuser, Message, ParseMailboxError}; use crate::prelude::{Host, Mailbox, Mailuser, Message, ParseMailboxError};
use std::{ use std::{
collections::{BTreeMap, HashMap}, collections::HashMap,
str::FromStr, str::FromStr,
}; };
@ -15,10 +15,14 @@ pub trait MailStore {
/// Checks whether this server has the given mailuser /// Checks whether this server has the given mailuser
fn has_mailuser(&self, mailuser: &str) -> bool; fn has_mailuser(&self, mailuser: &str) -> bool;
/// Retreives all of the messages for this user /// Retreives all of the messages for this user
fn get_user_messages(&self, user: &str) -> Option<BTreeMap<String, Message>>; fn get_folder(&self, user: &str, folder: &str) -> Option<Folder>;
/// Checks whether this server has a message with the given title for this user and if so /// Checks whether this server has a message with the given title for this user and if so
/// returns the message /// returns the message
fn get_message(&self, user: &str, title: &str) -> Option<Message>; fn get_message(&self, user: &str, folder: &str, title: &str) -> Option<Message>;
/// Deletes a message it it exists
fn delete_message(&mut self, user: &str, folder: &str, key: &str) -> bool;
/// Adds a message
fn add_message(&mut self, user: &str, folder: &str, message: Message);
/// Adds a new mailuser to the given domain /// Adds a new mailuser to the given domain
fn add_user(&mut self, user: &str) -> Result<(), ParseMailboxError>; fn add_user(&mut self, user: &str) -> Result<(), ParseMailboxError>;
/// Removes the given user if the account exists /// Removes the given user if the account exists
@ -36,7 +40,14 @@ pub struct Domain {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Account { pub struct Account {
pub user: Mailbox, pub user: Mailbox,
pub messages: BTreeMap<String, Message>, pub folders: HashMap<String, Folder>,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Folder {
pub name: String,
pub messages: HashMap<String, Message>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -64,19 +75,23 @@ impl MailStore for Domain {
.is_some() .is_some()
} }
fn get_user_messages(&self, user: &str) -> Option<BTreeMap<String, Message>> { fn get_folder(&self, user: &str, folder: &str) -> Option<Folder> {
self.users.get(user).cloned().map(|u| u.messages)
}
fn get_message(&self, user: &str, title: &str) -> Option<Message> {
self.users self.users
.get(user) .get(user)
.map(|u| { .cloned()
u.messages .and_then(|u| u.folders.get(folder).cloned())
}
fn get_message(&self, user: &str, folder: &str, title: &str) -> Option<Message> {
self.users
.get(user)
.and_then(|u| {
u.folders.get(folder).and_then(|f| {
f.messages
.values() .values()
.find(|m| m.title.as_ref().map(|x| x.as_str()) == Some(title)) .find(|m| m.title.as_ref().map(|x| x.as_str()) == Some(title))
}) })
.flatten() })
.cloned() .cloned()
} }
@ -85,10 +100,10 @@ impl MailStore for Domain {
return Ok(()); return Ok(());
} }
let mbox = Mailbox::from_str(&format!("{user}@{}", self.host.to_string()))?; let mbox = Mailbox::from_str(&format!("{user}@{}", self.host.to_string()))?;
let messages = BTreeMap::new(); let folders = HashMap::new();
let acct = Account { let acct = Account {
user: mbox, user: mbox,
messages, folders,
}; };
self.users.insert(user.to_string(), acct); self.users.insert(user.to_string(), acct);
Ok(()) Ok(())
@ -97,4 +112,12 @@ impl MailStore for Domain {
fn remove_user(&mut self, user: &str) -> bool { fn remove_user(&mut self, user: &str) -> bool {
self.users.remove(user).is_some() self.users.remove(user).is_some()
} }
fn add_message(&mut self, user: &str, folder: &str, message: Message) {
todo!()
}
fn delete_message(&mut self, user: &str, folder: &str, key: &str) -> bool {
todo!()
}
} }

View file

@ -56,6 +56,7 @@ impl fmt::Display for Lines {
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Message { pub struct Message {
pub id: String,
pub senders: Vec<Mailbox>, pub senders: Vec<Mailbox>,
pub recipients: Vec<Mailbox>, pub recipients: Vec<Mailbox>,
pub timstamp: Option<String>, pub timstamp: Option<String>,