use std::fmt;
use std::sync::Arc;
use std::sync::atomic::{AtomicU8, Ordering};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum AgentStatus {
Starting = 0,
Idle = 1,
Running = 2,
WaitingPermission = 3,
Error = 4,
Stopping = 5,
Stopped = 6,
}
impl fmt::Display for AgentStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Starting => write!(f, "starting"),
Self::Idle => write!(f, "idle"),
Self::Running => write!(f, "running"),
Self::WaitingPermission => write!(f, "waiting_permission"),
Self::Error => write!(f, "error"),
Self::Stopping => write!(f, "stopping"),
Self::Stopped => write!(f, "stopped"),
}
}
}
impl From<u8> for AgentStatus {
fn from(v: u8) -> Self {
match v {
0 => Self::Starting,
1 => Self::Idle,
2 => Self::Running,
3 => Self::WaitingPermission,
4 => Self::Error,
5 => Self::Stopping,
6 => Self::Stopped,
_ => Self::Error,
}
}
}
#[derive(Debug, Clone)]
pub struct StatusTracker {
inner: Arc<AtomicU8>,
}
impl StatusTracker {
pub fn new() -> Self {
Self { inner: Arc::new(AtomicU8::new(AgentStatus::Starting as u8)) }
}
pub fn get(&self) -> AgentStatus {
AgentStatus::from(self.inner.load(Ordering::Relaxed))
}
pub fn set(&self, status: AgentStatus) {
self.inner.store(status as u8, Ordering::Relaxed);
}
pub fn is_idle(&self) -> bool {
self.get() == AgentStatus::Idle
}
pub fn is_running(&self) -> bool {
self.get() == AgentStatus::Running
}
pub fn is_waiting_permission(&self) -> bool {
self.get() == AgentStatus::WaitingPermission
}
pub fn is_done(&self) -> bool {
matches!(self.get(), AgentStatus::Stopped | AgentStatus::Error)
}
}
impl Default for StatusTracker {
fn default() -> Self {
Self::new()
}
}