use std::fmt;
use chrono::NaiveDateTime;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SchedulerError {
DateTimeOutOfBounds {
what: &'static str,
datetime: NaiveDateTime,
start: NaiveDateTime,
end: NaiveDateTime,
},
BudgetMissingPeriods { budget_id: String },
UnknownBudgetId { goal_id: String, budget_id: String },
DuplicateBudgetId { budget_id: String },
InvalidBudgetBounds {
budget_id: String,
what: &'static str,
},
UnsupportedSchemaVersion { version: u32 },
NotSlotAligned {
field: &'static str,
value: usize,
slot_minutes: usize,
},
DateTimeNotAligned {
what: &'static str,
datetime: NaiveDateTime,
slot_minutes: usize,
},
CalendarSpanNotAligned {
span_minutes: i64,
slot_minutes: usize,
},
ValueOutOfRange { field: &'static str, value: usize },
}
impl fmt::Display for SchedulerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SchedulerError::DateTimeOutOfBounds {
what,
datetime,
start,
end,
} => write!(
f,
"{what} datetime {datetime:?} is more than one day outside the calendar window [{start:?}, {end:?}]"
),
SchedulerError::BudgetMissingPeriods { budget_id } => {
write!(f, "budget {budget_id:?} must have at least one period")
}
SchedulerError::UnknownBudgetId { goal_id, budget_id } => write!(
f,
"goal {goal_id:?} references unknown budgetId {budget_id:?}"
),
SchedulerError::DuplicateBudgetId { budget_id } => {
write!(f, "duplicate budget id {budget_id:?}")
}
SchedulerError::InvalidBudgetBounds { budget_id, what } => {
write!(f, "budget {budget_id:?} has invalid bounds: {what}")
}
SchedulerError::UnsupportedSchemaVersion { version } => {
write!(f, "unsupported schemaVersion {version}")
}
SchedulerError::NotSlotAligned {
field,
value,
slot_minutes,
} => write!(
f,
"{field} value {value} is not divisible by slot size {slot_minutes} minutes"
),
SchedulerError::DateTimeNotAligned {
what,
datetime,
slot_minutes,
} => write!(
f,
"{what} datetime {datetime:?} is not aligned to {slot_minutes}-minute slots"
),
SchedulerError::CalendarSpanNotAligned {
span_minutes,
slot_minutes,
} => write!(
f,
"calendar span of {span_minutes} minutes is not divisible by slot size {slot_minutes} minutes"
),
SchedulerError::ValueOutOfRange { field, value } => {
write!(
f,
"{field} value {value} is out of range after unit conversion"
)
}
}
}
}
impl std::error::Error for SchedulerError {}