1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::fmt::Display;

use crate::{BadiMonth, LAST_YEAR_SUPPORTED};

#[derive(Debug)]
pub enum BadiDateError {
    DayInvalid(u8, BadiMonth),
    MonthInvalid,
    YearInvalid,
    DateNotSupported,
}

impl BadiDateError {
    pub fn message(&self) -> String {
        match self {
            BadiDateError::DayInvalid(max, month) => {
                format!(
                    "Day must be in the range [1-{}] for {}",
                    max,
                    month.description()
                )
            }
            BadiDateError::MonthInvalid => {
                "Month must be BadiMonth::AyyamIHa or BadiMonth::Month(month) in the range [1-19]"
                    .to_string()
            }
            BadiDateError::YearInvalid => {
                format!("Year must be in the range [1-{}]", LAST_YEAR_SUPPORTED)
            }
            BadiDateError::DateNotSupported => "Gregorian date is not supported".to_string(),
        }
    }
}

impl Display for BadiDateError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "BadiDateError: {}", self.message())
    }
}