badi_date/
error.rs

1use std::fmt::Display;
2
3use crate::{BadiMonth, LAST_YEAR_SUPPORTED};
4
5/// Error returned from trying to construct a [`BadiDateLike`][`crate::BadiDateLike`] with invalid parameters
6#[derive(Debug)]
7pub enum BadiDateError {
8    /// The day number passed in for a given [`BadiMonth`] is invalid for that month
9    DayInvalid(BadiMonth, u16, u16),
10    /// The [`BadiMonth`] itself is invalid (due to an invalid day number)
11    MonthInvalid(BadiMonth),
12    /// The date passed in is not in the supported range
13    DateNotSupported,
14}
15
16impl BadiDateError {
17    /// Message associated with the [`BadiDateError`]
18    pub fn message(&self) -> String {
19        match self {
20            BadiDateError::DayInvalid(month, day, max_day) => {
21                format!(
22                    "ERROR: Invalid Badi month: day {} is not in the range [1-{}] for {}",
23                    day,
24                    max_day,
25                    month.description()
26                )
27            }
28            BadiDateError::MonthInvalid(month) => match month {
29                BadiMonth::Month(month) => {
30                    format!(
31                        "ERROR: BadiMonth::Month({}) is not in the range [1-19]",
32                        month
33                    )
34                }
35                BadiMonth::AyyamIHa => String::new(),
36            },
37            BadiDateError::DateNotSupported => {
38                format!(
39                    "The given date is not supported; year must be in the range [1-{}]",
40                    LAST_YEAR_SUPPORTED
41                )
42            }
43        }
44    }
45}
46
47impl Display for BadiDateError {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        write!(f, "BadiDateError: {}", self.message())
50    }
51}