wasmsh_protocol/lib.rs
1//! Message protocol for the wasmsh browser worker bridge.
2//!
3//! Defines versioned, serializable messages exchanged between the host
4//! page and the wasmsh Web Worker.
5//!
6//! ## Protocol version
7//! Current version: `0.1.0`
8
9#![warn(missing_docs)]
10
11/// Protocol version string.
12pub const PROTOCOL_VERSION: &str = "0.1.0";
13
14/// A command sent from the host to the worker.
15#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
16#[non_exhaustive]
17pub enum HostCommand {
18 /// Initialize the shell runtime with optional configuration.
19 Init {
20 /// Maximum step budget per execution (0 = unlimited).
21 step_budget: u64,
22 },
23 /// Execute a shell command string.
24 Run {
25 /// The shell source text to execute.
26 input: String,
27 },
28 /// Cancel the currently running execution.
29 Cancel,
30 /// Mount a virtual filesystem at the given path.
31 Mount {
32 /// Absolute path at which to mount the filesystem.
33 path: String,
34 },
35 /// Read a file from the virtual filesystem.
36 ReadFile {
37 /// Absolute path of the file to read.
38 path: String,
39 },
40 /// Write data to a file in the virtual filesystem.
41 WriteFile {
42 /// Absolute path of the file to write.
43 path: String,
44 /// Raw bytes to write into the file.
45 data: Vec<u8>,
46 },
47 /// List directory contents.
48 ListDir {
49 /// Absolute path of the directory to list.
50 path: String,
51 },
52}
53
54/// An event sent from the worker to the host.
55#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
56#[non_exhaustive]
57pub enum WorkerEvent {
58 /// Shell produced stdout output.
59 Stdout(Vec<u8>),
60 /// Shell produced stderr output.
61 Stderr(Vec<u8>),
62 /// Command execution finished with exit code.
63 Exit(i32),
64 /// A diagnostic message (warning, info, trace).
65 Diagnostic(DiagnosticLevel, String),
66 /// A file in the VFS was changed.
67 FsChanged(String),
68 /// Protocol version announcement (sent on Init).
69 Version(String),
70}
71
72/// Diagnostic severity level.
73#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
74#[non_exhaustive]
75pub enum DiagnosticLevel {
76 /// Informational message.
77 Info,
78 /// Non-fatal warning.
79 Warning,
80 /// Error-level diagnostic.
81 Error,
82 /// Low-level trace output.
83 Trace,
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn protocol_version() {
92 assert!(!PROTOCOL_VERSION.is_empty());
93 }
94
95 #[test]
96 fn host_command_variants() {
97 let cmd = HostCommand::Run {
98 input: "echo hello".into(),
99 };
100 assert!(matches!(cmd, HostCommand::Run { .. }));
101 }
102
103 #[test]
104 fn worker_event_variants() {
105 let evt = WorkerEvent::Exit(0);
106 assert_eq!(evt, WorkerEvent::Exit(0));
107
108 let evt = WorkerEvent::Diagnostic(DiagnosticLevel::Warning, "test".into());
109 assert!(matches!(
110 evt,
111 WorkerEvent::Diagnostic(DiagnosticLevel::Warning, _)
112 ));
113 }
114}