mirror of
https://github.com/chatmail/relay.git
synced 2026-09-17 04:40:08 +00:00
refactor: Implement Display for AddressDomain
Signed-off-by: Jagoda Ślązak <jslazak@jslazak.com>
This commit is contained in:
@@ -68,10 +68,7 @@ where
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
log::trace!(
|
log::trace!("Trying to acquire a permit for {domain} worker...",);
|
||||||
"Trying to acquire a permit for {} worker...",
|
|
||||||
domain.as_ref()
|
|
||||||
);
|
|
||||||
if let Some(permit) = self.workers.get_permit(&domain) {
|
if let Some(permit) = self.workers.get_permit(&domain) {
|
||||||
transaction.state.permits.insert(domain, permit);
|
transaction.state.permits.insert(domain, permit);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,10 +100,7 @@ where
|
|||||||
if let Some(w) = &worker
|
if let Some(w) = &worker
|
||||||
&& w.handle.is_finished()
|
&& w.handle.is_finished()
|
||||||
{
|
{
|
||||||
log::error!(
|
log::error!("Worker for destination {destination} crashed! Restarting...",);
|
||||||
"Worker for destination {} crashed! Restarting...",
|
|
||||||
destination.as_ref()
|
|
||||||
);
|
|
||||||
worker = None;
|
worker = None;
|
||||||
{
|
{
|
||||||
let mut map = self.inner.write();
|
let mut map = self.inner.write();
|
||||||
@@ -180,10 +177,7 @@ impl Worker {
|
|||||||
.map(|id| id.to_string())
|
.map(|id| id.to_string())
|
||||||
.unwrap_or("?".to_string());
|
.unwrap_or("?".to_string());
|
||||||
|
|
||||||
log::info!(
|
log::info!("Starting worker {worker_id} for destination {destination}");
|
||||||
"Starting worker {worker_id} for destination {}",
|
|
||||||
destination.as_ref()
|
|
||||||
);
|
|
||||||
|
|
||||||
let tls_resumption_store = Arc::new(rustls::client::ClientSessionMemoryCache::new(256));
|
let tls_resumption_store = Arc::new(rustls::client::ClientSessionMemoryCache::new(256));
|
||||||
let https_client = HttpsClient::new(tls_resumption_store.clone())?;
|
let https_client = HttpsClient::new(tls_resumption_store.clone())?;
|
||||||
@@ -206,8 +200,7 @@ impl Worker {
|
|||||||
.await;
|
.await;
|
||||||
if message.response_tx.send(result).is_err() {
|
if message.response_tx.send(result).is_err() {
|
||||||
log::error!(
|
log::error!(
|
||||||
"Worker {worker_id} ({}) failed to send response to transport handler.",
|
"Worker {worker_id} ({destination}) failed to send response to transport handler."
|
||||||
destination.as_ref()
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use hickory_resolver::{TokioResolver, proto::dnssec::TrustAnchors};
|
use hickory_resolver::{TokioResolver, proto::dnssec::TrustAnchors};
|
||||||
use mailparse::MailAddr;
|
use mailparse::MailAddr;
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@@ -88,6 +89,24 @@ impl AsRef<str> for AddressDomain {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Display for AddressDomain {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
AddressDomain::Literal(addr) => {
|
||||||
|
// naive check, but should be enough as long as `AddressDomain`
|
||||||
|
// is constructed using parse/from_str.
|
||||||
|
write!(f, "[")?;
|
||||||
|
if addr.contains(':') {
|
||||||
|
write!(f, "IPv6:")?;
|
||||||
|
}
|
||||||
|
write!(f, "{addr}]")?;
|
||||||
|
}
|
||||||
|
AddressDomain::Name(domain) => write!(f, "{domain}")?,
|
||||||
|
};
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Logs email to `/tmp/filtermail-rejected/<reason>/<timestamp>.eml`
|
/// Logs email to `/tmp/filtermail-rejected/<reason>/<timestamp>.eml`
|
||||||
/// and returns the file path.
|
/// and returns the file path.
|
||||||
///
|
///
|
||||||
@@ -160,4 +179,20 @@ mod tests {
|
|||||||
let result = AddressDomain::from_str(input).ok();
|
let result = AddressDomain::from_str(input).ok();
|
||||||
assert_eq!(result, expected);
|
assert_eq!(result, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[case::domain(AddressDomain::Name("example.org".to_string()), "example.org")]
|
||||||
|
#[case::ipv4(AddressDomain::Literal("192.0.2.0".to_string()), "192.0.2.0")]
|
||||||
|
#[case::ipv6(AddressDomain::Literal("2001:db8::1".to_string()), "2001:db8::1")]
|
||||||
|
fn test_address_domain_as_ref(#[case] input: AddressDomain, #[case] expected: &str) {
|
||||||
|
assert_eq!(input.as_ref(), expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[case::domain(AddressDomain::Name("example.org".to_string()), "example.org")]
|
||||||
|
#[case::ipv4(AddressDomain::Literal("192.0.2.0".to_string()), "[192.0.2.0]")]
|
||||||
|
#[case::ipv6(AddressDomain::Literal("2001:db8::1".to_string()), "[IPv6:2001:db8::1]")]
|
||||||
|
fn test_address_domain_display(#[case] input: AddressDomain, #[case] expected: &str) {
|
||||||
|
assert_eq!(&format!("{input}"), expected);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user