From 59c2f0cfddbfdc1d60fdb88da2e3ff0e2bec790e Mon Sep 17 00:00:00 2001 From: Seamus Mulholland-Patterson Date: Fri, 11 Nov 2022 13:06:28 +0800 Subject: [PATCH] added cli for specifying communication mode --- Cargo.lock | 1 + ruffd/Cargo.toml | 3 +- ruffd/src/main.rs | 91 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 90 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5e2022f..072ba90 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2049,6 +2049,7 @@ dependencies = [ name = "ruffd" version = "0.0.1" dependencies = [ + "clap", "ruffd-core", "ruffd-types", ] diff --git a/ruffd/Cargo.toml b/ruffd/Cargo.toml index 4f12660..f5ab801 100644 --- a/ruffd/Cargo.toml +++ b/ruffd/Cargo.toml @@ -3,8 +3,7 @@ name = "ruffd" version = "0.0.1" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] ruffd-core = { path="../ruffd-core" } ruffd-types = { path="../ruffd-types" } +clap = "4.0" diff --git a/ruffd/src/main.rs b/ruffd/src/main.rs index 34c87e9..79fbb93 100644 --- a/ruffd/src/main.rs +++ b/ruffd/src/main.rs @@ -1,8 +1,93 @@ -use ruffd_core::server::StdioServer; +use clap::Parser; +use ruffd_core::server::{StdioServer, TcpServer}; use ruffd_types::tokio; -#[tokio::main] -async fn main() { +#[derive(Parser, Debug)] +struct PipeArg { + #[arg(required_unless_present("named_pipe"))] + pos_pipe: Option, + #[arg(long("pipe"), required_unless_present("pos_pipe"))] + named_pipe: Option, +} + +// Below is opinionated (not based on the specification) +// prioritise named argument if present +impl Into for PipeArg { + fn into(self) -> String { + match (self.pos_pipe, self.named_pipe) { + (None, Some(pipe)) => pipe, + (Some(pipe), None) => pipe, + (Some(_), Some(named)) => named, + _ => unreachable!(), + } + } +} + +#[derive(Parser, Debug)] +struct PortArg { + #[arg(required_unless_present("named_port"))] + pos_port: Option, + #[arg(long("port"), required_unless_present("pos_port"))] + named_port: Option, +} + +// Below is opinionated (not based on the specification) +// prioritise named argument if present +impl Into for PortArg { + fn into(self) -> u64 { + match (self.pos_port, self.named_port) { + (None, Some(port)) => port, + (Some(port), None) => port, + (Some(_), Some(named)) => named, + _ => unreachable!(), + } + } +} + +#[derive(clap::Subcommand, Debug)] +enum CommMode { + Stdio, + Socket { + /// Port number to connect to client + #[command(flatten)] + port: PortArg, + }, + Pipe { + /// Pipe name or socket filename + #[command(flatten)] + pipe: PipeArg, + }, +} + +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +struct Cli { + #[command(subcommand)] + comm_mode: Option, +} + +async fn run_stdio_server() { let mut server = StdioServer::default(); server.get_service_mut().run().await; } + +async fn run_tcp_server(port: u64) { + let mut server = TcpServer::connect(format!("127.0.0.1:{}", port)) + .await + .unwrap(); + server.get_service_mut().run().await; +} + +#[tokio::main] +async fn main() { + let cli = Cli::parse(); + if let Some(comm_mode) = cli.comm_mode { + match comm_mode { + CommMode::Stdio => run_stdio_server().await, + CommMode::Socket { port } => run_tcp_server(port.into()).await, + _ => unimplemented!(), + } + } else { + run_stdio_server().await; + } +}