From 08f7b32fb397c86e69ac146aeef62d8862208336 Mon Sep 17 00:00:00 2001 From: f321x Date: Wed, 29 May 2024 16:48:36 +0200 Subject: [PATCH] parsing --- taptrade-cli-demo/src/cli/mod.rs | 65 ++++++++++++++++++++++++++------ taptrade-cli-demo/src/main.rs | 5 +-- 2 files changed, 55 insertions(+), 15 deletions(-) diff --git a/taptrade-cli-demo/src/cli/mod.rs b/taptrade-cli-demo/src/cli/mod.rs index eca9c8d..6f6c845 100644 --- a/taptrade-cli-demo/src/cli/mod.rs +++ b/taptrade-cli-demo/src/cli/mod.rs @@ -1,27 +1,61 @@ use clap::{command, Arg, Command, ArgMatches}; -use crate::coordinator; +#[derive(Debug)] +pub struct Coordinator { -pub struct cli_settings { - pub coordinator: bool, - pub maker: bool, - pub taker: bool, - pub c_endpoint: String, - pub electrum_ep: String, +} + +#[derive(Debug)] +pub struct TraderSettings { + pub electrum_endpoint: String, + pub coordinator_endpoint: String, +} + +#[derive(Debug)] +pub enum CliSettings { + Coordinator(Coordinator), + Taker(TraderSettings), + Maker(TraderSettings) } trait ArgMatchesParser { - + fn parse_into_enum(&self) -> CliSettings; } -pub fn parse_cli_args() -> ArgMatches { +impl ArgMatchesParser for ArgMatches { + fn parse_into_enum(&self) -> CliSettings { + if let Some(_mode) = self.subcommand_matches("coordinator") { + CliSettings::Coordinator(Coordinator { }) + } else if let Some(_mode) = self.subcommand_matches("trader") { + let trader_settings = TraderSettings { + coordinator_endpoint: self.get_one::("coordinator-ep") + .expect("Coordinator endpoint not provided!").clone(), + electrum_endpoint: self.get_one::("electrum-ep") + .expect("Electrum endpoint not provided").clone() + }; + if self.contains_id("maker") { + CliSettings::Maker( trader_settings ) + } else if self.contains_id("taker") { + CliSettings::Taker( trader_settings ) + } else { + panic!("Wrong arguments for Trader mode!") + } + } else { + panic!("Select either coordinator or trader mode!") + } + } +} + +pub fn parse_cli_args() -> CliSettings { command!() .about("RoboSats taproot onchain trade pipeline CLI demonstrator. Don't use with real funds.") .subcommand( Command::new("coordinator") + .about("Run in coordinator mode.") ) .subcommand( - Command::new("trader") + Command::new("trader") + .about("Two available trader modes: Maker and Taker. Select one and provide Coordinator and Electum endpoint") .arg( Arg::new("taker") .short('t') @@ -39,12 +73,21 @@ pub fn parse_cli_args() -> ArgMatches { .conflicts_with("taker") ) .arg( - Arg::new("endpoint") + Arg::new("coordinator-ep") .short('p') .long("endpoint") .required(true) .help("Communication endpoint of the coordinator to connect to") ) + .arg( + Arg::new("electrum-ep") + .short('e') + .long("electrum") + .required(true) + .help("URL of the electrum endpoint") + ) ) + .arg_required_else_help(true) .get_matches() + .parse_into_enum() } diff --git a/taptrade-cli-demo/src/main.rs b/taptrade-cli-demo/src/main.rs index 230f7ee..98a6af8 100644 --- a/taptrade-cli-demo/src/main.rs +++ b/taptrade-cli-demo/src/main.rs @@ -6,8 +6,5 @@ use cli::parse_cli_args; fn main() { let cli_args = parse_cli_args(); - + dbg!(cli_args); } - -// use clap to parse mode (taker, maker or coordinator), communication endpoint (URL or PID or something else), electrum server -// https://www.youtube.com/watch?v=Ot3qCA3Iv_8