Skip to content

Commit

Permalink
added cli for specifying communication mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Seamooo committed Nov 11, 2022
1 parent 44c19a8 commit 59c2f0c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions ruffd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
91 changes: 88 additions & 3 deletions ruffd/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
#[arg(long("pipe"), required_unless_present("pos_pipe"))]
named_pipe: Option<String>,
}

// Below is opinionated (not based on the specification)
// prioritise named argument if present
impl Into<String> 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<u64>,
#[arg(long("port"), required_unless_present("pos_port"))]
named_port: Option<u64>,
}

// Below is opinionated (not based on the specification)
// prioritise named argument if present
impl Into<u64> 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<CommMode>,
}

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;
}
}

0 comments on commit 59c2f0c

Please sign in to comment.