use std::fmt;
use serde::{Deserialize, Serialize};
use crate::Currency;
#[derive(Clone, Debug, Eq, Serialize, Deserialize)]
pub struct Coin {
symbol: Box<str>,
name: Box<str>,
currency: Currency,
}
impl Coin {
#[must_use]
pub fn new(symbol: impl Into<String>, name: impl Into<String>, currency: Currency) -> Self {
Self {
symbol: symbol.into().to_uppercase().into_boxed_str(),
name: name.into().into_boxed_str(),
currency,
}
}
#[must_use]
#[inline]
pub const fn symbol(&self) -> &str {
&self.symbol
}
#[must_use]
#[inline]
pub const fn name(&self) -> &str {
&self.name
}
#[must_use]
#[inline]
pub const fn currency(&self) -> Currency {
self.currency
}
#[must_use]
#[inline]
pub const fn table_prefix() -> &'static str {
"candles"
}
#[must_use]
pub fn table_name(&self) -> String {
format!(
"{}_{}_{}",
Self::table_prefix(),
self.symbol.to_lowercase(),
self.currency.to_string().to_lowercase()
)
}
}
impl fmt::Display for Coin {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if f.alternate() {
write!(f, "{} ({})", self.name, self.symbol)
} else {
write!(f, "{}", self.symbol)
}
}
}
impl PartialEq for Coin {
fn eq(&self, other: &Self) -> bool {
self.symbol == other.symbol
}
}