1use serde::{Deserialize, Serialize};
2#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
3pub enum ActorKind {
5 Human,
7 Agent,
9 Service,
11}
12
13#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14#[serde(deny_unknown_fields)]
16pub struct ActorId {
17 pub kind: ActorKind,
19 pub name: String,
21}
22
23impl ActorId {
24 pub fn human(name: impl Into<String>) -> Self {
26 Self {
27 kind: ActorKind::Human,
28 name: name.into(),
29 }
30 }
31
32 pub fn agent(name: impl Into<String>) -> Self {
34 Self {
35 kind: ActorKind::Agent,
36 name: name.into(),
37 }
38 }
39
40 pub fn service(name: impl Into<String>) -> Self {
42 Self {
43 kind: ActorKind::Service,
44 name: name.into(),
45 }
46 }
47}
48
49impl std::fmt::Display for ActorKind {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 f.write_str(match self {
53 Self::Human => "human",
54 Self::Agent => "agent",
55 Self::Service => "service",
56 })
57 }
58}
59
60impl std::fmt::Display for ActorId {
61 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62 write!(f, "{}:{}", self.kind, self.name)
63 }
64}
65
66#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
68pub enum ActorParseError {
69 #[error("actor {0:?} must be human:NAME, agent:NAME, or service:NAME")]
71 MissingKind(String),
72 #[error("unknown actor kind {0}")]
74 UnknownKind(String),
75 #[error("actor name must not be empty")]
77 EmptyName,
78}
79
80impl std::str::FromStr for ActorId {
81 type Err = ActorParseError;
82
83 fn from_str(value: &str) -> Result<Self, Self::Err> {
86 let (kind, name) = value
87 .split_once(':')
88 .ok_or_else(|| ActorParseError::MissingKind(value.into()))?;
89 if name.trim().is_empty() {
90 return Err(ActorParseError::EmptyName);
91 }
92 let kind = match kind {
93 "human" => ActorKind::Human,
94 "agent" => ActorKind::Agent,
95 "service" => ActorKind::Service,
96 _ => return Err(ActorParseError::UnknownKind(kind.into())),
97 };
98 Ok(Self {
99 kind,
100 name: name.into(),
101 })
102 }
103}