diff --git a/coffee_cmd/src/cmd.rs b/coffee_cmd/src/cmd.rs index 702b90c..013538d 100644 --- a/coffee_cmd/src/cmd.rs +++ b/coffee_cmd/src/cmd.rs @@ -1,5 +1,6 @@ //! Coffee command line arguments definition. use clap::{Parser, Subcommand}; +use coffee_lib::{error, errors::CoffeeError}; /// Coffee main command line definition for the command line tools. #[derive(Debug, Parser)] @@ -133,6 +134,36 @@ impl From<&RemoteAction> for coffee_core::RemoteAction { } } +#[derive(Debug)] +enum ClnNetwork { + Mainnet, + Testnet, + Signet, + Regtest, +} + +impl ClnNetwork { + fn from_str(network: &str) -> Option { + + match network { + "mainnet" => Some(Self::Mainnet), + "testnet" => Some(Self::Testnet), + "signet" => Some(Self::Signet), + "regtest" => Some(Self::Regtest), + _ => None, + } + } + + fn to_str(network: Self) -> Option { + match network { + Self::Mainnet => Some("mainnet".to_string()), + Self::Testnet => Some("testnet".to_string()), + Self::Signet => Some("signet".to_string()), + Self::Regtest => Some("regtest".to_string()), + } + } +} + impl coffee_core::CoffeeArgs for CoffeeArgs { fn command(&self) -> coffee_core::CoffeeOperation { coffee_core::CoffeeOperation::from(&self.command) @@ -147,7 +178,10 @@ impl coffee_core::CoffeeArgs for CoffeeArgs { } fn network(&self) -> Option { - self.network.clone() + let network = self.network.clone().ok_or_else(|| error!("Network is not set")).ok()?; + let validated_network = ClnNetwork::from_str(&network.to_lowercase()).ok_or_else(|| error!("Invalid network name")).ok()?; + + ClnNetwork::to_str(validated_network) } fn skip_verify(&self) -> bool {