1
1
use uuid:: Uuid ;
2
2
use serde:: { Deserialize , Serialize } ;
3
3
4
- /// Agent ID
5
- ///
6
- /// A newtype is used to prevent mixing them with other ID values.
7
- #[ derive( Debug , Clone , PartialEq , Serialize , Deserialize ) ]
8
- pub struct AgentId ( Uuid ) ;
4
+ /// Macro to define a newtype ID based on Uuid.
5
+ /// Accepts doc comments and other derive-able attributes.
6
+ macro_rules! define_id_type {
7
+ // This arm handles calls that do specify extra derives (like Eq).
8
+ ( $( #[ $attr: meta] ) * $name: ident, $( $extra_derive: ident) ,* ) => {
9
+ $( #[ $attr] ) *
10
+ #[ derive( Debug , Clone , PartialEq , Serialize , Deserialize , Eq , Hash , $( $extra_derive) ,* ) ]
11
+ pub struct $name( Uuid ) ;
9
12
10
- impl AgentId {
11
- pub fn new ( ) -> Self {
12
- Self ( Uuid :: new_v4 ( ) )
13
- }
14
- }
13
+ impl $name {
14
+ /// Creates a new random ID.
15
+ pub fn new( ) -> Self {
16
+ Self ( Uuid :: new_v4( ) )
17
+ }
18
+ }
15
19
16
- impl From < Uuid > for AgentId {
17
- fn from ( uuid : Uuid ) -> Self {
18
- Self ( uuid)
19
- }
20
- }
20
+ /// Allows creating an ID from a Uuid.
21
+ impl From <Uuid > for $name {
22
+ fn from( uuid: Uuid ) -> Self {
23
+ Self ( uuid)
24
+ }
25
+ }
21
26
22
- /// Thread ID
23
- ///
24
- /// A newtype is used to prevent mixing them with other ID values.
25
- #[ derive( Debug , Clone , PartialEq , Serialize , Deserialize ) ]
26
- pub struct ThreadId ( Uuid ) ;
27
+ /// Allows converting an ID back into a Uuid.
28
+ impl From <$name> for Uuid {
29
+ fn from( id: $name) -> Self {
30
+ id. 0
31
+ }
32
+ }
27
33
28
- impl ThreadId {
29
- pub fn new ( ) -> Self {
30
- Self ( Uuid :: new_v4 ( ) )
31
- }
32
- }
34
+ /// Allows getting a reference to the inner Uuid.
35
+ impl AsRef <Uuid > for $name {
36
+ fn as_ref( & self ) -> & Uuid {
37
+ & self . 0
38
+ }
39
+ }
33
40
34
- impl From < Uuid > for ThreadId {
35
- fn from ( uuid : Uuid ) -> Self {
36
- Self ( uuid)
37
- }
38
- }
41
+ /// Allows printing the ID.
42
+ impl std:: fmt:: Display for $name {
43
+ fn fmt( & self , f: & mut std:: fmt:: Formatter <' _>) -> std:: fmt:: Result {
44
+ write!( f, "{}" , self . 0 )
45
+ }
46
+ }
39
47
40
- #[ derive( Debug , Clone , PartialEq , Serialize , Deserialize ) ]
41
- pub struct RunId ( Uuid ) ;
48
+ /// Allows parsing an ID from a string slice.
49
+ impl std:: str :: FromStr for $name {
50
+ type Err = uuid:: Error ;
42
51
43
- /// Run ID
44
- ///
45
- /// A newtype is used to prevent mixing them with other ID values.
46
- impl RunId {
47
- pub fn new ( ) -> Self {
48
- Self ( Uuid :: new_v4 ( ) )
49
- }
50
- }
52
+ fn from_str( s: & str ) -> Result <Self , Self :: Err > {
53
+ Ok ( Self ( Uuid :: parse_str( s) ?) )
54
+ }
55
+ }
51
56
52
- impl From < Uuid > for RunId {
53
- fn from ( uuid : Uuid ) -> Self {
54
- Self ( uuid)
55
- }
57
+ /// Allows comparing the ID with a Uuid.
58
+ impl PartialEq <Uuid > for $name {
59
+ fn eq( & self , other: & Uuid ) -> bool {
60
+ self . 0 == * other
61
+ }
62
+ }
63
+
64
+ /// Allows comparing the ID with a string slice.
65
+ impl PartialEq <str > for $name {
66
+ fn eq( & self , other: & str ) -> bool {
67
+ if let Ok ( uuid) = Uuid :: parse_str( other) {
68
+ self . 0 == uuid
69
+ } else {
70
+ false
71
+ }
72
+ }
73
+ }
74
+ } ;
75
+ ( $( #[ $attr: meta] ) * $name: ident) => {
76
+ define_id_type!( $( #[ $attr] ) * $name, ) ;
77
+ } ;
56
78
}
57
79
58
- /// Tool Call ID
59
- ///
60
- /// A newtype is used to prevent mixing them with other ID values.
61
- #[ derive( Debug , Clone , PartialEq , Eq , Serialize , Deserialize ) ]
62
- pub struct ToolCallId ( Uuid ) ;
80
+ define_id_type ! (
81
+ /// Agent ID
82
+ ///
83
+ /// A newtype is used to prevent mixing them with other ID values.
84
+ AgentId
85
+ ) ;
63
86
64
- impl ToolCallId {
65
- pub fn new ( ) -> Self {
66
- Self ( Uuid :: new_v4 ( ) )
67
- }
68
- }
87
+ define_id_type ! (
88
+ /// Thread ID
89
+ ///
90
+ /// A newtype is used to prevent mixing them with other ID values.
91
+ ThreadId
92
+ ) ;
93
+
94
+ define_id_type ! (
95
+ /// Run ID
96
+ ///
97
+ /// A newtype is used to prevent mixing them with other ID values.
98
+ RunId
99
+ ) ;
100
+
101
+ define_id_type ! (
102
+ /// Tool Call ID
103
+ ///
104
+ /// A newtype is used to prevent mixing them with other ID values.
105
+ ToolCallId
106
+ ) ;
69
107
70
- impl From < Uuid > for ToolCallId {
71
- fn from ( uuid : Uuid ) -> Self {
72
- Self ( uuid)
73
- }
74
- }
108
+ define_id_type ! (
109
+ /// Message ID
110
+ ///
111
+ /// A newtype is used to prevent mixing them with other ID values.
112
+ MessageId
113
+ ) ;
0 commit comments