From 5114eec704b5850ba0f3f6c23060a20e1f275785 Mon Sep 17 00:00:00 2001 From: rorychatt Date: Fri, 17 Apr 2026 08:35:09 +0200 Subject: [PATCH] [00223] Add typed EventName enum to event registry --- rusty/src/core/event_registry.rs | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/rusty/src/core/event_registry.rs b/rusty/src/core/event_registry.rs index 724621f..85b7e45 100644 --- a/rusty/src/core/event_registry.rs +++ b/rusty/src/core/event_registry.rs @@ -1,6 +1,36 @@ use std::collections::HashMap; use std::sync::Arc; +/// Typed event names for compile-time safety. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum EventName { + Click, + Change, +} + +impl EventName { + pub fn as_str(&self) -> &'static str { + match self { + EventName::Click => "click", + EventName::Change => "change", + } + } + + pub fn from_str(s: &str) -> Option { + match s { + "click" => Some(EventName::Click), + "change" => Some(EventName::Change), + _ => None, + } + } +} + +impl std::fmt::Display for EventName { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.as_str()) + } +} + /// Type alias for event handler callbacks. /// Receives the event arguments as a serde_json::Value. pub type EventCallback = Arc; @@ -41,6 +71,27 @@ impl EventRegistry { self.handlers.extend(other.handlers); } + /// Register a callback using a typed event name. + pub fn register_typed( + &mut self, + widget_id: &str, + event: EventName, + callback: EventCallback, + ) { + self.register(widget_id, event.as_str(), callback); + } + + /// Dispatch an event using a typed event name. + /// Returns true if a handler was found and invoked, false otherwise. + pub fn dispatch_typed( + &self, + widget_id: &str, + event: EventName, + args: serde_json::Value, + ) -> bool { + self.dispatch(widget_id, event.as_str(), args) + } + /// Remove all registered handlers. pub fn clear(&mut self) { self.handlers.clear();