Skip to main content

euv_core/event/name/
impl.rs

1use crate::*;
2
3/// Implements `Display` for `NativeEventName` to provide string representation.
4///
5/// This also provides `ToString::to_string()` via the standard blanket implementation,
6/// which is the preferred way to obtain the event name as a `String`.
7impl std::fmt::Display for NativeEventName {
8    /// Formats this event name as a string.
9    ///
10    /// # Arguments
11    ///
12    /// - `&mut Formatter<'_>`- The formatter.
13    ///
14    /// # Returns
15    ///
16    /// - `std::fmt::Result`- The formatting result.
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        match self {
19            NativeEventName::Click => write!(f, "click"),
20            NativeEventName::DblClick => write!(f, "dblclick"),
21            NativeEventName::MouseDown => write!(f, "mousedown"),
22            NativeEventName::MouseUp => write!(f, "mouseup"),
23            NativeEventName::MouseMove => write!(f, "mousemove"),
24            NativeEventName::MouseEnter => write!(f, "mouseenter"),
25            NativeEventName::MouseLeave => write!(f, "mouseleave"),
26            NativeEventName::MouseOver => write!(f, "mouseover"),
27            NativeEventName::MouseOut => write!(f, "mouseout"),
28            NativeEventName::ContextMenu => write!(f, "contextmenu"),
29            NativeEventName::Input => write!(f, "input"),
30            NativeEventName::KeyDown => write!(f, "keydown"),
31            NativeEventName::KeyUp => write!(f, "keyup"),
32            NativeEventName::KeyPress => write!(f, "keypress"),
33            NativeEventName::Focus => write!(f, "focus"),
34            NativeEventName::Blur => write!(f, "blur"),
35            NativeEventName::FocusIn => write!(f, "focusin"),
36            NativeEventName::FocusOut => write!(f, "focusout"),
37            NativeEventName::Submit => write!(f, "submit"),
38            NativeEventName::Change => write!(f, "change"),
39            NativeEventName::Drag => write!(f, "drag"),
40            NativeEventName::DragStart => write!(f, "dragstart"),
41            NativeEventName::DragEnd => write!(f, "dragend"),
42            NativeEventName::DragOver => write!(f, "dragover"),
43            NativeEventName::DragEnter => write!(f, "dragenter"),
44            NativeEventName::DragLeave => write!(f, "dragleave"),
45            NativeEventName::Drop => write!(f, "drop"),
46            NativeEventName::TouchStart => write!(f, "touchstart"),
47            NativeEventName::TouchEnd => write!(f, "touchend"),
48            NativeEventName::TouchMove => write!(f, "touchmove"),
49            NativeEventName::TouchCancel => write!(f, "touchcancel"),
50            NativeEventName::Wheel => write!(f, "wheel"),
51            NativeEventName::Copy => write!(f, "copy"),
52            NativeEventName::Cut => write!(f, "cut"),
53            NativeEventName::Paste => write!(f, "paste"),
54            NativeEventName::Play => write!(f, "play"),
55            NativeEventName::Pause => write!(f, "pause"),
56            NativeEventName::Ended => write!(f, "ended"),
57            NativeEventName::LoadedData => write!(f, "loadeddata"),
58            NativeEventName::CanPlay => write!(f, "canplay"),
59            NativeEventName::VolumeChange => write!(f, "volumechange"),
60            NativeEventName::TimeUpdate => write!(f, "timeupdate"),
61            NativeEventName::HashChange => write!(f, "hashchange"),
62            NativeEventName::PopState => write!(f, "popstate"),
63            NativeEventName::Resize => write!(f, "resize"),
64            NativeEventName::Scroll => write!(f, "scroll"),
65            NativeEventName::Load => write!(f, "load"),
66            NativeEventName::Unload => write!(f, "unload"),
67            NativeEventName::BeforeUnload => write!(f, "beforeunload"),
68            NativeEventName::Error => write!(f, "error"),
69            NativeEventName::Online => write!(f, "online"),
70            NativeEventName::Offline => write!(f, "offline"),
71            NativeEventName::VisibilityChange => write!(f, "visibilitychange"),
72            NativeEventName::AnimationStart => write!(f, "animationstart"),
73            NativeEventName::AnimationEnd => write!(f, "animationend"),
74            NativeEventName::AnimationIteration => write!(f, "animationiteration"),
75            NativeEventName::TransitionStart => write!(f, "transitionstart"),
76            NativeEventName::TransitionEnd => write!(f, "transitionend"),
77            NativeEventName::TransitionRun => write!(f, "transitionrun"),
78            NativeEventName::EuvSignalUpdate => write!(f, "__euv_signal_update__"),
79            NativeEventName::Other(name) => write!(f, "{}", name),
80        }
81    }
82}
83
84/// Implements `FromStr` for `NativeEventName`, enabling parsing from Web-standard
85/// lowercase event name strings (e.g., `"click"`, `"dblclick"`, `"mouseenter"`).
86///
87/// This is the standard library trait for string-to-enum parsing, used by the
88/// `html!` macro to convert event attribute keys (e.g., `onclick`) into the
89/// corresponding `NativeEventName` variant at runtime.
90///
91/// # Supported names
92///
93/// All standard DOM event names in lowercase: `click`, `dblclick`, `mousedown`,
94/// `mouseup`, `mousemove`, `mouseenter`, `mouseleave`, `mouseover`, `mouseout`,
95/// `contextmenu`, `input`, `keydown`, `keyup`, `keypress`, `focus`, `blur`,
96/// `focusin`, `focusout`, `submit`, `change`, `drag`, `dragstart`, `dragend`,
97/// `dragover`, `dragenter`, `dragleave`, `drop`, `touchstart`, `touchend`,
98/// `touchmove`, `touchcancel`, `wheel`, `copy`, `cut`, `paste`, `play`,
99/// `pause`, `ended`, `loadeddata`, `canplay`, `volumechange`, `timeupdate`,
100/// `hashchange`, `popstate`, `resize`, `scroll`, `load`, `unload`,
101/// `beforeunload`, `error`, `online`, `offline`, `visibilitychange`,
102/// `animationstart`, `animationend`, `animationiteration`, `transitionstart`,
103/// `transitionend`, `transitionrun`.
104///
105/// Unknown names fall back to `NativeEventName::Other(name)`.
106impl std::str::FromStr for NativeEventName {
107    type Err = ParseNativeEventNameError;
108
109    /// Parses a Web-standard event name string into a `NativeEventName`.
110    ///
111    /// # Arguments
112    ///
113    /// - `&str` - The event name string (e.g., `"click"`, `"mouseenter"`).
114    ///
115    /// # Returns
116    ///
117    /// - `Result<NativeEventName, ParseNativeEventNameError>` - The parsed enum variant,
118    ///   or an error if the input is empty.
119    fn from_str(data: &str) -> Result<Self, Self::Err> {
120        match data {
121            "click" => Ok(NativeEventName::Click),
122            "dblclick" => Ok(NativeEventName::DblClick),
123            "mousedown" => Ok(NativeEventName::MouseDown),
124            "mouseup" => Ok(NativeEventName::MouseUp),
125            "mousemove" => Ok(NativeEventName::MouseMove),
126            "mouseenter" => Ok(NativeEventName::MouseEnter),
127            "mouseleave" => Ok(NativeEventName::MouseLeave),
128            "mouseover" => Ok(NativeEventName::MouseOver),
129            "mouseout" => Ok(NativeEventName::MouseOut),
130            "contextmenu" => Ok(NativeEventName::ContextMenu),
131            "input" => Ok(NativeEventName::Input),
132            "keydown" => Ok(NativeEventName::KeyDown),
133            "keyup" => Ok(NativeEventName::KeyUp),
134            "keypress" => Ok(NativeEventName::KeyPress),
135            "focus" => Ok(NativeEventName::Focus),
136            "blur" => Ok(NativeEventName::Blur),
137            "focusin" => Ok(NativeEventName::FocusIn),
138            "focusout" => Ok(NativeEventName::FocusOut),
139            "submit" => Ok(NativeEventName::Submit),
140            "change" => Ok(NativeEventName::Change),
141            "drag" => Ok(NativeEventName::Drag),
142            "dragstart" => Ok(NativeEventName::DragStart),
143            "dragend" => Ok(NativeEventName::DragEnd),
144            "dragover" => Ok(NativeEventName::DragOver),
145            "dragenter" => Ok(NativeEventName::DragEnter),
146            "dragleave" => Ok(NativeEventName::DragLeave),
147            "drop" => Ok(NativeEventName::Drop),
148            "touchstart" => Ok(NativeEventName::TouchStart),
149            "touchend" => Ok(NativeEventName::TouchEnd),
150            "touchmove" => Ok(NativeEventName::TouchMove),
151            "touchcancel" => Ok(NativeEventName::TouchCancel),
152            "wheel" => Ok(NativeEventName::Wheel),
153            "copy" => Ok(NativeEventName::Copy),
154            "cut" => Ok(NativeEventName::Cut),
155            "paste" => Ok(NativeEventName::Paste),
156            "play" => Ok(NativeEventName::Play),
157            "pause" => Ok(NativeEventName::Pause),
158            "ended" => Ok(NativeEventName::Ended),
159            "loadeddata" => Ok(NativeEventName::LoadedData),
160            "canplay" => Ok(NativeEventName::CanPlay),
161            "volumechange" => Ok(NativeEventName::VolumeChange),
162            "timeupdate" => Ok(NativeEventName::TimeUpdate),
163            "hashchange" => Ok(NativeEventName::HashChange),
164            "popstate" => Ok(NativeEventName::PopState),
165            "resize" => Ok(NativeEventName::Resize),
166            "scroll" => Ok(NativeEventName::Scroll),
167            "load" => Ok(NativeEventName::Load),
168            "unload" => Ok(NativeEventName::Unload),
169            "beforeunload" => Ok(NativeEventName::BeforeUnload),
170            "error" => Ok(NativeEventName::Error),
171            "online" => Ok(NativeEventName::Online),
172            "offline" => Ok(NativeEventName::Offline),
173            "visibilitychange" => Ok(NativeEventName::VisibilityChange),
174            "animationstart" => Ok(NativeEventName::AnimationStart),
175            "animationend" => Ok(NativeEventName::AnimationEnd),
176            "animationiteration" => Ok(NativeEventName::AnimationIteration),
177            "transitionstart" => Ok(NativeEventName::TransitionStart),
178            "transitionend" => Ok(NativeEventName::TransitionEnd),
179            "transitionrun" => Ok(NativeEventName::TransitionRun),
180            "" => Err(ParseNativeEventNameError::new(data.to_string())),
181            other => Ok(NativeEventName::Other(other.to_string())),
182        }
183    }
184}