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 std::{
collections::{BTreeMap, HashMap},
collections::HashMap,
str::FromStr,
};
@ -15,10 +15,14 @@ pub trait MailStore {
/// Checks whether this server has the given mailuser
fn has_mailuser(&self, mailuser: &str) -> bool;
/// 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
/// 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
fn add_user(&mut self, user: &str) -> Result<(), ParseMailboxError>;
/// Removes the given user if the account exists
@ -36,7 +40,14 @@ pub struct Domain {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Account {
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)]
@ -64,19 +75,23 @@ impl MailStore for Domain {
.is_some()
}
fn get_user_messages(&self, user: &str) -> Option<BTreeMap<String, Message>> {
self.users.get(user).cloned().map(|u| u.messages)
}
fn get_message(&self, user: &str, title: &str) -> Option<Message> {
fn get_folder(&self, user: &str, folder: &str) -> Option<Folder> {
self.users
.get(user)
.map(|u| {
u.messages
.cloned()
.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()
.find(|m| m.title.as_ref().map(|x| x.as_str()) == Some(title))
})
.flatten()
})
.cloned()
}
@ -85,10 +100,10 @@ impl MailStore for Domain {
return Ok(());
}
let mbox = Mailbox::from_str(&format!("{user}@{}", self.host.to_string()))?;
let messages = BTreeMap::new();
let folders = HashMap::new();
let acct = Account {
user: mbox,
messages,
folders,
};
self.users.insert(user.to_string(), acct);
Ok(())
@ -97,4 +112,12 @@ impl MailStore for Domain {
fn remove_user(&mut self, user: &str) -> bool {
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)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Message {
pub id: String,
pub senders: Vec<Mailbox>,
pub recipients: Vec<Mailbox>,
pub timstamp: Option<String>,