pricelevel/errors/
types.rs

1use std::fmt::{Debug, Display, Formatter, Result};
2
3/// Represents errors that can occur when processing price levels in trading operations.
4///
5/// This enum encapsulates various error conditions that might arise during order book
6/// management, price validation, and other trading-related operations.
7///
8/// # Examples
9///
10/// ```
11/// use pricelevel::PriceLevelError;
12///
13/// // Creating a parse error
14/// let error = PriceLevelError::ParseError {
15///     message: "Failed to parse price: invalid decimal format".to_string()
16/// };
17///
18/// // Creating a missing field error
19/// let missing_field_error = PriceLevelError::MissingField("price".to_string());
20/// ```
21pub enum PriceLevelError {
22    /// Error that occurs when parsing fails with a specific message.
23    ///
24    /// This variant is used when string conversion or data parsing operations fail.
25    ParseError {
26        /// Descriptive message explaining the parsing failure
27        message: String,
28    },
29
30    /// Error indicating that the input is in an invalid format.
31    ///
32    /// This is a general error for when the input data doesn't conform to expected patterns
33    /// but doesn't fit into more specific error categories.
34    InvalidFormat,
35
36    /// Error indicating an unrecognized order type was provided.
37    ///
38    /// Used when the system encounters an order type string that isn't in the supported set.
39    /// The string parameter contains the unrecognized order type.
40    UnknownOrderType(String),
41
42    /// Error indicating a required field is missing.
43    ///
44    /// Used when a mandatory field is absent in the input data.
45    /// The string parameter specifies which field is missing.
46    MissingField(String),
47
48    /// Error indicating a field has an invalid value.
49    ///
50    /// This error occurs when a field's value is present but doesn't meet validation criteria.
51    InvalidFieldValue {
52        /// The name of the field with the invalid value
53        field: String,
54        /// The invalid value as a string representation
55        value: String,
56    },
57
58    /// Error indicating an operation cannot be performed for the specified reason.
59    ///
60    /// Used when an action is prevented due to business rules or system constraints.
61    InvalidOperation {
62        /// Explanation of why the operation is invalid
63        message: String,
64    },
65}
66impl Display for PriceLevelError {
67    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
68        match self {
69            PriceLevelError::ParseError { message } => write!(f, "{message}"),
70            PriceLevelError::InvalidFormat => write!(f, "Invalid format"),
71            PriceLevelError::UnknownOrderType(order_type) => {
72                write!(f, "Unknown order type: {order_type}")
73            }
74            PriceLevelError::MissingField(field) => write!(f, "Missing field: {field}"),
75            PriceLevelError::InvalidFieldValue { field, value } => {
76                write!(f, "Invalid value for field {field}: {value}")
77            }
78            PriceLevelError::InvalidOperation { message } => {
79                write!(f, "Invalid operation: {message}")
80            }
81        }
82    }
83}
84
85impl Debug for PriceLevelError {
86    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
87        match self {
88            PriceLevelError::ParseError { message } => write!(f, "{message}"),
89            PriceLevelError::InvalidFormat => write!(f, "Invalid format"),
90            PriceLevelError::UnknownOrderType(order_type) => {
91                write!(f, "Unknown order type: {order_type}")
92            }
93            PriceLevelError::MissingField(field) => write!(f, "Missing field: {field}"),
94            PriceLevelError::InvalidFieldValue { field, value } => {
95                write!(f, "Invalid value for field {field}: {value}")
96            }
97            PriceLevelError::InvalidOperation { message } => {
98                write!(f, "Invalid operation: {message}")
99            }
100        }
101    }
102}
103
104impl std::error::Error for PriceLevelError {}