use crate::common::args::Cli; use clap::ValueEnum; use std::{fmt::Display, time::Duration}; const DEFAULT_TIMEOUT_LEN: u64 = 5000; // ms const DEFAULT_DELAY_LEN: u64 = 500; // ms const DEFAULT_WIN_AFTER: usize = 20; #[derive(Debug, Clone, Copy)] pub enum Mode { Tcp, Tls, } impl ValueEnum for Mode { fn to_possible_value(&self) -> Option { Some(match self { Self::Tcp => clap::builder::PossibleValue::new("tcp"), Self::Tls => clap::builder::PossibleValue::new("tls"), }) } fn value_variants<'a>() -> &'a [Self] { &[Self::Tcp] } fn from_str(input: &str, ignore_case: bool) -> Result { let comp: String = if ignore_case { input.to_lowercase() } else { input.to_string() }; match comp.as_str() { "tcp" => return Ok(Self::Tcp), _ => return Err(format!("\"{input}\" is not a valid mode")), } } } impl Display for Mode { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let repr: String = match self { Self::Tcp => format!("tcp"), Self::Tls => format!("tls"), }; write!(f, "{}", repr) } } pub struct Config { pub addr: std::net::SocketAddr, pub mode: Mode, pub threads: usize, pub timeout: Duration, pub delay: Duration, pub win_after: usize, } impl Config { pub fn new(cli: &Cli) -> Self { Config { addr: cli.addr.clone(), mode: cli.mode.clone(), threads: cli.threads, timeout: Duration::from_millis(DEFAULT_TIMEOUT_LEN), delay: Duration::from_millis(DEFAULT_DELAY_LEN), win_after: DEFAULT_WIN_AFTER, } } }