use ::core::fmt;
pub type Result<T> = ::core::result::Result<T, Error>;
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum Error {
SwStart,
SwStop,
SwGuard,
GuardNew,
}
impl Error {
#[inline]
#[must_use]
pub const fn expects_running(&self) -> bool {
match self {
Self::SwStop | Self::GuardNew => true,
Self::SwStart | Self::SwGuard => false,
}
}
#[inline]
#[must_use]
pub const fn expects_stopped(&self) -> bool {
!self.expects_running()
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[inline]
const fn state_to_str(running: bool) -> &'static str {
if running {
"running"
} else {
"stopped"
}
}
let verb = match self {
Self::SwStart => "started stopwatch",
Self::SwStop => "stopped stopwatch",
Self::SwGuard => "guarded stopwatch",
Self::GuardNew => "created stopwatch guard",
};
let state = state_to_str(!self.expects_running());
let expected_state = state_to_str(self.expects_running());
write!(f, "{verb} while {state}, but expected {expected_state}")
}
}
#[cfg(feature = "std")]
impl ::std::error::Error for Error {}
#[cfg(all(feature = "nightly", not(feature = "std")))]
impl ::core::error::Error for Error {}