Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added cli for specifying communication mode #25

Merged
merged 2 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 From<PipeArg> for String {
fn from(arg: PipeArg) -> Self {
match (arg.pos_pipe, arg.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 From<PortArg> for u64 {
fn from(arg: PortArg) -> Self {
match (arg.pos_port, arg.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;
}
}