Use of associated types + Identity and EmailSubmission helpers.

main
Mauro D 2022-06-01 14:19:02 +00:00
parent 1b27e6e146
commit 70df0e341c
4 changed files with 75 additions and 21 deletions

View File

@ -1,4 +1,6 @@
use crate::{Get, core::get::GetObject, Set}; use std::collections::HashMap;
use crate::{core::get::GetObject, Get, Set};
use super::{Address, Delivered, DeliveryStatus, Displayed, EmailSubmission, UndoStatus}; use super::{Address, Delivered, DeliveryStatus, Displayed, EmailSubmission, UndoStatus};
@ -7,6 +9,10 @@ impl EmailSubmission<Get> {
self.id.as_ref().unwrap() self.id.as_ref().unwrap()
} }
pub fn unwrap_id(self) -> String {
self.id.unwrap()
}
pub fn identity_id(&self) -> &str { pub fn identity_id(&self) -> &str {
self.identity_id.as_ref().unwrap() self.identity_id.as_ref().unwrap()
} }
@ -35,10 +41,14 @@ impl EmailSubmission<Get> {
self.undo_status.as_ref().unwrap() self.undo_status.as_ref().unwrap()
} }
pub fn delivery_status(&self, email: &str) -> Option<&DeliveryStatus> { pub fn delivery_status_email(&self, email: &str) -> Option<&DeliveryStatus> {
self.delivery_status.as_ref().and_then(|ds| ds.get(email)) self.delivery_status.as_ref().and_then(|ds| ds.get(email))
} }
pub fn delivery_status(&self) -> Option<&HashMap<String, DeliveryStatus>> {
self.delivery_status.as_ref()
}
pub fn dsn_blob_ids(&self) -> Option<&[String]> { pub fn dsn_blob_ids(&self) -> Option<&[String]> {
self.dsn_blob_ids.as_deref() self.dsn_blob_ids.as_deref()
} }
@ -66,6 +76,15 @@ impl Address<Get> {
} }
impl DeliveryStatus { impl DeliveryStatus {
#[cfg(feature = "debug")]
pub fn new(smtp_reply: impl Into<String>, delivered: Delivered, displayed: Displayed) -> Self {
Self {
smtp_reply: smtp_reply.into(),
delivered,
displayed,
}
}
pub fn smtp_reply(&self) -> &str { pub fn smtp_reply(&self) -> &str {
&self.smtp_reply &self.smtp_reply
} }

View File

@ -34,14 +34,15 @@ impl Client {
.created(&id) .created(&id)
} }
pub async fn email_submission_create_envelope<T, U>( pub async fn email_submission_create_envelope<S, T, U>(
&mut self, &mut self,
email_id: impl Into<String>, email_id: impl Into<String>,
identity_id: impl Into<String>, identity_id: impl Into<String>,
mail_from: U, mail_from: S,
rcpt_to: T, rcpt_to: T,
) -> crate::Result<EmailSubmission<Get>> ) -> crate::Result<EmailSubmission<Get>>
where where
S: Into<Address>,
T: IntoIterator<Item = U>, T: IntoIterator<Item = U>,
U: Into<Address>, U: Into<Address>,
{ {

View File

@ -89,7 +89,7 @@ pub struct Address<State = Get> {
parameters: Option<HashMap<String, Option<String>>>, parameters: Option<HashMap<String, Option<String>>>,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum UndoStatus { pub enum UndoStatus {
#[serde(rename = "pending")] #[serde(rename = "pending")]
Pending, Pending,
@ -99,7 +99,7 @@ pub enum UndoStatus {
Canceled, Canceled,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DeliveryStatus { pub struct DeliveryStatus {
#[serde(rename = "smtpReply")] #[serde(rename = "smtpReply")]
smtp_reply: String, smtp_reply: String,
@ -111,7 +111,7 @@ pub struct DeliveryStatus {
displayed: Displayed, displayed: Displayed,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum Delivered { pub enum Delivered {
#[serde(rename = "queued")] #[serde(rename = "queued")]
Queued, Queued,
@ -123,7 +123,7 @@ pub enum Delivered {
Unknown, Unknown,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum Displayed { pub enum Displayed {
#[serde(rename = "unknown")] #[serde(rename = "unknown")]
Unknown, Unknown,

View File

@ -15,15 +15,13 @@ impl EmailSubmission<Set> {
self self
} }
pub fn envelope<T, U>(&mut self, mail_from: U, rcpt_to: T) -> &mut Self pub fn envelope<S, T, U>(&mut self, mail_from: S, rcpt_to: T) -> &mut Self
where where
S: Into<Address>,
T: IntoIterator<Item = U>, T: IntoIterator<Item = U>,
U: Into<Address>, U: Into<Address>,
{ {
self.envelope = Some(Envelope { self.envelope = Some(Envelope::new(mail_from, rcpt_to));
mail_from: mail_from.into(),
rcpt_to: rcpt_to.into_iter().map(|s| s.into()).collect(),
});
self self
} }
@ -70,21 +68,37 @@ impl SetObject for EmailSubmission<Get> {
} }
} }
impl Address { impl Envelope {
pub fn new(email: String) -> Address<Set> { pub fn new<S, T, U>(mail_from: S, rcpt_to: T) -> Envelope
Address { where
_state: Default::default(), S: Into<Address>,
email, T: IntoIterator<Item = U>,
parameters: None, U: Into<Address>,
{
Envelope {
mail_from: mail_from.into(),
rcpt_to: rcpt_to.into_iter().map(|s| s.into()).collect(),
} }
} }
} }
impl Address<Set> { impl Address<Set> {
pub fn parameter(mut self, parameter: String, value: Option<String>) -> Self { pub fn new(email: impl Into<String>) -> Address<Set> {
Address {
_state: Default::default(),
email: email.into(),
parameters: None,
}
}
pub fn parameter(
mut self,
parameter: impl Into<String>,
value: Option<impl Into<String>>,
) -> Self {
self.parameters self.parameters
.get_or_insert_with(HashMap::new) .get_or_insert_with(HashMap::new)
.insert(parameter, value); .insert(parameter.into(), value.map(|s| s.into()));
self self
} }
} }
@ -108,3 +122,23 @@ impl From<&str> for Address {
} }
} }
} }
impl From<Address<Set>> for Address<Get> {
fn from(addr: Address<Set>) -> Self {
Address {
_state: Default::default(),
email: addr.email,
parameters: addr.parameters,
}
}
}
impl From<Address<Get>> for Address<Set> {
fn from(addr: Address<Get>) -> Self {
Address {
_state: Default::default(),
email: addr.email,
parameters: addr.parameters,
}
}
}