Skip to main content

rushai_protocol/
ids.rs

1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3
4macro_rules! id_type {
5    ($name:ident) => {
6        #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
7        #[serde(transparent)]
8        pub struct $name(String);
9
10        impl $name {
11            pub fn new() -> Self {
12                Self(Uuid::now_v7().to_string())
13            }
14
15            pub fn as_str(&self) -> &str {
16                &self.0
17            }
18        }
19
20        impl Default for $name {
21            fn default() -> Self {
22                Self::new()
23            }
24        }
25
26        impl std::fmt::Display for $name {
27            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28                f.write_str(&self.0)
29            }
30        }
31
32        impl From<String> for $name {
33            fn from(s: String) -> Self {
34                Self(s)
35            }
36        }
37
38        impl From<&str> for $name {
39            fn from(s: &str) -> Self {
40                Self(s.to_owned())
41            }
42        }
43    };
44}
45
46id_type!(SessionId);
47id_type!(MessageId);
48id_type!(CallId);
49id_type!(RequestId);