wasmcloud_actor_core/lib.rs
1#![doc(html_logo_url = "https://avatars2.githubusercontent.com/u/52050279?s=200&v=4")]
2//! # Core wasmCloud Actor Interface
3//!
4//! This crate contains the data types required by all actors, namely the health check request and
5//! health check response, and [CapabilityConfiguration](struct.CapabilityConfiguration.html), a struct used
6//! by capability providers to receive link data for an actor.
7//!
8//! If you use the `init` macro, then a default health check handler will be created for you, as shown in
9//! this example. If you want to provide your own custom health check handler, then simply call
10//! `Handlers::register_health_check` with your handler function.
11//!
12//! # Example
13//! ```
14//! extern crate wasmcloud_actor_core as actor;
15//!
16//! #[actor::init]
17//! pub fn init() {
18//! // register handlers here
19//! }
20//! ```
21//!
22//! # Caveat
23//! Your `init` function is called by the wasmcloud host runtime when the actor is first loaded into the host. This function
24//! is only ever called once, and _is called before any provider linking takes place_. In other words, code written inside this
25//! function cannot communicate with capability providers.
26//!
27//! Also, in keeping with the notion of _stateless_ actors, avoid using this function to initialize or create global state.
28
29mod generated;
30pub use generated::*;
31
32#[cfg(feature = "guest")]
33use serde::{Deserialize, Serialize};
34
35#[cfg(feature = "guest")]
36/// Performs an actor-to-actor call, with the target actor identified by a reference string. This
37/// reference can be an OCI image URL, a 56-character public key (subject), or, if one is defined,
38/// a developer-friendly call alias
39pub fn call_actor<'de, T: Serialize, U: Deserialize<'de>>(
40 actor_ref: &str,
41 operation: &str,
42 msg: &T,
43) -> wapc_guest::HandlerResult<U> {
44 let res = wapc_guest::host_call("default", actor_ref, operation, &generated::serialize(msg)?)?;
45 let res = generated::deserialize(&res)?;
46 Ok(res)
47}
48
49impl HealthCheckResponse {
50 pub fn healthy() -> HealthCheckResponse {
51 HealthCheckResponse {
52 healthy: true,
53 message: "".to_string(),
54 }
55 }
56}
57
58#[cfg(feature = "guest")]
59pub use wasmcloud_actor_core_derive::init;