Skip to content

Commit e496ae8

Browse files
author
Ifeanyichukwu
committed
fix(cmd): add network validation in coffee cmd inputs
1 parent db419c6 commit e496ae8

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

coffee_cmd/src/cmd.rs

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Coffee command line arguments definition.
22
use clap::{Parser, Subcommand};
3+
use coffee_lib::{error, errors::CoffeeError};
34

45
/// Coffee main command line definition for the command line tools.
56
#[derive(Debug, Parser)]
@@ -133,6 +134,36 @@ impl From<&RemoteAction> for coffee_core::RemoteAction {
133134
}
134135
}
135136

137+
#[derive(Debug)]
138+
enum ClnNetwork {
139+
Mainnet,
140+
Testnet,
141+
Signet,
142+
Regtest,
143+
}
144+
145+
impl ClnNetwork {
146+
fn from_str(network: &str) -> Option<Self> {
147+
148+
match network {
149+
"mainnet" => Some(Self::Mainnet),
150+
"testnet" => Some(Self::Testnet),
151+
"signet" => Some(Self::Signet),
152+
"regtest" => Some(Self::Regtest),
153+
_ => None,
154+
}
155+
}
156+
157+
fn to_str(network: Self) -> Option<String> {
158+
match network {
159+
Self::Mainnet => Some("mainnet".to_string()),
160+
Self::Testnet => Some("testnet".to_string()),
161+
Self::Signet => Some("signet".to_string()),
162+
Self::Regtest => Some("regtest".to_string()),
163+
}
164+
}
165+
}
166+
136167
impl coffee_core::CoffeeArgs for CoffeeArgs {
137168
fn command(&self) -> coffee_core::CoffeeOperation {
138169
coffee_core::CoffeeOperation::from(&self.command)
@@ -147,7 +178,10 @@ impl coffee_core::CoffeeArgs for CoffeeArgs {
147178
}
148179

149180
fn network(&self) -> Option<String> {
150-
self.network.clone()
181+
let network = self.network.clone().ok_or_else(|| error!("Network is not set")).ok()?;
182+
let validated_network = ClnNetwork::from_str(&network.to_lowercase()).ok_or_else(|| error!("Invalid network name")).ok()?;
183+
184+
ClnNetwork::to_str(validated_network)
151185
}
152186

153187
fn skip_verify(&self) -> bool {

0 commit comments

Comments
 (0)