use clap::Parser;
use color_eyre::Report;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
mod address;
mod query;
mod stake_address;
mod transaction;
pub use address::Address;
pub use query::Query;
pub use stake_address::StakeAddress;
pub use transaction::Transaction;
#[derive(Parser, Debug)]
pub enum Command {
#[clap(subcommand)]
Query(Query),
#[clap(subcommand)]
Address(Address),
#[clap(subcommand)]
StakeAddress(StakeAddress),
#[clap(subcommand)]
Transaction(Transaction),
}
impl Command {
pub fn exec(self) -> Result<(), Report> {
match self {
Self::Query(query) => query.exec().map_err(Into::into),
Self::Address(address) => address.exec().map_err(Into::into),
Self::StakeAddress(stake_address) => stake_address.exec().map_err(Into::into),
Self::Transaction(transaction) => transaction.exec().map_err(Into::into),
}
}
}
pub fn write_to_file_or_println(
maybe_file: Option<PathBuf>,
content: &str,
) -> Result<(), std::io::Error> {
if let Some(out_file) = maybe_file {
let mut file = File::create(out_file)?;
file.write_all(content.as_bytes())?;
} else {
println!("{content}");
}
Ok(())
}