use core::fmt;
use crate::Moment;
use super::HostPhase;
#[derive(Debug)]
pub enum HostError<DutyError, ClockError> {
NotRunning {
phase: HostPhase,
},
Clock(ClockError),
ClockRegressed {
previous: Moment,
observed: Moment,
},
Duty(DutyError),
}
impl<DutyError, ClockError> HostError<DutyError, ClockError> {
pub const fn phase(&self) -> Option<HostPhase> {
match self {
Self::NotRunning { phase } => Some(*phase),
Self::Clock(_) | Self::ClockRegressed { .. } | Self::Duty(_) => None,
}
}
}
impl<DutyError, ClockError> fmt::Display for HostError<DutyError, ClockError>
where
DutyError: fmt::Display,
ClockError: fmt::Display,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotRunning { phase } => write!(formatter, "host is already {phase:?}"),
Self::Clock(source) => write!(formatter, "host clock failed: {source}"),
Self::ClockRegressed { previous, observed } => write!(
formatter,
"host clock regressed from {}ns to {}ns",
previous.as_nanos(),
observed.as_nanos()
),
Self::Duty(source) => write!(formatter, "host duty failed: {source}"),
}
}
}
impl<DutyError, ClockError> core::error::Error for HostError<DutyError, ClockError>
where
DutyError: core::error::Error + 'static,
ClockError: core::error::Error + 'static,
{
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Clock(source) => Some(source),
Self::Duty(source) => Some(source),
Self::NotRunning { .. } | Self::ClockRegressed { .. } => None,
}
}
}