use std::fmt;
use crate::consts::{AGENT0_NAME, AGENTINFINITY_NAME, CLONE_PREFIX};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AgentId {
Agent0,
Clone(u32),
Agentinfinity,
}
impl AgentId {
pub fn from_number(n: u32) -> Self {
if n == 0 { Self::Agent0 } else { Self::Clone(n) }
}
pub fn name(&self) -> String {
match self {
Self::Agent0 => AGENT0_NAME.to_string(),
Self::Clone(n) => format!("{CLONE_PREFIX}{n}"),
Self::Agentinfinity => AGENTINFINITY_NAME.to_string(),
}
}
pub fn env_n(&self) -> String {
match self {
Self::Agent0 => "0".to_string(),
Self::Clone(n) => n.to_string(),
Self::Agentinfinity => "infinity".to_string(),
}
}
pub fn is_agent0(&self) -> bool {
matches!(self, Self::Agent0)
}
pub fn is_agentinfinity(&self) -> bool {
matches!(self, Self::Agentinfinity)
}
}
impl fmt::Display for AgentId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.name())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn name_roundtrip() {
assert_eq!(AgentId::Agent0.name(), "agent0");
assert_eq!(AgentId::Clone(5).name(), "agent5");
assert_eq!(AgentId::Agentinfinity.name(), "agentinfinity");
}
#[test]
fn from_number_zero_is_agent0() {
assert_eq!(AgentId::from_number(0), AgentId::Agent0);
assert_eq!(AgentId::from_number(3), AgentId::Clone(3));
}
#[test]
fn env_n_strings() {
assert_eq!(AgentId::Agent0.env_n(), "0");
assert_eq!(AgentId::Clone(7).env_n(), "7");
assert_eq!(AgentId::Agentinfinity.env_n(), "infinity");
}
}