Merge pull request #406 from amousset/fixes
* chore(all): Code cleanup * fix(transport-smtp): Set root certs when using rustls
This commit is contained in:
@@ -4,7 +4,7 @@ use idna::domain_to_ascii;
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
use std::{
|
||||
convert::TryFrom,
|
||||
convert::{TryFrom, TryInto},
|
||||
error::Error,
|
||||
ffi::OsStr,
|
||||
fmt::{Display, Formatter, Result as FmtResult},
|
||||
@@ -36,7 +36,16 @@ where
|
||||
|
||||
fn try_from(from: (U, D)) -> Result<Self, Self::Error> {
|
||||
let (user, domain) = from;
|
||||
Self::new(user, domain)
|
||||
let user = user.into();
|
||||
Address::check_user(&user)?;
|
||||
let domain = domain.into();
|
||||
Address::check_domain(&domain)?;
|
||||
let complete = format!("{}@{}", &user, &domain);
|
||||
Ok(Address {
|
||||
user,
|
||||
domain,
|
||||
complete,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,19 +67,10 @@ impl Address {
|
||||
/// Create email address from parts
|
||||
#[inline]
|
||||
pub fn new<U: Into<String>, D: Into<String>>(user: U, domain: D) -> Result<Self, AddressError> {
|
||||
let user = user.into();
|
||||
Address::check_user(&user)?;
|
||||
let domain = domain.into();
|
||||
Address::check_domain(&domain)?;
|
||||
let complete = format!("{}@{}", &user, &domain);
|
||||
Ok(Address {
|
||||
user,
|
||||
domain,
|
||||
complete,
|
||||
})
|
||||
(user, domain).try_into()
|
||||
}
|
||||
|
||||
pub fn check_user(user: &str) -> Result<(), AddressError> {
|
||||
fn check_user(user: &str) -> Result<(), AddressError> {
|
||||
if USER_RE.is_match(user) {
|
||||
Ok(())
|
||||
} else {
|
||||
@@ -78,7 +78,7 @@ impl Address {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_domain(domain: &str) -> Result<(), AddressError> {
|
||||
fn check_domain(domain: &str) -> Result<(), AddressError> {
|
||||
Address::check_domain_ascii(domain).or_else(|_| {
|
||||
domain_to_ascii(domain)
|
||||
.map_err(|_| AddressError::InvalidDomain)
|
||||
@@ -280,3 +280,5 @@ pub mod serde {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME test serializer deserializer
|
||||
|
||||
@@ -3,6 +3,7 @@ use std::{
|
||||
fmt::{self, Display, Formatter},
|
||||
};
|
||||
|
||||
// FIXME message-specific errors
|
||||
/// Error type for email content
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
@@ -45,8 +46,4 @@ impl From<std::io::Error> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl StdError for Error {
|
||||
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
||||
None
|
||||
}
|
||||
}
|
||||
impl StdError for Error {}
|
||||
|
||||
@@ -36,6 +36,8 @@ use std::{
|
||||
time::Duration,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
#[cfg(feature = "rustls")]
|
||||
use webpki_roots::TLS_SERVER_ROOTS;
|
||||
|
||||
pub mod authentication;
|
||||
pub mod client;
|
||||
@@ -168,7 +170,12 @@ impl SmtpClient {
|
||||
|
||||
#[cfg(feature = "rustls")]
|
||||
pub fn new_simple(domain: &str) -> Result<SmtpClient, Error> {
|
||||
let tls_parameters = ClientTlsParameters::new(domain.to_string(), ClientConfig::new());
|
||||
let mut tls = ClientConfig::new();
|
||||
tls.config
|
||||
.root_store
|
||||
.add_server_trust_anchors(&TLS_SERVER_ROOTS);
|
||||
|
||||
let tls_parameters = ClientTlsParameters::new(domain.to_string(), tls);
|
||||
|
||||
SmtpClient::new(
|
||||
(domain, SUBMISSIONS_PORT),
|
||||
|
||||
Reference in New Issue
Block a user