df_hyperliquid_core/markets/
dex.rs1use crate::utils::Json;
6
7#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
9pub enum Dex<'a> {
10 #[default]
12 Main,
13 Hip3(&'a str),
15}
16
17impl<'a> Dex<'a> {
18 #[must_use]
20 pub fn of_coin(coin: &'a str) -> Dex<'a> {
21 match coin.split_once(':') {
22 Some((dex, _)) => Dex::Hip3(dex),
23 None => Dex::Main,
24 }
25 }
26
27 #[must_use]
29 pub const fn name(self) -> Option<&'a str> {
30 match self {
31 Dex::Main => None,
32 Dex::Hip3(name) => Some(name),
33 }
34 }
35
36 #[must_use]
38 pub fn field(self) -> Option<(&'static str, Json)> {
39 self.name().map(|name| ("dex", Json::String(name.to_owned())))
40 }
41}
42
43#[must_use]
45pub fn split_coin(coin: &str) -> Option<(&str, &str)> {
46 let (dex, symbol) = coin.split_once(':')?;
47 (!dex.is_empty() && !symbol.is_empty()).then_some((dex, symbol))
48}
49
50#[must_use]
52pub fn coin(dex: &str, symbol: &str) -> String {
53 format!("{dex}:{symbol}")
54}
55
56#[cfg(test)]
57#[path = "tests/dex.rs"]
58mod tests;