refactor: use enum for mode cli arg (#83)

This commit is contained in:
Andrey 0xdc09
2026-03-05 10:17:37 +01:00
committed by GitHub
parent 3a571b7aa7
commit c088cbfe4d
+26 -5
View File
@@ -46,6 +46,24 @@ use std::sync::Arc;
const ENCRYPTION_NEEDED_523: &str = "523 Encryption Needed: Invalid Unencrypted Mail"; const ENCRYPTION_NEEDED_523: &str = "523 Encryption Needed: Invalid Unencrypted Mail";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Mode {
Outgoing,
Incoming,
}
impl std::str::FromStr for Mode {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"outgoing" => Ok(Mode::Outgoing),
"incoming" => Ok(Mode::Incoming),
_ => Err("Error: mode must be 'incoming' or 'outgoing'"),
}
}
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// default to info level // default to info level
@@ -72,10 +90,13 @@ async fn main() {
unreachable!("args length checked above") unreachable!("args length checked above")
}; };
if mode != "incoming" && mode != "outgoing" { let mode = match mode.parse::<Mode>() {
eprintln!("Error: mode must be 'incoming' or 'outgoing'"); Ok(mode) => mode,
process::exit(1); Err(e) => {
} eprintln!("{e}");
process::exit(1);
}
};
let config = match Config::from_file(config_path) { let config = match Config::from_file(config_path) {
Ok(c) => c, Ok(c) => c,
@@ -85,7 +106,7 @@ async fn main() {
} }
}; };
if mode == "outgoing" { if mode == Mode::Outgoing {
let handler = Arc::new(OutgoingBeforeQueueHandler::new(config.clone())); let handler = Arc::new(OutgoingBeforeQueueHandler::new(config.clone()));
let addr = format!("127.0.0.1:{}", config.filtermail_smtp_port); let addr = format!("127.0.0.1:{}", config.filtermail_smtp_port);
let max_size = config.max_message_size; let max_size = config.max_message_size;