rusty_model/
venues.rs

1use serde::{Deserialize, Serialize};
2
3/// Represents a trading venue or exchange.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[repr(u8)]
6pub enum Venue {
7    /// Upbit exchange.
8    Upbit = 1,
9    /// Binance exchange.
10    Binance = 2,
11    /// Coinbase exchange.
12    Coinbase = 3,
13    /// Bybit exchange.
14    Bybit = 4,
15    /// Bithumb exchange.
16    Bithumb = 5,
17    /// A venue for testing purposes.
18    Test = 99, // For testing purposes
19}
20
21impl Serialize for Venue {
22    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
23    where
24        S: serde::Serializer,
25    {
26        serializer.serialize_u8(*self as u8)
27    }
28}
29
30impl<'de> Deserialize<'de> for Venue {
31    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
32    where
33        D: serde::Deserializer<'de>,
34    {
35        match u8::deserialize(deserializer)? {
36            1 => Ok(Self::Upbit),
37            2 => Ok(Self::Binance),
38            3 => Ok(Self::Coinbase),
39            4 => Ok(Self::Bybit),
40            5 => Ok(Self::Bithumb),
41            99 => Ok(Self::Test),
42            _ => Err(serde::de::Error::custom("invalid value")),
43        }
44    }
45}
46
47impl Venue {
48    /// Convert venue to String representation
49    #[must_use]
50    pub const fn to_str(&self) -> &'static str {
51        match self {
52            Self::Upbit => "Upbit",
53            Self::Binance => "Binance",
54            Self::Coinbase => "Coinbase",
55            Self::Bybit => "Bybit",
56            Self::Bithumb => "Bithumb",
57            Self::Test => "Test",
58        }
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65    use rusty_common::json;
66
67    #[test]
68    fn test_venue_to_str() {
69        assert_eq!(Venue::Upbit.to_str(), "Upbit");
70        assert_eq!(Venue::Binance.to_str(), "Binance");
71        assert_eq!(Venue::Coinbase.to_str(), "Coinbase");
72        assert_eq!(Venue::Bybit.to_str(), "Bybit");
73        assert_eq!(Venue::Bithumb.to_str(), "Bithumb");
74        assert_eq!(Venue::Test.to_str(), "Test");
75    }
76
77    #[test]
78    fn test_venue_to_str_const_context() {
79        // Verify that to_str() can be used in const contexts
80        const BINANCE_STR: &str = Venue::Binance.to_str();
81        const COINBASE_STR: &str = Venue::Coinbase.to_str();
82        const BYBIT_STR: &str = Venue::Bybit.to_str();
83        const UPBIT_STR: &str = Venue::Upbit.to_str();
84        const BITHUMB_STR: &str = Venue::Bithumb.to_str();
85        const TEST_STR: &str = Venue::Test.to_str();
86
87        assert_eq!(BINANCE_STR, "Binance");
88        assert_eq!(COINBASE_STR, "Coinbase");
89        assert_eq!(BYBIT_STR, "Bybit");
90        assert_eq!(UPBIT_STR, "Upbit");
91        assert_eq!(BITHUMB_STR, "Bithumb");
92        assert_eq!(TEST_STR, "Test");
93    }
94
95    #[test]
96    fn test_venue_serialization() {
97        let venue = Venue::Binance;
98        let serialized = json::to_smartstring(&venue).unwrap();
99        assert_eq!(serialized, "2"); // Binance has value 2
100
101        let deserialized: Venue = json::parse(&serialized).unwrap();
102        assert_eq!(deserialized, Venue::Binance);
103    }
104
105    #[test]
106    fn test_venue_deserialization() {
107        let upbit: Venue = json::parse("1").unwrap();
108        let binance: Venue = json::parse("2").unwrap();
109        let coinbase: Venue = json::parse("3").unwrap();
110        let bybit: Venue = json::parse("4").unwrap();
111        let bithumb: Venue = json::parse("5").unwrap();
112        let test: Venue = json::parse("99").unwrap();
113
114        assert_eq!(upbit, Venue::Upbit);
115        assert_eq!(binance, Venue::Binance);
116        assert_eq!(coinbase, Venue::Coinbase);
117        assert_eq!(bybit, Venue::Bybit);
118        assert_eq!(bithumb, Venue::Bithumb);
119        assert_eq!(test, Venue::Test);
120    }
121}