ccxt_core/symbol/
error.rs1use std::fmt;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum SymbolError {
11 InvalidFormat(String),
13
14 MissingComponent(String),
16
17 InvalidCurrency(String),
19
20 InvalidExpiryDate {
22 year: u8,
24 month: u8,
26 day: u8,
28 },
29
30 UnexpectedDateSuffix,
32
33 MissingDateSuffix,
35
36 EmptySymbol,
38
39 MultipleColons,
41
42 InvalidDateFormat(String),
44}
45
46impl fmt::Display for SymbolError {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 match self {
49 Self::InvalidFormat(msg) => write!(f, "Invalid symbol format: {}", msg),
50 Self::MissingComponent(component) => {
51 write!(f, "Missing required component: {}", component)
52 }
53 Self::InvalidCurrency(code) => write!(f, "Invalid currency code: {}", code),
54 Self::InvalidExpiryDate { year, month, day } => {
55 write!(f, "Invalid expiry date: {:02}{:02}{:02}", year, month, day)
56 }
57 Self::UnexpectedDateSuffix => write!(f, "Unexpected date suffix in swap symbol"),
58 Self::MissingDateSuffix => write!(f, "Missing date suffix in futures symbol"),
59 Self::EmptySymbol => write!(f, "Symbol string is empty"),
60 Self::MultipleColons => write!(f, "Symbol contains multiple colons"),
61 Self::InvalidDateFormat(msg) => write!(f, "Invalid date format: {}", msg),
62 }
63 }
64}
65
66impl std::error::Error for SymbolError {}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn test_error_display() {
74 assert_eq!(
75 SymbolError::InvalidFormat("test".to_string()).to_string(),
76 "Invalid symbol format: test"
77 );
78 assert_eq!(
79 SymbolError::MissingComponent("base".to_string()).to_string(),
80 "Missing required component: base"
81 );
82 assert_eq!(
83 SymbolError::InvalidCurrency("123".to_string()).to_string(),
84 "Invalid currency code: 123"
85 );
86 assert_eq!(
87 SymbolError::InvalidExpiryDate {
88 year: 24,
89 month: 13,
90 day: 1
91 }
92 .to_string(),
93 "Invalid expiry date: 241301"
94 );
95 assert_eq!(
96 SymbolError::UnexpectedDateSuffix.to_string(),
97 "Unexpected date suffix in swap symbol"
98 );
99 assert_eq!(
100 SymbolError::MissingDateSuffix.to_string(),
101 "Missing date suffix in futures symbol"
102 );
103 assert_eq!(
104 SymbolError::EmptySymbol.to_string(),
105 "Symbol string is empty"
106 );
107 assert_eq!(
108 SymbolError::MultipleColons.to_string(),
109 "Symbol contains multiple colons"
110 );
111 }
112}