add umbrella crate

This commit is contained in:
Aleksandr 2025-09-20 22:55:40 +03:00
parent d171fc723b
commit fe04530f84
17 changed files with 247 additions and 5 deletions

12
email/Cargo.toml Normal file
View file

@ -0,0 +1,12 @@
[package]
name = "viendesu-email"
version = "0.1.0"
edition = "2024"
[features]
default = ["smtp"]
smtp = []
[dependencies]
viendesu-core.workspace = true
eva.workspace = true

11
email/src/error.rs Normal file
View file

@ -0,0 +1,11 @@
use viendesu_core::{errors::Aux, mk_error};
pub type MailResult<O> = Result<O, MailError>;
mk_error!(MailError);
impl From<MailError> for Aux {
fn from(value: MailError) -> Self {
Self::Mail(eva::str::format_compact!("{}", value.0))
}
}

20
email/src/lib.rs Normal file
View file

@ -0,0 +1,20 @@
use eva::{data, fut::Fut};
use std::borrow::Cow;
pub mod mock;
pub mod smtp;
pub mod error;
#[data]
pub struct Letter<'a> {
pub subject: Cow<'a, str>,
pub contents: Cow<'a, str>,
pub content_type: Cow<'a, str>,
}
#[eva::auto_impl(&, &mut, Arc, Box)]
pub trait Mailer: Send + Sync {
fn send(&self, dst: &str, letter: Letter<'_>) -> impl Fut<Output = error::MailResult<()>>;
}

43
email/src/mock.rs Normal file
View file

@ -0,0 +1,43 @@
use std::{
borrow::Cow,
sync::{Arc, Mutex},
};
use eva::{
collections::HashMap,
str::{CompactString, ToCompactString},
};
use crate::{Letter, Mailer, error::MailResult};
pub type Mailbox = Vec<Letter<'static>>;
pub type Letters = HashMap<CompactString, Mailbox>;
#[derive(Debug, Clone, Default)]
pub struct Mock(Arc<Mutex<Letters>>);
impl Mock {
pub fn collect_letters(&self) -> Letters {
std::mem::take(&mut *self.0.lock().unwrap())
}
}
fn owned(c: Cow<'_, str>) -> Cow<'static, str> {
Cow::Owned((*c).to_owned())
}
impl Mailer for Mock {
async fn send(&self, dst: &str, letter: Letter<'_>) -> MailResult<()> {
let dst = dst.to_compact_string();
let mut this = self.0.lock().unwrap();
let mailbox = this.entry(dst).or_default();
mailbox.push(Letter {
subject: owned(letter.subject),
contents: owned(letter.contents),
content_type: owned(letter.content_type),
});
Ok(())
}
}

1
email/src/smtp.rs Normal file
View file

@ -0,0 +1 @@
pub struct Smtp;