use clap::{arg, Parser};
use serde::{Deserialize, Serialize};
use crate::commands::HEADING_RPC;
use super::locator;
pub mod add;
pub mod ls;
pub mod rm;
#[derive(Debug, Parser)]
pub enum Cmd {
Add(add::Cmd),
Rm(rm::Cmd),
Ls(ls::Cmd),
}
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Add(#[from] add::Error),
#[error(transparent)]
Rm(#[from] rm::Error),
#[error(transparent)]
Ls(#[from] ls::Error),
#[error(transparent)]
Config(#[from] locator::Error),
#[error("network arg or rpc url and network passphrase are required if using the network")]
Network,
}
impl Cmd {
pub fn run(&self) -> Result<(), Error> {
match self {
Cmd::Add(cmd) => cmd.run()?,
Cmd::Rm(new) => new.run()?,
Cmd::Ls(cmd) => cmd.run()?,
};
Ok(())
}
}
#[derive(Debug, clap::Args, Clone, Default)]
#[group(skip)]
pub struct Args {
#[arg(
long = "rpc-url",
requires = "network_passphrase",
env = "SOROBAN_RPC_URL",
help_heading = HEADING_RPC,
)]
pub rpc_url: Option<String>,
#[arg(
long = "network-passphrase",
requires = "rpc_url",
env = "SOROBAN_NETWORK_PASSPHRASE",
help_heading = HEADING_RPC,
)]
pub network_passphrase: Option<String>,
#[arg(
long,
conflicts_with = "network_passphrase",
conflicts_with = "rpc_url",
env = "SOROBAN_NETWORK"
)]
pub network: Option<String>,
}
impl Args {
pub fn get(&self, locator: &locator::Args) -> Result<Network, Error> {
if let Some(name) = self.network.as_deref() {
Ok(locator.read_network(name)?)
} else if let (Some(rpc_url), Some(network_passphrase)) =
(self.rpc_url.clone(), self.network_passphrase.clone())
{
Ok(Network {
rpc_url,
network_passphrase,
})
} else {
Err(Error::Network)
}
}
pub fn is_no_network(&self) -> bool {
self.network.is_none() && self.network_passphrase.is_none() && self.rpc_url.is_none()
}
}
#[derive(Debug, clap::Args, Serialize, Deserialize, Clone)]
#[group(skip)]
pub struct Network {
#[arg(
long = "rpc-url",
env = "SOROBAN_RPC_URL",
help_heading = HEADING_RPC,
)]
pub rpc_url: String,
#[arg(
long,
env = "SOROBAN_NETWORK_PASSPHRASE",
help_heading = HEADING_RPC,
)]
pub network_passphrase: String,
}