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