Skip to main content

core_invoice/
error.rs

1//! Construction errors for amounts, dates, and attachments.
2
3use std::fmt;
4
5/// Error constructing an [`crate::InvoiceAmount`].
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum AmountError {
8    /// More than two fraction digits. The type never rounds.
9    TooManyDecimals,
10    /// Decimal overflow.
11    Overflow,
12}
13
14impl fmt::Display for AmountError {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        match self {
17            Self::TooManyDecimals => write!(f, "amount has more than two fraction digits"),
18            Self::Overflow => write!(f, "amount overflow"),
19        }
20    }
21}
22
23impl std::error::Error for AmountError {}
24
25/// Error constructing a [`crate::Date`].
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub enum DateError {
28    /// Not a calendar day `YYYY-MM-DD` (no time, no zone).
29    Invalid,
30}
31
32impl fmt::Display for DateError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        write!(f, "not an EN 16931 calendar date (YYYY-MM-DD, no time)")
35    }
36}
37
38impl std::error::Error for DateError {}
39
40/// Error constructing an [`crate::Attachment`].
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub enum AttachmentError {
43    /// MIME code is empty or whitespace.
44    EmptyMime,
45    /// Filename is empty or whitespace.
46    EmptyFilename,
47}
48
49impl fmt::Display for AttachmentError {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        match self {
52            Self::EmptyMime => write!(f, "attachment mime code shall be present"),
53            Self::EmptyFilename => write!(f, "attachment filename shall be present"),
54        }
55    }
56}
57
58impl std::error::Error for AttachmentError {}