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

fix(cmd): add network validation in coffee cmd inputs #274

Merged
Merged
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
52 changes: 51 additions & 1 deletion coffee_cmd/src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Coffee command line arguments definition.
use clap::{Parser, Subcommand};
use coffee_lib::error;
use coffee_lib::errors::CoffeeError;
use std::fmt::Display;
Comment on lines +3 to +5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look at how we organize the import, we follow the following rules

std import

external dependencies import

coffee importa

crate import

But this can be addressed in a followup PR

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing that out. I'll address that asap!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've addressed this in #276.


/// Coffee main command line definition for the command line tools.
#[derive(Debug, Parser)]
Expand Down Expand Up @@ -133,6 +136,43 @@ impl From<&RemoteAction> for coffee_core::RemoteAction {
}
}

#[derive(Debug)]
enum ClnNetwork {
Mainnet,
Testnet,
Signet,
Regtest,
Liquid,
}

impl Display for ClnNetwork {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
ClnNetwork::Mainnet => "mainnet",
ClnNetwork::Testnet => "testnet",
ClnNetwork::Signet => "signet",
ClnNetwork::Regtest => "regtest",
ClnNetwork::Liquid => "liquid",
};
write!(f, "{s}")
}
}

impl TryFrom<String> for ClnNetwork {
type Error = String;

fn try_from(network: String) -> Result<Self, Self::Error> {
match network.as_str() {
"mainnet" => Ok(Self::Mainnet),
"testnet" => Ok(Self::Testnet),
"signet" => Ok(Self::Signet),
"regtest" => Ok(Self::Regtest),
"liquid" => Ok(Self::Liquid),
_ => Err(format!("{network} is not a valid network name")),
}
}
}

impl coffee_core::CoffeeArgs for CoffeeArgs {
fn command(&self) -> coffee_core::CoffeeOperation {
coffee_core::CoffeeOperation::from(&self.command)
Expand All @@ -147,7 +187,17 @@ impl coffee_core::CoffeeArgs for CoffeeArgs {
}

fn network(&self) -> Option<String> {
self.network.clone()
let network = self
.network
.clone()
.ok_or_else(|| error!("Network is not defined"))
.ok()?;
let validated_network = ClnNetwork::try_from(network.to_lowercase()).ok();

match validated_network {
Some(valid_network) => format!("{valid_network}").into(),
None => None,
}
}

fn skip_verify(&self) -> bool {
Expand Down
Loading