1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use std::str::FromStr;

use strum::EnumIter;
pub use strum::IntoEnumIterator;

/// Functions called by guest, exported by host
pub mod host_exports {
  /// The wasmRS protocol function `__init_buffers`
  pub const INIT: &str = "__init_buffers";
  /// The wasmRS protocol function `__send`
  pub const SEND: &str = "__send";
  /// The wasmRS protocol function `__console_log`
  pub const LOG: &str = "__console_log";
  /// The wasmRS protocol function `__op_list`
  pub const OP_LIST: &str = "__op_list";
}

/// The exported host functions as an enum.
#[derive(Debug, Copy, Clone, EnumIter)]
#[allow(missing_docs)]
pub enum HostExports {
  Init,
  Send,
  Log,
  OpList,
}

impl FromStr for HostExports {
  type Err = ();

  fn from_str(s: &str) -> Result<Self, Self::Err> {
    let result = match s {
      host_exports::INIT => Self::Init,
      host_exports::SEND => Self::Send,
      host_exports::LOG => Self::Log,
      host_exports::OP_LIST => Self::OpList,
      _ => return Err(()),
    };
    Ok(result)
  }
}

impl AsRef<str> for HostExports {
  fn as_ref(&self) -> &str {
    match self {
      Self::Init => host_exports::INIT,
      Self::Send => host_exports::SEND,
      Self::Log => host_exports::LOG,
      Self::OpList => host_exports::OP_LIST,
    }
  }
}

impl std::fmt::Display for HostExports {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.write_str(self.as_ref())
  }
}

/// Functions called by host, exported by guest
pub mod guest_exports {
  /// The wasmRS protocol function `__wasmrs_init`
  pub const INIT: &str = "__wasmrs_init";

  /// The wasmRS protocol function `__wasmrs_send`
  pub const SEND: &str = "__wasmrs_send";

  /// The wasmRS protocol function `__wasmrs_op_list_request`
  pub const OP_LIST_REQUEST: &str = "__wasmrs_op_list_request";

  /// The wasmRS protocol function `_start`
  pub const TINYGO_START: &str = "_start";

  /// Start functions to attempt to call - order is important
  pub const REQUIRED_STARTS: [&str; 2] = [TINYGO_START, INIT];
}

/// The exported guest functions as an enum.
#[derive(Debug, Copy, Clone, EnumIter)]
#[allow(missing_docs)]
pub enum GuestExports {
  Init,
  Start,
  OpListRequest,
  Send,
}

impl FromStr for GuestExports {
  type Err = ();

  fn from_str(s: &str) -> Result<Self, Self::Err> {
    let result = match s {
      guest_exports::INIT => Self::Init,
      guest_exports::TINYGO_START => Self::Start,
      guest_exports::OP_LIST_REQUEST => Self::OpListRequest,
      guest_exports::SEND => Self::Send,
      _ => return Err(()),
    };
    Ok(result)
  }
}

impl AsRef<str> for GuestExports {
  fn as_ref(&self) -> &str {
    match self {
      GuestExports::Init => guest_exports::INIT,
      GuestExports::Start => guest_exports::TINYGO_START,
      GuestExports::Send => guest_exports::SEND,
      GuestExports::OpListRequest => guest_exports::OP_LIST_REQUEST,
    }
  }
}

impl std::fmt::Display for GuestExports {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.write_str(self.as_ref())
  }
}