ccxt_core/symbol/
error.rs

1//! Symbol error types for parsing and validation
2//!
3//! This module provides error types for symbol-related operations including
4//! parsing, validation, and conversion errors.
5
6use std::fmt;
7
8/// Symbol-related errors
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum SymbolError {
11    /// Invalid symbol format
12    InvalidFormat(String),
13
14    /// Missing required component
15    MissingComponent(String),
16
17    /// Invalid currency code
18    InvalidCurrency(String),
19
20    /// Invalid expiry date
21    InvalidExpiryDate {
22        /// Year (2-digit)
23        year: u8,
24        /// Month (1-12)
25        month: u8,
26        /// Day (1-31)
27        day: u8,
28    },
29
30    /// Unexpected date suffix in swap symbol
31    UnexpectedDateSuffix,
32
33    /// Missing date suffix in futures symbol
34    MissingDateSuffix,
35
36    /// Empty symbol string
37    EmptySymbol,
38
39    /// Multiple colons in symbol
40    MultipleColons,
41
42    /// Invalid date format in symbol
43    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}