use core::fmt;
use core::fmt::Display;
use std::str::FromStr;
use anyhow::anyhow;
use walletd_hd_key::slip44::Symbol;
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum CryptoCoin {
BTC,
ETH,
}
impl CryptoCoin {
pub fn main_unit(&self) -> String {
match self {
Self::BTC => "BTC".to_string(),
Self::ETH => "ETH".to_string(),
}
}
pub fn fundamental_unit(&self) -> String {
match self {
Self::BTC => "satoshi".to_string(),
Self::ETH => "wei".to_string(),
}
}
}
impl FromStr for CryptoCoin {
type Err = anyhow::Error;
fn from_str(coin_name: &str) -> Result<Self, Self::Err> {
match coin_name.to_string().to_lowercase().as_str() {
"btc" | "bitcoin" => Ok(Self::BTC),
"eth" | "ethereum" | "ether" => Ok(Self::ETH),
_ => Err(anyhow!("Current valid options are BTC or ETH")),
}
}
}
impl Display for CryptoCoin {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::BTC => write!(f, "Bitcoin (BTC)")?,
Self::ETH => write!(f, "Ethereum (ETH)")?,
}
Ok(())
}
}
impl TryFrom<Symbol> for CryptoCoin {
type Error = anyhow::Error;
fn try_from(value: Symbol) -> Result<Self, Self::Error> {
match value {
Symbol::BTC => Ok(CryptoCoin::BTC),
Symbol::ETH => Ok(CryptoCoin::ETH),
_ => Err(anyhow!("Unsupported coin type")),
}
}
}