Skip to main content

yoagent_state/
ids.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3use uuid::Uuid;
4
5macro_rules! id_type {
6    ($name:ident, $prefix:literal) => {
7        #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
8        pub struct $name(pub String);
9
10        impl $name {
11            pub fn new(value: impl Into<String>) -> Self {
12                Self(value.into())
13            }
14
15            pub fn generate() -> Self {
16                Self(format!("{}_{}", $prefix, Uuid::new_v4().simple()))
17            }
18
19            pub fn as_str(&self) -> &str {
20                &self.0
21            }
22        }
23
24        impl fmt::Display for $name {
25            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26                f.write_str(&self.0)
27            }
28        }
29
30        impl From<&str> for $name {
31            fn from(value: &str) -> Self {
32                Self(value.to_string())
33            }
34        }
35
36        impl From<String> for $name {
37            fn from(value: String) -> Self {
38                Self(value)
39            }
40        }
41    };
42}
43
44id_type!(EventId, "event");
45id_type!(NodeId, "node");
46id_type!(PatchId, "patch");
47id_type!(RunId, "run");
48id_type!(GoalId, "goal");
49id_type!(TaskId, "task");
50id_type!(ObservationId, "observation");
51id_type!(HypothesisId, "hypothesis");
52id_type!(EvalId, "eval");
53id_type!(DecisionId, "decision");
54id_type!(ArtifactId, "artifact");
55id_type!(FrameId, "frame");
56id_type!(ForkId, "fork");
57id_type!(BehaviorId, "behavior");
58id_type!(PolicyId, "policy");
59id_type!(PackId, "pack");
60id_type!(ViewId, "view");