1use alloc::{borrow::Cow, string::String};
2use core::fmt::{Display, Formatter};
3
4use miden_crypto::{
5 field::PrimeField64,
6 utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
7};
8
9use crate::{Felt, utils::hash_string_to_word};
10
11mod sys_events;
12pub use sys_events::SystemEvent;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)]
30#[cfg_attr(
31 all(feature = "arbitrary", test),
32 miden_test_serialization_macros::serialization_test
33)]
34pub struct EventId(u64);
35
36impl EventId {
37 pub fn from_name(name: impl AsRef<str>) -> Self {
52 let digest_word = hash_string_to_word(name.as_ref());
53 Self(digest_word[0].as_canonical_u64())
54 }
55
56 pub fn from_felt(event_id: Felt) -> Self {
58 Self(event_id.as_canonical_u64())
59 }
60
61 pub const fn from_u64(event_id: u64) -> Self {
63 Self(event_id % Felt::ORDER_U64)
64 }
65
66 pub const fn as_felt(&self) -> Felt {
68 Felt::new_unchecked(self.0)
69 }
70
71 pub const fn as_u64(&self) -> u64 {
73 self.0
74 }
75}
76
77impl Display for EventId {
78 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
79 Display::fmt(&self.0, f)
80 }
81}
82
83#[derive(Debug, Clone, PartialEq, Eq)]
98#[cfg_attr(
99 all(feature = "arbitrary", test),
100 miden_test_serialization_macros::serialization_test
101)]
102pub struct EventName(Cow<'static, str>);
103
104impl EventName {
105 pub const RESERVED_NAMESPACE: &'static str = "sys::";
110
111 pub const fn new(name: &'static str) -> Self {
115 Self(Cow::Borrowed(name))
116 }
117
118 pub fn from_string(name: String) -> Self {
122 Self(Cow::Owned(name))
123 }
124
125 pub fn as_str(&self) -> &str {
127 self.0.as_ref()
128 }
129
130 pub fn to_event_id(&self) -> EventId {
134 EventId::from_name(self.as_str())
135 }
136
137 pub fn is_reserved(&self) -> bool {
141 self.as_str().starts_with(Self::RESERVED_NAMESPACE)
142 }
143}
144
145impl Display for EventName {
146 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
147 write!(f, "{}", self.0)
148 }
149}
150
151impl AsRef<str> for EventName {
152 fn as_ref(&self) -> &str {
153 self.0.as_ref()
154 }
155}
156
157impl Serializable for EventId {
161 fn write_into<W: ByteWriter>(&self, target: &mut W) {
162 self.as_felt().write_into(target);
163 }
164}
165
166impl Deserializable for EventId {
167 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
168 Ok(Self(Felt::read_from(source)?.as_canonical_u64()))
169 }
170}
171
172impl Serializable for EventName {
173 fn write_into<W: ByteWriter>(&self, target: &mut W) {
174 self.0.as_ref().write_into(target)
176 }
177}
178
179impl Deserializable for EventName {
180 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
181 let name = String::read_from(source)?;
182 Ok(Self::from_string(name))
183 }
184}
185
186#[cfg(all(feature = "arbitrary", test))]
190impl proptest::prelude::Arbitrary for EventId {
191 type Parameters = ();
192
193 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
194 use proptest::prelude::*;
195 any::<u64>().prop_map(EventId::from_u64).boxed()
196 }
197
198 type Strategy = proptest::prelude::BoxedStrategy<Self>;
199}
200
201#[cfg(all(feature = "arbitrary", test))]
202impl proptest::prelude::Arbitrary for EventName {
203 type Parameters = ();
204
205 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
206 use proptest::prelude::*;
207
208 prop_oneof![
210 Just(EventName::new("test::static::event")),
212 Just(EventName::new("core::handler::example")),
213 Just(EventName::new("user::custom::event")),
214 any::<(u32, u32)>()
216 .prop_map(|(a, b)| EventName::from_string(format!("dynamic::event::{a}::{b}"))),
217 ]
218 .boxed()
219 }
220
221 type Strategy = proptest::prelude::BoxedStrategy<Self>;
222}
223
224#[cfg(test)]
228mod tests {
229 use alloc::string::ToString;
230
231 use super::*;
232
233 #[test]
234 fn event_basics() {
235 let id1 = EventId::from_u64(100);
237 assert_eq!(id1.as_u64(), 100);
238 assert_eq!(id1.as_felt(), Felt::new_unchecked(100));
239
240 let id2 = EventId::from_felt(Felt::new_unchecked(200));
241 assert_eq!(id2.as_u64(), 200);
242
243 let id3 = EventId::from_name("test::event");
245 let id4 = EventId::from_name("test::event");
246 assert_eq!(id3, id4);
247
248 let name1 = EventName::new("static::event");
250 assert_eq!(name1.as_str(), "static::event");
251 assert_eq!(format!("{name1}"), "static::event");
252
253 let name2 = EventName::from_string("dynamic::event".to_string());
254 assert_eq!(name2.as_str(), "dynamic::event");
255
256 assert_eq!(name1.to_event_id(), EventId::from_name("static::event"));
258 }
259
260 #[test]
261 fn event_name_reserved_namespace() {
262 assert!(EventName::new("sys::merkle_node_merge").is_reserved());
264 assert!(EventName::new("sys::not_a_real_event").is_reserved());
265 assert!(EventName::new("sys::").is_reserved());
266
267 assert!(!EventName::new("user::event").is_reserved());
268 assert!(!EventName::new("sys").is_reserved());
269 assert!(!EventName::new("").is_reserved());
270
271 for event in SystemEvent::all() {
273 assert!(event.event_name().is_reserved());
274 }
275 }
276}