use crate::utils::Json;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum Dex<'a> {
#[default]
Main,
Hip3(&'a str),
}
impl<'a> Dex<'a> {
#[must_use]
pub fn of_coin(coin: &'a str) -> Dex<'a> {
match coin.split_once(':') {
Some((dex, _)) => Dex::Hip3(dex),
None => Dex::Main,
}
}
#[must_use]
pub const fn name(self) -> Option<&'a str> {
match self {
Dex::Main => None,
Dex::Hip3(name) => Some(name),
}
}
#[must_use]
pub fn field(self) -> Option<(&'static str, Json)> {
self.name().map(|name| ("dex", Json::String(name.to_owned())))
}
}
#[must_use]
pub fn split_coin(coin: &str) -> Option<(&str, &str)> {
let (dex, symbol) = coin.split_once(':')?;
(!dex.is_empty() && !symbol.is_empty()).then_some((dex, symbol))
}
#[must_use]
pub fn coin(dex: &str, symbol: &str) -> String {
format!("{dex}:{symbol}")
}
#[cfg(test)]
#[path = "tests/dex.rs"]
mod tests;