use std::fmt;
use std::num::NonZeroU32;
use crate::error::{CoreError, ParseError};
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct MailboxPath(String);
impl MailboxPath {
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl TryFrom<&str> for MailboxPath {
type Error = ParseError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
if value.is_empty()
|| value.len() > 1024
|| value.contains("://")
|| value.starts_with("//")
|| value.chars().any(char::is_control)
{
return Err(ParseError::MailboxPath);
}
Ok(Self(value.to_owned()))
}
}
impl fmt::Display for MailboxPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Uid(NonZeroU32);
impl Uid {
#[must_use]
pub const fn get(self) -> u32 {
self.0.get()
}
}
impl TryFrom<u32> for Uid {
type Error = ParseError;
fn try_from(value: u32) -> Result<Self, Self::Error> {
NonZeroU32::new(value).map(Self).ok_or(ParseError::Uid)
}
}
impl fmt::Display for Uid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.get())
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct UidValidity(NonZeroU32);
impl UidValidity {
#[must_use]
pub const fn get(self) -> u32 {
self.0.get()
}
}
impl TryFrom<u32> for UidValidity {
type Error = ParseError;
fn try_from(value: u32) -> Result<Self, Self::Error> {
NonZeroU32::new(value).map(Self).ok_or(ParseError::Uid)
}
}
impl fmt::Display for UidValidity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.get())
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct MessageId {
pub mailbox: MailboxPath,
pub validity: UidValidity,
pub uid: Uid,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MailboxRole {
Inbox,
Sent,
Drafts,
Trash,
Junk,
Archive,
Notes,
Other,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Mailbox {
pub path: MailboxPath,
pub role: MailboxRole,
pub total: u32,
pub unread: u32,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MailSearch {
pub mailbox: Option<MailboxPath>,
pub role: Option<MailboxRole>,
pub from: Option<String>,
pub to: Option<String>,
pub subject: Option<String>,
pub text: Option<String>,
pub since: Option<String>,
pub before: Option<String>,
pub unread_only: bool,
pub flagged_only: bool,
pub limit: u32,
pub cursor: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MessageSummary {
pub id: MessageId,
pub from: String,
pub subject: String,
pub date: String,
pub unread: bool,
pub flagged: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MailPage {
pub messages: Vec<MessageSummary>,
pub next_cursor: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AttachmentInfo {
pub part: String,
pub content_type: String,
pub size: u64,
pub name: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Message {
pub id: MessageId,
pub from: String,
pub to: Vec<String>,
pub subject: String,
pub date: String,
pub body: String,
pub attachments: Vec<AttachmentInfo>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Attachment {
pub info: AttachmentInfo,
pub text: Option<String>,
pub bytes: Option<Vec<u8>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Draft {
pub to: Vec<String>,
pub subject: String,
pub body: String,
pub from: Option<String>,
pub reply_to: Option<MessageId>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FlagChange {
pub seen: Option<bool>,
pub flagged: Option<bool>,
}
#[trait_variant::make(Send)]
pub trait MailProvider {
async fn list_mailboxes(&self) -> Result<Vec<Mailbox>, CoreError>;
async fn search(&self, query: &MailSearch) -> Result<MailPage, CoreError>;
async fn get_message(&self, id: &MessageId) -> Result<Message, CoreError>;
async fn get_attachment(&self, id: &MessageId, part: &str) -> Result<Attachment, CoreError>;
async fn create_draft(&self, draft: &Draft) -> Result<MessageId, CoreError>;
async fn send(&self, draft: &Draft) -> Result<MessageId, CoreError>;
async fn move_to(&self, id: &MessageId, destination: &MailboxPath) -> Result<(), CoreError>;
async fn set_flags(&self, id: &MessageId, flags: &FlagChange) -> Result<(), CoreError>;
}