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
#![doc(html_logo_url = "https://avatars2.githubusercontent.com/u/52050279?s=200&v=4")]

//! # wascc-actor
//!
//! The `wascc-actor` library provides WebAssembly module developers with access to the
//! wascc host runtime. Each actor module has a single receive function, declared with the
//!  `actor_receive!` macro. Inside this receive function, the actor
//! module should check the operation of the delivered message and handle it accordingly,
//! returning any binary payload in response. It is the responsibility of the actor module to ensure
//! that the capability provider will be able to understand whichever messages it sends.
//!
//! # Example
//! ```
//! extern crate wascc_actor as actor;
//!
//! use actor::prelude::*;
//!
//! actor_handlers!{
//!    codec::http::OP_HANDLE_REQUEST => hello_world,
//!    codec::core::OP_HEALTH_REQUEST => health
//! }
//!
//! pub fn hello_world(_req: codec::http::Request) -> HandlerResult<codec::http::Response> {
//!   Ok(codec::http::Response::ok())
//! }
//!
//! pub fn health(_req: codec::core::HealthRequest) -> HandlerResult<()> {
//!   Ok(())
//! }
//! ```

#[macro_use]
extern crate lazy_static;

pub type HandlerResult<T> = ::std::result::Result<T, Box<dyn std::error::Error + Sync + Send>>;

pub extern crate wapc_guest as wapc;

use wapc_guest::console_log;

/// Actor developers will use this macro to set up their operation handlers
#[macro_export]
macro_rules! actor_handlers(
    { $($key:path => $user_handler:ident),* } => {
        use $crate::wapc::prelude::*;

        wapc_handler!(handle_wapc);
        fn handle_wapc(operation: &str, msg: &[u8]) -> CallResult {
            $crate::logger::ensure_logger();
            match operation {
                $( $key => $user_handler(deserialize(msg)?)
                            .and_then(|r| serialize(r))
                            .map_err(|e| e.into()), )*
                _ => Err("bad dispatch".into())
            }
        }

     };
);

/// Use this function for simple, unstructured logging outside the usual log macros
pub fn println(msg: &str) {
    console_log(msg)
}

pub mod errors;
pub mod events;
pub mod extras;
pub mod http_client;
pub mod keyvalue;
pub mod logger;
pub mod messaging;
pub mod objectstore;
pub mod prelude;
pub mod untyped;