We have a server at least #3

Merged
PlexSheep merged 14 commits from devel into master 2024-01-19 16:40:16 +01:00
4 changed files with 62 additions and 12 deletions
Showing only changes of commit 085223d0a6 - Show all commits

0
src/client/mod.rs Normal file
View File

38
src/common/mod.rs Normal file
View File

@ -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<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)
}
}

View File

@ -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<InfoLevel>,
pub(crate) verbose: Verbosity<InfoLevel>,
/// 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");
}

0
src/server/mod.rs Normal file
View File