From 085223d0a6cc0c0a86bd0052463d79359808de61 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Thu, 18 Jan 2024 23:45:50 +0100 Subject: [PATCH] continues cli interface, mode enum --- src/client/mod.rs | 0 src/common/mod.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/main.rs | 36 ++++++++++++++++++++++++------------ src/server/mod.rs | 0 4 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 src/client/mod.rs create mode 100644 src/common/mod.rs create mode 100644 src/server/mod.rs diff --git a/src/client/mod.rs b/src/client/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/common/mod.rs b/src/common/mod.rs new file mode 100644 index 0000000..00da770 --- /dev/null +++ b/src/common/mod.rs @@ -0,0 +1,38 @@ +use std::fmt::Display; + +use clap::ValueEnum; +#[derive(Debug, Clone, Copy)] +pub(crate) 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) + } +} diff --git a/src/main.rs b/src/main.rs index 3de5b45..cd27c7b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,13 +8,11 @@ use libpt::log::*; use clap::Parser; use clap_verbosity_flag::{InfoLevel, Verbosity}; -/// Chose a mode for the application to run in, server needs more than client. -enum Mode { - #[cfg(feature = "server")] - Server, - #[cfg(feature = "client")] - Client, -} +mod client; +mod common; +mod server; + +use common::Mode; /// short about section displayed in help const ABOUT_ROOT: &'static str = r##" @@ -42,18 +40,25 @@ libpt: {version} Author: {author-with-newline} "# )] -pub struct Cli { +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 verbose: Verbosity, + pub(crate) verbose: Verbosity, /// show additional logging meta data #[arg(long)] - pub meta: bool, + 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 addr: std::net::SocketAddr, + pub(crate) addr: std::net::SocketAddr, } fn cli_parse() -> Cli { @@ -80,5 +85,12 @@ fn cli_parse() -> Cli { fn main() { let mut cli = cli_parse(); debug!("dumping cli args:\n{:#?}", cli); - println!("Hello, world!"); + #[cfg(feature = "server")] + if cli.server { + info!("starting server"); + + return; + } + // implicit else, so we can work without the server feature + info!("starting client"); } diff --git a/src/server/mod.rs b/src/server/mod.rs new file mode 100644 index 0000000..e69de29