diff --git a/src/common/args.rs b/src/common/args.rs new file mode 100644 index 0000000..65a1085 --- /dev/null +++ b/src/common/args.rs @@ -0,0 +1,76 @@ +use libpt::log::{Level, Logger}; + +use clap::Parser; +use clap_verbosity_flag::{InfoLevel, Verbosity}; + +use crate::common::conf::Mode; + +/// short about section displayed in help +const ABOUT_ROOT: &'static str = r##" +Let your hosts play ping pong over the network +"##; +/// longer about section displayed in help, is combined with [the short help](ABOUT_ROOT) +static LONG_ABOUT_ROOT: &'static str = r##" + + Connect to a ping pong server and periodically send a ping. The server will reply with a pong. + That's it really. You can also host your own netpong server (server feature). +"##; + +#[derive(Debug, Clone, Parser)] +#[command( + author, + version, + about = ABOUT_ROOT, + long_about = format!("{}{}", ABOUT_ROOT ,LONG_ABOUT_ROOT), + help_template = +r#"{about-section} +{usage-heading} {usage} +{all-args}{tab} + +libpt: {version} +Author: {author-with-newline} +"# + )] +pub(crate) struct Cli { + // clap_verbosity_flag seems to make this a global option implicitly + /// set a verbosity, multiple allowed (f.e. -vvv) + #[command(flatten)] + pub(crate) verbose: Verbosity, + + /// show additional logging meta data + #[arg(long)] + pub(crate) meta: bool, + + #[cfg(feature = "server")] + #[arg(short, long, default_value_t = false)] + pub(crate) server: bool, + + #[arg(short, long, default_value_t = Mode::Tcp, ignore_case = true)] + pub(crate) mode: Mode, + + /// Address of the server + pub(crate) addr: std::net::SocketAddr, +} + +impl Cli { + pub fn cli_parse() -> Self { + let cli = Self::parse(); + let ll: Level = match cli.verbose.log_level().unwrap().as_str() { + "TRACE" => Level::TRACE, + "DEBUG" => Level::DEBUG, + "INFO" => Level::INFO, + "WARN" => Level::WARN, + "ERROR" => Level::ERROR, + _ => { + unreachable!(); + } + }; + if cli.meta { + Logger::init(None, Some(ll)).expect("could not initialize Logger"); + } else { + // less verbose version + Logger::init_mini(Some(ll)).expect("could not initialize Logger"); + } + return cli; + } +} diff --git a/src/common/conf.rs b/src/common/conf.rs new file mode 100644 index 0000000..514f588 --- /dev/null +++ b/src/common/conf.rs @@ -0,0 +1,49 @@ +use std::fmt::Display; +use clap::ValueEnum; +use crate::common::args::Cli; +#[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(crate) struct Config { + cli: Cli, + addr: std::net::SocketAddr, +} + +impl Config { + pub fn new(cli: Cli, addr: std::net::SocketAddr) -> Self { + Config { cli, addr } + } +} diff --git a/src/common/mod.rs b/src/common/mod.rs index 00da770..8d2493b 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -1,38 +1,6 @@ -use std::fmt::Display; -use clap::ValueEnum; -#[derive(Debug, Clone, Copy)] -pub(crate) enum Mode { - Tcp, -} +pub mod args; +use args::*; -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 mod conf; +use conf::*; diff --git a/src/main.rs b/src/main.rs index 83d938c..3b1d87d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,85 +5,14 @@ use libpt::log::*; -use clap::Parser; -use clap_verbosity_flag::{InfoLevel, Verbosity}; - mod client; mod common; mod server; -use common::Mode; - -/// short about section displayed in help -const ABOUT_ROOT: &'static str = r##" -Let your hosts play ping pong over the network -"##; -/// longer about section displayed in help, is combined with [the short help](ABOUT_ROOT) -static LONG_ABOUT_ROOT: &'static str = r##" - - Connect to a ping pong server and periodically send a ping. The server will reply with a pong. - That's it really. You can also host your own netpong server (server feature). -"##; - -#[derive(Debug, Clone, Parser)] -#[command( - author, - version, - about = ABOUT_ROOT, - long_about = format!("{}{}", ABOUT_ROOT ,LONG_ABOUT_ROOT), - help_template = -r#"{about-section} -{usage-heading} {usage} -{all-args}{tab} - -libpt: {version} -Author: {author-with-newline} -"# - )] -pub(crate) struct Cli { - // clap_verbosity_flag seems to make this a global option implicitly - /// set a verbosity, multiple allowed (f.e. -vvv) - #[command(flatten)] - pub(crate) verbose: Verbosity, - - /// show additional logging meta data - #[arg(long)] - pub(crate) meta: bool, - - #[cfg(feature = "server")] - #[arg(short, long, default_value_t = false)] - pub(crate) server: bool, - - #[arg(short, long, default_value_t = Mode::Tcp, ignore_case = true)] - pub(crate) mode: Mode, - - /// Address of the server - pub(crate) addr: std::net::SocketAddr, -} - -fn cli_parse() -> Cli { - let cli = Cli::parse(); - let ll: Level = match cli.verbose.log_level().unwrap().as_str() { - "TRACE" => Level::TRACE, - "DEBUG" => Level::DEBUG, - "INFO" => Level::INFO, - "WARN" => Level::WARN, - "ERROR" => Level::ERROR, - _ => { - unreachable!(); - } - }; - if cli.meta { - Logger::init(None, Some(ll)).expect("could not initialize Logger"); - } else { - // less verbose version - Logger::init_mini(Some(ll)).expect("could not initialize Logger"); - } - return cli; -} +use common::{args::Cli, conf::*}; fn main() { - let cli = cli_parse(); + let cli = Cli::cli_parse(); debug!("dumping cli args:\n{:#?}", cli); #[cfg(feature = "server")] if cli.server {