use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum AgentState {
#[default]
Idle,
Spawning,
Running,
Working,
Stuck,
Done,
Stopped,
Dead,
}
impl AgentState {
pub fn as_str(&self) -> &'static str {
match self {
AgentState::Idle => "idle",
AgentState::Spawning => "spawning",
AgentState::Running => "running",
AgentState::Working => "working",
AgentState::Stuck => "stuck",
AgentState::Done => "done",
AgentState::Stopped => "stopped",
AgentState::Dead => "dead",
}
}
pub fn is_active(&self) -> bool {
matches!(
self,
AgentState::Spawning | AgentState::Running | AgentState::Working
)
}
pub fn is_terminal(&self) -> bool {
matches!(self, AgentState::Done | AgentState::Stopped | AgentState::Dead)
}
}
impl fmt::Display for AgentState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl FromStr for AgentState {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"idle" => Ok(AgentState::Idle),
"spawning" => Ok(AgentState::Spawning),
"running" => Ok(AgentState::Running),
"working" => Ok(AgentState::Working),
"stuck" => Ok(AgentState::Stuck),
"done" => Ok(AgentState::Done),
"stopped" => Ok(AgentState::Stopped),
"dead" => Ok(AgentState::Dead),
_ => Err(format!("unknown agent state: {}", s)),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum MolType {
Swarm,
Patrol,
#[default]
Work,
}
impl MolType {
pub fn as_str(&self) -> &'static str {
match self {
MolType::Swarm => "swarm",
MolType::Patrol => "patrol",
MolType::Work => "work",
}
}
}
impl fmt::Display for MolType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl FromStr for MolType {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"swarm" => Ok(MolType::Swarm),
"patrol" => Ok(MolType::Patrol),
"work" => Ok(MolType::Work),
_ => Err(format!("unknown molecule type: {}", s)),
}
}
}