Skip to main content

expiration_date/
error.rs

1//! # Error Module
2//!
3//! Provides error types for expiration date operations including parsing,
4//! conversion, and date calculation failures.
5
6use thiserror::Error;
7
8/// Error types for expiration date operations.
9#[derive(Debug, Error)]
10pub enum ExpirationDateError {
11    /// Failed to parse a string into an ExpirationDate.
12    #[error("parse error: {0}")]
13    ParseError(String),
14
15    /// Failure during numeric or date conversion.
16    #[error("conversion error from {from_type} to {to_type}: {reason}")]
17    ConversionError {
18        /// The source type of the conversion.
19        from_type: String,
20        /// The target type of the conversion.
21        to_type: String,
22        /// The detailed reason for the failure.
23        reason: String,
24    },
25
26    /// Provided datetime is invalid for the context.
27    #[error("invalid datetime: {0}")]
28    InvalidDateTime(String),
29
30    /// Error from the underlying Positive type.
31    #[error("positive error: {0}")]
32    PositiveError(#[from] positive::error::PositiveError),
33
34    /// Error parsing dates using the chrono crate.
35    #[error("chrono parse error: {0}")]
36    ChronoParseError(#[from] chrono::ParseError),
37
38    /// Error parsing integers from strings.
39    #[error("parse int error: {0}")]
40    ParseIntError(#[from] std::num::ParseIntError),
41
42    /// Numeric overflow during financial convention calculations.
43    #[error("arithmetic overflow: {0}")]
44    ArithmeticOverflow(String),
45}
46
47impl From<String> for ExpirationDateError {
48    fn from(s: String) -> Self {
49        Self::ParseError(s)
50    }
51}
52
53impl From<&str> for ExpirationDateError {
54    fn from(s: &str) -> Self {
55        Self::ParseError(s.to_string())
56    }
57}