#![forbid(unsafe_code)]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
use std::path::Path;
use async_impl::{send_email::send_email, token::retrieve_token};
use error::Result;
use service_account::ServiceAccount;
pub mod error;
mod async_impl;
mod common;
mod service_account;
#[cfg(feature = "blocking")]
mod blocking;
#[derive(Debug, Clone)]
pub struct GmailClientBuilder {
service_account: ServiceAccount,
send_from_email: String,
mock_mode: bool,
}
impl<'a> GmailClientBuilder {
pub fn new<P: AsRef<Path>, S: Into<String>>(
service_account_path: P,
send_from_email: S,
) -> Result<Self> {
Ok(Self {
service_account: ServiceAccount::load_from_file(service_account_path)?,
send_from_email: send_from_email.into(),
mock_mode: false,
})
}
pub fn mock_mode(mut self, enabled: bool) -> Self {
self.mock_mode = enabled;
self
}
pub async fn build(self) -> Result<GmailClient> {
let token = retrieve_token(&self.service_account, &self.send_from_email).await?;
Ok(GmailClient {
send_from_email: self.send_from_email,
token,
mock_mode: self.mock_mode,
})
}
#[cfg(feature = "blocking")]
pub fn build_blocking(self) -> Result<GmailClient> {
use blocking::token::retrieve_token_blocking;
let token = retrieve_token_blocking(&self.service_account, &self.send_from_email)?;
Ok(GmailClient {
send_from_email: self.send_from_email,
token,
mock_mode: self.mock_mode,
})
}
}
#[derive(Debug, Clone)]
pub struct GmailClient {
send_from_email: String,
token: String,
mock_mode: bool,
}
impl GmailClient {
pub fn builder<P: AsRef<Path>, S: Into<String>>(
service_account_path: P,
send_from_email: S,
) -> Result<GmailClientBuilder> {
GmailClientBuilder::new(service_account_path, send_from_email)
}
pub async fn send_email(
&self,
send_to_email: &str,
subject: &str,
content: &str,
) -> Result<()> {
send_email(
send_to_email,
subject,
content,
&self.token,
&self.send_from_email,
self.mock_mode,
)
.await
}
#[cfg(feature = "blocking")]
pub fn send_email_blocking(
&self,
send_to_email: &str,
subject: &str,
content: &str,
) -> Result<()> {
use blocking::send_email::send_email_blocking;
send_email_blocking(
send_to_email,
subject,
content,
&self.token,
&self.send_from_email,
self.mock_mode,
)
}
}