wasmflow_component/
signals.rs

1use std::str::FromStr;
2
3#[allow(missing_debug_implementations, missing_copy_implementations)]
4#[must_use]
5/// The [OutputSignal] enum is a way of combining port output and messaging signal into one message. Used for WASM modules to reduce the number of calls between the host and guest.
6pub enum OutputSignal {
7  /// A single output.
8  Output,
9  /// An output and a done signal.
10  OutputDone,
11  /// A done signal.
12  Done,
13}
14
15impl OutputSignal {
16  #[must_use]
17  #[doc(hidden)]
18  pub fn as_str(&self) -> &'static str {
19    match self {
20      OutputSignal::Output => "1",
21      OutputSignal::OutputDone => "2",
22      OutputSignal::Done => "3",
23    }
24  }
25}
26
27impl FromStr for OutputSignal {
28  type Err = ();
29
30  fn from_str(s: &str) -> Result<Self, Self::Err> {
31    let result = match s {
32      "1" => OutputSignal::Output,
33      "2" => OutputSignal::OutputDone,
34      "3" => OutputSignal::Done,
35      _ => return Err(()),
36    };
37    Ok(result)
38  }
39}
40
41#[allow(missing_debug_implementations, missing_copy_implementations)]
42#[must_use]
43/// The [HostCommand] enum tells the host what to do for the host call.
44pub enum HostCommand {
45  /// Port output.
46  Output,
47  /// Make a call to a linked entity.
48  LinkCall,
49  /// Logging output.
50  Log,
51}
52
53impl HostCommand {
54  #[must_use]
55  #[doc(hidden)]
56  pub fn as_str(&self) -> &'static str {
57    match self {
58      HostCommand::Output => "0",
59      HostCommand::LinkCall => "1",
60      HostCommand::Log => "2",
61    }
62  }
63}
64
65impl FromStr for HostCommand {
66  type Err = ();
67
68  fn from_str(s: &str) -> Result<Self, Self::Err> {
69    let result = match s {
70      "0" => HostCommand::Output,
71      "1" => HostCommand::LinkCall,
72      "2" => HostCommand::Log,
73      _ => return Err(()),
74    };
75    Ok(result)
76  }
77}
78
79#[allow(missing_debug_implementations, missing_copy_implementations)]
80#[must_use]
81/// The [LogLevel] enum defines log levels for WASM modules to log appropriately to hosts.
82pub enum LogLevel {
83  /// Information-related messages
84  Info,
85  /// Error output
86  Error,
87  /// Non-fatal warnings
88  Warn,
89  /// Debug messages
90  Debug,
91  /// Trace-level messages
92  Trace,
93  /// Performance mark messages
94  Mark,
95}
96
97impl LogLevel {
98  #[must_use]
99  #[doc(hidden)]
100  pub fn as_str(&self) -> &'static str {
101    match self {
102      LogLevel::Info => "0",
103      LogLevel::Error => "1",
104      LogLevel::Warn => "2",
105      LogLevel::Debug => "3",
106      LogLevel::Trace => "4",
107      LogLevel::Mark => "5",
108    }
109  }
110}
111
112impl FromStr for LogLevel {
113  type Err = ();
114
115  fn from_str(s: &str) -> Result<Self, Self::Err> {
116    let result = match s {
117      "0" => LogLevel::Info,
118      "1" => LogLevel::Error,
119      "2" => LogLevel::Warn,
120      "3" => LogLevel::Debug,
121      "4" => LogLevel::Trace,
122      "5" => LogLevel::Mark,
123      _ => return Err(()),
124    };
125    Ok(result)
126  }
127}