generated from PlexSheep/rs-base
split more stuff into modules
cargo devel CI / cargo CI (push) Successful in 2m7s
Details
cargo devel CI / cargo CI (push) Successful in 2m7s
Details
This commit is contained in:
parent
69615cf4d5
commit
a212cfff83
|
@ -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<InfoLevel>,
|
||||||
|
|
||||||
|
/// 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<clap::builder::PossibleValue> {
|
||||||
|
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<Self, String> {
|
||||||
|
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 }
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,38 +1,6 @@
|
||||||
use std::fmt::Display;
|
|
||||||
|
|
||||||
use clap::ValueEnum;
|
pub mod args;
|
||||||
#[derive(Debug, Clone, Copy)]
|
use args::*;
|
||||||
pub(crate) enum Mode {
|
|
||||||
Tcp,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ValueEnum for Mode {
|
pub mod conf;
|
||||||
fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
|
use conf::*;
|
||||||
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<Self, String> {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
75
src/main.rs
75
src/main.rs
|
@ -5,85 +5,14 @@
|
||||||
|
|
||||||
use libpt::log::*;
|
use libpt::log::*;
|
||||||
|
|
||||||
use clap::Parser;
|
|
||||||
use clap_verbosity_flag::{InfoLevel, Verbosity};
|
|
||||||
|
|
||||||
mod client;
|
mod client;
|
||||||
mod common;
|
mod common;
|
||||||
mod server;
|
mod server;
|
||||||
|
|
||||||
use common::Mode;
|
use common::{args::Cli, conf::*};
|
||||||
|
|
||||||
/// 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<InfoLevel>,
|
|
||||||
|
|
||||||
/// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let cli = cli_parse();
|
let cli = Cli::cli_parse();
|
||||||
debug!("dumping cli args:\n{:#?}", cli);
|
debug!("dumping cli args:\n{:#?}", cli);
|
||||||
#[cfg(feature = "server")]
|
#[cfg(feature = "server")]
|
||||||
if cli.server {
|
if cli.server {
|
||||||
|
|
Loading…
Reference in New Issue