wasmrs_guest/
lib.rs

1#![deny(
2  clippy::expect_used,
3  clippy::explicit_deref_methods,
4  clippy::option_if_let_else,
5  clippy::await_holding_lock,
6  clippy::cloned_instead_of_copied,
7  clippy::explicit_into_iter_loop,
8  clippy::flat_map_option,
9  clippy::fn_params_excessive_bools,
10  clippy::implicit_clone,
11  clippy::inefficient_to_string,
12  clippy::large_types_passed_by_value,
13  clippy::manual_ok_or,
14  clippy::map_flatten,
15  clippy::map_unwrap_or,
16  clippy::must_use_candidate,
17  clippy::needless_for_each,
18  clippy::needless_pass_by_value,
19  clippy::option_option,
20  clippy::redundant_else,
21  clippy::semicolon_if_nothing_returned,
22  clippy::too_many_lines,
23  clippy::trivially_copy_pass_by_ref,
24  clippy::unnested_or_patterns,
25  clippy::future_not_send,
26  clippy::useless_let_if_seq,
27  clippy::str_to_string,
28  clippy::inherent_to_string,
29  clippy::let_and_return,
30  clippy::string_to_string,
31  clippy::try_err,
32  clippy::unused_async,
33  clippy::missing_enforced_import_renames,
34  clippy::nonstandard_macro_braces,
35  clippy::rc_mutex,
36  clippy::unwrap_or_else_default,
37  clippy::manual_split_once,
38  clippy::derivable_impls,
39  clippy::needless_option_as_deref,
40  clippy::iter_not_returning_iterator,
41  clippy::same_name_method,
42  clippy::manual_assert,
43  clippy::non_send_fields_in_send_ty,
44  clippy::equatable_if_let,
45  bad_style,
46  clashing_extern_declarations,
47  dead_code,
48  deprecated,
49  explicit_outlives_requirements,
50  improper_ctypes,
51  invalid_value,
52  missing_copy_implementations,
53  missing_debug_implementations,
54  mutable_transmutes,
55  no_mangle_generic_items,
56  non_shorthand_field_patterns,
57  overflowing_literals,
58  path_statements,
59  patterns_in_fns_without_body,
60  private_in_public,
61  trivial_bounds,
62  trivial_casts,
63  trivial_numeric_casts,
64  type_alias_bounds,
65  unconditional_recursion,
66  unreachable_pub,
67  unsafe_code,
68  unstable_features,
69  unused,
70  unused_allocation,
71  unused_comparisons,
72  unused_import_braces,
73  unused_parens,
74  unused_qualifications,
75  while_true,
76  missing_docs
77)]
78#![doc = include_str!("../README.md")]
79
80mod guest;
81pub use error::Error;
82pub use futures_util::{FutureExt, Stream};
83
84pub use guest::*;
85pub use wasmrs_runtime as runtime;
86pub use wasmrs_rx::{Flux, FluxChannel, FluxReceiver, Mono, Observable, Observer};
87
88mod exports;
89mod imports;
90mod server;
91
92/// The wasmRS-guest error module.
93pub mod error;
94
95pub use serde_json::Value;
96pub use wasmrs::Payload;
97pub use wasmrs_rx::{BoxFlux, BoxMono};
98
99/// Deserialize a generic [Value] from CBOR bytes.
100pub fn deserialize_generic(buf: &[u8]) -> Result<std::collections::BTreeMap<String, Value>, Error> {
101  deserialize(buf).map_err(|e| Error::Codec(e.to_string()))
102}
103
104cfg_if::cfg_if!(
105  if #[cfg(all(feature = "logging", target_os = "wasi"))] {
106    /// Turn on logging for the guest (WASI only).
107    pub fn init_logging() {
108      env_logger::builder()
109        .filter_level(log::LevelFilter::Trace)
110        .parse_env("wasmrs")
111        .init();
112    }
113  } else {
114    /// Turn on logging for the guest (WASI only).
115    pub fn init_logging() {}
116  }
117);
118
119#[cfg(test)]
120mod test {
121
122  use super::*;
123  #[test]
124  fn test_basic() {
125    #[derive(serde::Deserialize, serde::Serialize, Debug, PartialEq)]
126    struct Input {
127      input: String,
128      num: u32,
129    }
130    let input = Input {
131      input: "HELLO WORLD".to_owned(),
132      num: 32,
133    };
134    let bytes = serialize(&input).unwrap();
135    let input2: Input = deserialize(&bytes).unwrap();
136    assert_eq!(input.input, input2.input);
137    assert_eq!(input.num, input2.num);
138    println!("{:?}", bytes);
139    let map: Value = deserialize(&bytes).unwrap();
140    println!("{:?}", map);
141    if let Value::Object(map) = map {
142      assert_eq!(map.get("input"), Some(&Value::String("HELLO WORLD".to_owned())));
143    } else {
144      panic!("expected map");
145    }
146  }
147}