mailspy/src/mail.rs

44 lines
986 B
Rust

use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tokio::sync::Mutex;
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Mail {
pub from: String,
pub to: String,
pub date: String,
pub reply_to: String,
pub subject: String,
pub body: Vec<MailPart>,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct MailPart {
pub content_type: String,
pub data: String,
}
#[derive(Debug, Clone)]
pub struct Mailbox(Arc<Mutex<Vec<Mail>>>);
impl Mailbox {
pub fn new() -> Self {
Self(Arc::new(Mutex::new(vec![])))
}
pub async fn store(&self, mail: Mail) {
let mut inner = self.0.lock().await;
inner.push(mail);
tracing::info!("New message stored");
}
pub async fn clear(&self) {
let mut inner = self.0.lock().await;
inner.clear();
}
pub async fn all(&self) -> Vec<Mail> {
let inner = self.0.lock().await;
inner.clone()
}
}