use serde::{Deserialize, Serialize};
use std::{fmt::Display, path::PathBuf, str::FromStr};
use tsify::Tsify;
use wasm_bindgen::prelude::*;
#[derive(Serialize, Deserialize, Debug, Tsify)]
#[tsify(from_wasm_abi)]
pub struct Opt {
pub config: Option<PathBuf>,
pub verbose: i32,
pub pretty: bool,
pub options: ShellTomlConfig,
}
#[derive(Default, Debug, Serialize, Deserialize, Tsify)]
#[serde(rename_all = "snake_case")]
#[tsify(from_wasm_abi)]
pub enum SupportedChains {
#[default]
LocalTestnet,
LocalMainnet,
Testnet,
Mainnet,
}
#[derive(Debug, Serialize, Deserialize, Tsify)]
#[tsify(from_wasm_abi)]
pub struct ShellTomlConfig {
pub bind_ip: IpAddr,
pub bind_port: u16,
pub url: url::Url,
pub bootnodes: Vec<Multiaddr>,
pub node_key: Option<String>,
pub base_path: PathBuf,
pub keystore_password: Option<String>,
pub chain: SupportedChains,
pub verbose: i32,
pub pretty: bool,
}
impl FromStr for SupportedChains {
type Err = String;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"local_testnet" => Ok(SupportedChains::LocalTestnet),
"local_mainnet" => Ok(SupportedChains::LocalMainnet),
"testnet" => Ok(SupportedChains::Testnet),
"mainnet" => Ok(SupportedChains::Mainnet),
_ => Err(format!("Invalid chain: {}", s)),
}
}
}
impl Display for SupportedChains {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SupportedChains::LocalTestnet => write!(f, "local_testnet"),
SupportedChains::LocalMainnet => write!(f, "local_mainnet"),
SupportedChains::Testnet => write!(f, "testnet"),
SupportedChains::Mainnet => write!(f, "mainnet"),
}
}
}