use crate::common::args::Cli; use clap::ValueEnum; use std::{fmt::Display, time::Duration}; const DEFAULT_TIMEOUT_LEN: u64 = 5000; // ms #[derive(Debug, Clone, Copy)] pub enum Mode { Tcp, } impl ValueEnum for Mode { fn to_possible_value(&self) -> Option { Some(match self { Self::Tcp => clap::builder::PossibleValue::new("tcp"), }) } 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"), }; write!(f, "{}", repr) } } pub struct Config { pub addr: std::net::SocketAddr, pub mode: Mode, pub threads: usize, pub timeout: Duration, } 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), } } }