use core::ops::BitOr;
use alloc::{borrow::Cow, string::String, vec::Vec};
use crate::rfc5321::types::{atom::Atom, parameter::Parameter};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DsnRet {
Full,
Hdrs,
}
impl DsnRet {
pub fn into_parameter(self) -> Parameter<'static> {
let value = match self {
Self::Full => "FULL",
Self::Hdrs => "HDRS",
};
Parameter {
keyword: Atom(Cow::Borrowed("RET")),
value: Some(Cow::Borrowed(value)),
}
}
}
pub fn envid(id: impl Into<String>) -> Parameter<'static> {
Parameter {
keyword: Atom(Cow::Borrowed("ENVID")),
value: Some(Cow::Owned(id.into())),
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DsnNotify(u8);
impl DsnNotify {
pub const NEVER: Self = Self(0);
pub const SUCCESS: Self = Self(1);
pub const FAILURE: Self = Self(2);
pub const DELAY: Self = Self(4);
#[must_use]
pub const fn or(self, other: Self) -> Self {
Self(self.0 | other.0)
}
pub fn into_parameter(self) -> Parameter<'static> {
let value = if self.0 == 0 {
Cow::Borrowed("NEVER")
} else {
let mut parts: Vec<&str> = Vec::new();
if self.0 & 1 != 0 {
parts.push("SUCCESS");
}
if self.0 & 2 != 0 {
parts.push("FAILURE");
}
if self.0 & 4 != 0 {
parts.push("DELAY");
}
Cow::Owned(parts.join(","))
};
Parameter {
keyword: Atom(Cow::Borrowed("NOTIFY")),
value: Some(value),
}
}
}
impl BitOr for DsnNotify {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
Self(self.0 | rhs.0)
}
}
pub fn orcpt_rfc822(address: impl Into<String>) -> Parameter<'static> {
let value = {
let mut s = String::from("rfc822;");
s.push_str(&address.into());
s
};
Parameter {
keyword: Atom(Cow::Borrowed("ORCPT")),
value: Some(Cow::Owned(value)),
}
}