wascc_actor/
lib.rs

1#![doc(html_logo_url = "https://avatars2.githubusercontent.com/u/52050279?s=200&v=4")]
2
3//! # wascc-actor
4//!
5//! The `wascc-actor` library provides WebAssembly module developers with access to the
6//! wascc host runtime. Each actor module has a single receive function, declared with the
7//!  `actor_receive!` macro. Inside this receive function, the actor
8//! module should check the operation of the delivered message and handle it accordingly,
9//! returning any binary payload in response. It is the responsibility of the actor module to ensure
10//! that the capability provider will be able to understand whichever messages it sends.
11//!
12//! # Example
13//! ```
14//! extern crate wascc_actor as actor;
15//!
16//! use actor::prelude::*;
17//!
18//! actor_handlers!{
19//!    codec::http::OP_HANDLE_REQUEST => hello_world,
20//!    codec::core::OP_HEALTH_REQUEST => health
21//! }
22//!
23//! pub fn hello_world(_req: codec::http::Request) -> HandlerResult<codec::http::Response> {
24//!   Ok(codec::http::Response::ok())
25//! }
26//!
27//! pub fn health(_req: codec::core::HealthRequest) -> HandlerResult<()> {
28//!   Ok(())
29//! }
30//! ```
31
32#[macro_use]
33extern crate lazy_static;
34
35pub type HandlerResult<T> = ::std::result::Result<T, Box<dyn std::error::Error + Sync + Send>>;
36
37pub extern crate wapc_guest as wapc;
38
39use wapc_guest::console_log;
40
41/// Actor developers will use this macro to set up their operation handlers
42#[macro_export]
43macro_rules! actor_handlers(
44    { $($key:path => $user_handler:ident),* } => {
45        use $crate::wapc::prelude::*;
46
47        wapc_handler!(handle_wapc);
48        fn handle_wapc(operation: &str, msg: &[u8]) -> CallResult {
49            $crate::logger::ensure_logger();
50            match operation {
51                $( $key => $user_handler(deserialize(msg)?)
52                            .and_then(|r| serialize(r))
53                            .map_err(|e| e.into()), )*
54                _ => Err("bad dispatch".into())
55            }
56        }
57
58     };
59);
60
61/// Use this function for simple, unstructured logging outside the usual log macros
62pub fn println(msg: &str) {
63    console_log(msg)
64}
65
66pub mod errors;
67pub mod events;
68pub mod extras;
69pub mod http_client;
70pub mod keyvalue;
71pub mod logger;
72pub mod messaging;
73pub mod objectstore;
74pub mod prelude;
75pub mod untyped;