Skip to main content

CommonLibrary/IPC/
SkyEvent.rs

1//! # Sky Event Registry - single source of truth for Mountain → Sky/Wind events
2//!
3//! Mountain emits Tauri events on `sky://…` URIs to notify the webview of
4//! state changes that don't originate from a Wind-initiated `invoke` call.
5//! Historically each emit site used a free-text string literal and each Wind
6//! listener matched against its own free-text string - drift was invisible
7//! until runtime (the listener simply never fired).
8//!
9//! `SkyEvent` is the enumerated registry. Mountain callers dispatch on the
10//! variant; the wire string is produced by `AsStr()` and parsed by `FromStr`.
11//! The matching TypeScript const object lives at
12//! `Element/Wind/Source/IPC/SkyEvent.ts` - kept in sync by convention, same
13//! protocol as the `Channel` registry.
14//!
15//! ## Adding a new event
16//!
17//! 1. Add the variant here AND in `Element/Wind/Source/IPC/SkyEvent.ts`.
18//! 2. Emit from Mountain:
19//!    `ApplicationHandle.emit(SkyEvent::TerminalData.AsStr(), Payload)`.
20//! 3. Subscribe from Wind: `IPCService.events(SkyEvent.TerminalData)`.
21//!
22//! ## Why a declarative macro?
23//!
24//! Same rationale as `Channel`: the variant → wire-string mapping is pure
25//! data. `DefineSkyEvents!` expands it into enum body + `AsStr` + `All` +
26//! `FromStr` in one pass so adding an event is a single-line change that
27//! compilers can't forget.
28
29#![allow(non_snake_case, non_camel_case_types)]
30
31macro_rules! DefineSkyEvents {
32	($($Variant:ident => $Wire:literal,)* $(,)?) => {
33		/// Enumerated Mountain → Sky/Wind event identifiers.
34		#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
35		pub enum SkyEvent {
36			$($Variant,)*
37		}
38
39		impl SkyEvent {
40			/// Wire string produced on the Tauri event transport.
41			pub fn AsStr(&self) -> &'static str {
42				match self {
43					$(Self::$Variant => $Wire,)*
44				}
45			}
46
47			/// Full set of events, in declaration order.
48			pub fn All() -> &'static [Self] {
49				&[$(Self::$Variant,)*]
50			}
51		}
52
53		impl ::std::fmt::Display for SkyEvent {
54			fn fmt(&self, Formatter:&mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
55				Formatter.write_str(self.AsStr())
56			}
57		}
58
59		impl ::std::str::FromStr for SkyEvent {
60			type Err = ::std::string::String;
61
62			fn from_str(Wire:&str) -> ::std::result::Result<Self, Self::Err> {
63				match Wire {
64					$($Wire => Ok(Self::$Variant),)*
65					_ => Err(format!("unknown Sky event: {}", Wire)),
66				}
67			}
68		}
69	};
70}
71
72DefineSkyEvents! {
73	// --- Configuration ---
74	ConfigurationChanged                          => "sky://configuration/changed",
75
76	// --- Debug ---
77	DebugDapMessage                               => "sky://debug/dap-message",
78	DebugRegister                                 => "sky://debug/register",
79	DebugStart                                    => "sky://debug/start",
80	DebugStop                                     => "sky://debug/stop",
81
82	// --- Diagnostics ---
83	DiagnosticsChanged                            => "sky://diagnostics/changed",
84
85	// --- CustomEditor ---
86	CustomEditorSaved                             => "sky://customEditor/saved",
87
88	// --- Dialog ---
89	DialogOpen                                    => "sky://dialog/open",
90	DialogSave                                    => "sky://dialog/save",
91
92	// --- Documents ---
93	DocumentsOpen                                 => "sky://documents/open",
94	DocumentsRenamed                              => "sky://documents/renamed",
95	DocumentsSaved                                => "sky://documents/saved",
96
97	// --- Editor ---
98	EditorApplyEdits                              => "sky://editor/applyEdits",
99	EditorOpenDocument                            => "sky://editor/openDocument",
100	EditorSaveAll                                 => "sky://editor/saveAll",
101
102	// --- Extensions ---
103	ExtensionsInstalled                           => "sky://extensions/installed",
104	ExtensionsUninstalled                         => "sky://extensions/uninstalled",
105
106	// --- ExtHost ---
107	ExtHostDebugClose                             => "sky://exthost/debug-close",
108	ExtHostDebugReload                            => "sky://exthost/debug-reload",
109
110	// --- Input ---
111	InputBoxShow                                  => "sky://input-box/show",
112
113	// --- Language ---
114	LanguageConfigure                             => "sky://language/configure",
115	LanguagesSetDocumentLanguage                  => "sky://languages/setDocumentLanguage",
116
117	// --- Lifecycle ---
118	LifecyclePhaseChanged                         => "sky://lifecycle/phaseChanged",
119	LifecycleWillShutdown                         => "sky://lifecycle/willShutdown",
120
121	// --- Native ---
122	NativeOpenExternal                            => "sky://native/openExternal",
123
124	// --- Notifications ---
125	NotificationProgressBegin                     => "sky://notification/progress-begin",
126	NotificationProgressEnd                       => "sky://notification/progress-end",
127	NotificationProgressUpdate                    => "sky://notification/progress-update",
128	NotificationShow                              => "sky://notification/show",
129
130	// --- Output ---
131	OutputAppend                                  => "sky://output/append",
132	OutputClear                                   => "sky://output/clear",
133	OutputCreate                                  => "sky://output/create",
134	OutputDispose                                 => "sky://output/dispose",
135	OutputReplace                                 => "sky://output/replace",
136	OutputReveal                                  => "sky://output/reveal",
137	OutputShow                                    => "sky://output/show",
138
139	// --- Progress ---
140	ProgressBegin                                 => "sky://progress/begin",
141	ProgressComplete                              => "sky://progress/complete",
142	ProgressEnd                                   => "sky://progress/end",
143	ProgressReport                                => "sky://progress/report",
144	ProgressStart                                 => "sky://progress/start",
145	ProgressUpdate                                => "sky://progress/update",
146
147	// --- QuickPick ---
148	QuickPickShow                                 => "sky://quickpick/show",
149
150	// --- Source Control ---
151	SCMGroupChanged                               => "sky://scm/group/changed",
152	SCMProviderAdded                              => "sky://scm/provider/added",
153	SCMProviderChanged                            => "sky://scm/provider/changed",
154	SCMProviderRemoved                            => "sky://scm/provider/removed",
155	SCMRegister                                   => "sky://scm/register",
156	SCMUpdateGroup                                => "sky://scm/updateGroup",
157
158	// --- Status bar ---
159	// Canonical prefix is `sky://statusbar/` (no hyphen). The earlier
160	// `sky://status-bar/message` channel was an accidental fork produced by
161	// a separate emit site and has been consolidated onto
162	// `sky://statusbar/set-message`.
163	StatusBarCreate                               => "sky://statusbar/create",
164	StatusBarDispose                              => "sky://statusbar/dispose",
165	StatusBarDisposeEntry                         => "sky://statusbar/dispose-entry",
166	StatusBarDisposeMessage                       => "sky://statusbar/dispose-message",
167	StatusBarSetEntry                             => "sky://statusbar/set-entry",
168	StatusBarSetMessage                           => "sky://statusbar/set-message",
169	StatusBarUpdate                               => "sky://statusbar/update",
170
171	// --- Task ---
172	TaskExecute                                   => "sky://task/execute",
173	TaskTerminate                                 => "sky://task/terminate",
174
175	// --- Terminal ---
176	TerminalClosed                                => "sky://terminal/closed",
177	TerminalCreate                                => "sky://terminal/create",
178	TerminalData                                  => "sky://terminal/data",
179	TerminalExit                                  => "sky://terminal/exit",
180	TerminalHide                                  => "sky://terminal/hide",
181	TerminalOpened                                => "sky://terminal/opened",
182	TerminalProcessId                             => "sky://terminal/processId",
183	TerminalResize                                => "sky://terminal/resize",
184	TerminalShow                                  => "sky://terminal/show",
185
186	// --- Test ---
187	TestRegistered                                => "sky://test/registered",
188	TestRunStarted                                => "sky://test/run-started",
189	TestRunStatusChanged                          => "sky://test/run-status-changed",
190
191	// --- Theme ---
192	ThemeChange                                   => "sky://theme/change",
193
194	// --- Tree view ---
195	// Canonical prefix is `sky://tree-view/` (kebab-case). The earlier
196	// `sky://treeView/register` camelCase channel was a parallel emission
197	// from `CocoonService/TreeView.rs`; it has been collapsed into
198	// `TreeViewCreate`, which every handler already subscribes to.
199	TreeViewCreate                                => "sky://tree-view/create",
200	TreeViewDispose                               => "sky://tree-view/dispose",
201	TreeViewNodeExpanded                          => "sky://tree-view/node-expanded",
202	TreeViewRefresh                               => "sky://tree-view/refresh",
203	TreeViewRestoreState                          => "sky://tree-view/restore-state",
204	TreeViewReveal                                => "sky://tree-view/reveal",
205	TreeViewSelectionChanged                      => "sky://tree-view/selection-changed",
206	TreeViewSetBadge                              => "sky://tree-view/set-badge",
207	TreeViewSetMessage                            => "sky://tree-view/set-message",
208	TreeViewSetTitle                              => "sky://tree-view/set-title",
209
210	// --- UI ---
211	// `UIShow{InputBox,QuickPick}Request` are deprecated aliases. The
212	// Sky listener channels are `InputBoxShow` and `QuickPickShow`
213	// declared earlier in this enum. `UserInterfaceProvider.rs` now
214	// references those directly so the `UIShow*Request` channel names
215	// below remain reachable only from older code paths and tests.
216	UIShowInputBoxRequest                         => "sky://ui/show-input-box-request",
217	UIShowMessageRequest                          => "sky://ui/show-message-request",
218	UIShowQuickPickRequest                        => "sky://ui/show-quick-pick-request",
219
220	// --- Virtual file system ---
221	VFSFileChange                                 => "sky://vfs/fileChange",
222
223	// --- Webview ---
224	// Canonical form is kebab-case (`sky://webview/post-message`,
225	// `sky://webview/set-html`). The `…CamelCase` aliases existed because
226	// mod.rs emitted `sky://webview/postMessage` / `sky://webview/setHtml`
227	// inline; those emit sites have been migrated to the enum so Sky only
228	// ever sees the kebab-case form.
229	WebviewCreate                                 => "sky://webview/create",
230	WebviewCreated                                => "sky://webview/created",
231	WebviewDispose                                => "sky://webview/dispose",
232	WebviewDisposed                               => "sky://webview/disposed",
233	WebviewMessage                                => "sky://webview/message",
234	WebviewOptionsChanged                         => "sky://webview/options-changed",
235	WebviewPostMessage                            => "sky://webview/post-message",
236	WebviewRevealed                               => "sky://webview/revealed",
237	WebviewSetHTML                                => "sky://webview/set-html",
238
239	// --- Window ---
240	WindowShowTextDocument                        => "sky://window/showTextDocument",
241
242	// --- Workspace ---
243	WorkspaceApplyEdit                            => "sky://workspace/applyEdit",
244	WorkspacesChanged                             => "sky://workspaces/changed",
245}
246
247#[cfg(test)]
248mod Tests {
249	use std::str::FromStr;
250
251	use super::SkyEvent;
252
253	#[test]
254	fn RoundTrip() {
255		for Variant in SkyEvent::All() {
256			let Wire = Variant.AsStr();
257			let Parsed = SkyEvent::from_str(Wire).expect("round-trip");
258			assert_eq!(*Variant, Parsed, "{} failed round-trip", Wire);
259		}
260	}
261
262	#[test]
263	fn EveryWireStartsWithSkyScheme() {
264		for Variant in SkyEvent::All() {
265			assert!(
266				Variant.AsStr().starts_with("sky://"),
267				"{} does not use the sky:// scheme",
268				Variant.AsStr()
269			);
270		}
271	}
272
273	#[test]
274	fn RejectsUnknown() {
275		assert!(SkyEvent::from_str("mountain://nope").is_err());
276		assert!(SkyEvent::from_str("").is_err());
277	}
278
279	/// Guards against drift between this Rust enum and its TS mirror at
280	/// `Element/Wind/Source/IPC/SkyEvent.ts`. Both files are hand-edited,
281	/// so the test scrapes the TS literal array and asserts every wire
282	/// string here exists there, and vice versa. If this fails the two
283	/// tables disagree - add or remove from whichever side is missing.
284	#[test]
285	fn RustAndTypeScriptTablesAgree() {
286		use std::{collections::HashSet, path::PathBuf};
287
288		let TsPath = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../Wind/Source/IPC/SkyEvent.ts");
289		let Source = match std::fs::read_to_string(&TsPath) {
290			Ok(S) => S,
291			// In packaging contexts where Wind isn't checked out alongside
292			// Common we skip the cross-check silently rather than
293			// failing - the RoundTrip / UniqueWireStrings guards above
294			// still cover the Rust side on its own.
295			Err(_) => return,
296		};
297
298		let mut TsWires:HashSet<String> = HashSet::new();
299		for Line in Source.lines() {
300			if let Some(Start) = Line.find("\"sky://") {
301				let Tail = &Line[Start + 1..];
302				if let Some(End) = Tail.find('"') {
303					TsWires.insert(Tail[..End].to_string());
304				}
305			}
306		}
307
308		let RsWires:HashSet<String> = SkyEvent::All().iter().map(|V| V.AsStr().to_string()).collect();
309
310		let OnlyInRust:Vec<_> = RsWires.difference(&TsWires).collect();
311		let OnlyInTs:Vec<_> = TsWires.difference(&RsWires).collect();
312
313		assert!(
314			OnlyInRust.is_empty() && OnlyInTs.is_empty(),
315			"SkyEvent drift between Rust and TS:\n  only in Rust: {:?}\n  only in TS:   {:?}",
316			OnlyInRust,
317			OnlyInTs
318		);
319	}
320
321	#[test]
322	fn UniqueWireStrings() {
323		let mut Seen = std::collections::HashSet::new();
324		for Variant in SkyEvent::All() {
325			assert!(Seen.insert(Variant.AsStr()), "duplicate wire: {}", Variant.AsStr());
326		}
327	}
328}