use nanoserde::DeJson;
use crate::error::Error;
#[derive(Clone, Copy, Debug, Default, DeJson, PartialEq, Eq)]
#[repr(u8)]
pub enum BehaviorState {
#[default]
Idle = 0,
Running = 1,
Success = 2,
Failure = 3,
Skipped = 4,
}
impl BehaviorState {
#[must_use]
pub const fn is_active(&self) -> bool {
matches!(self, Self::Idle | Self::Skipped)
}
#[must_use]
pub const fn is_completed(&self) -> bool {
matches!(self, Self::Success | Self::Failure)
}
#[must_use]
pub const fn as_str(&self) -> &'static str {
match self {
Self::Idle => crate::IDLE,
Self::Running => crate::RUNNING,
Self::Success => crate::SUCCESS,
Self::Failure => crate::FAILURE,
Self::Skipped => crate::SKIPPED,
}
}
}
impl core::fmt::Display for BehaviorState {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl core::str::FromStr for BehaviorState {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.to_ascii_lowercase();
let res = match s.as_ref() {
"idle" => Self::Idle,
"running" => Self::Running,
"success" => Self::Success,
"failure" => Self::Failure,
"skipped" => Self::Skipped,
_ => {
return Err(Error::ParseError {
value: s.into(),
src: "BehaviorState::from_str()".into(),
});
}
};
Ok(res)
}
}