wasmcloud_actor_core/
generated.rs

1extern crate rmp_serde as rmps;
2use rmps::{Deserializer, Serializer};
3use serde::{Deserialize, Serialize};
4use std::io::Cursor;
5
6#[cfg(feature = "guest")]
7extern crate wapc_guest as guest;
8#[cfg(feature = "guest")]
9use guest::prelude::*;
10
11#[cfg(feature = "guest")]
12pub struct Handlers {}
13
14#[cfg(feature = "guest")]
15impl Handlers {
16    /// This operation is invoked by the host runtime to determine the health of an
17    /// actor
18    pub fn register_health_request(
19        f: fn(HealthCheckRequest) -> HandlerResult<HealthCheckResponse>,
20    ) {
21        *HEALTH_REQUEST.write().unwrap() = Some(f);
22        register_function(&"HealthRequest", health_request_wrapper);
23    }
24}
25
26#[cfg(feature = "guest")]
27lazy_static::lazy_static! {
28static ref HEALTH_REQUEST: std::sync::RwLock<Option<fn(HealthCheckRequest) -> HandlerResult<HealthCheckResponse>>> = std::sync::RwLock::new(None);
29}
30
31#[cfg(feature = "guest")]
32fn health_request_wrapper(input_payload: &[u8]) -> CallResult {
33    let input = deserialize::<HealthCheckRequest>(input_payload)?;
34    let lock = HEALTH_REQUEST.read().unwrap().unwrap();
35    let result = lock(input)?;
36    serialize(result)
37}
38
39/// Represents the data sent to a capability provider at link time
40#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
41pub struct CapabilityConfiguration {
42    /// The module name
43    #[serde(rename = "module")]
44    pub module: String,
45    /// A map of values that represent the configuration values
46    #[serde(rename = "values")]
47    pub values: std::collections::HashMap<String, String>,
48}
49
50/// A request sent to the actor by the host in order to determine health status
51#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
52pub struct HealthCheckRequest {
53    /// Since we cannot currently serialize empty requests, this placeholder is required
54    #[serde(rename = "placeholder")]
55    pub placeholder: bool,
56}
57
58/// All actors must return a health check response to the host upon receipt of a
59/// health request. Returning in `Err` indicates total actor failure, while
60/// returning a valid response with the `healthy` flag set to false indicates that
61/// the actor has somehow detected that it cannot perform its given task
62#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
63pub struct HealthCheckResponse {
64    /// A flag that indicates the the actor is healthy
65    #[serde(rename = "healthy")]
66    pub healthy: bool,
67    /// A message containing additional information about the actors health
68    #[serde(rename = "message")]
69    pub message: String,
70}
71
72/// The standard function for serializing codec structs into a format that can be
73/// used for message exchange between actor and host. Use of any other function to
74/// serialize could result in breaking incompatibilities.
75pub fn serialize<T>(
76    item: T,
77) -> ::std::result::Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>
78where
79    T: Serialize,
80{
81    let mut buf = Vec::new();
82    item.serialize(&mut Serializer::new(&mut buf).with_struct_map())?;
83    Ok(buf)
84}
85
86/// The standard function for de-serializing codec structs from a format suitable
87/// for message exchange between actor and host. Use of any other function to
88/// deserialize could result in breaking incompatibilities.
89pub fn deserialize<'de, T: Deserialize<'de>>(
90    buf: &[u8],
91) -> ::std::result::Result<T, Box<dyn std::error::Error + Send + Sync>> {
92    let mut de = Deserializer::new(Cursor::new(buf));
93    match Deserialize::deserialize(&mut de) {
94        Ok(t) => Ok(t),
95        Err(e) => Err(format!("Failed to de-serialize: {}", e).into()),
96    }
97}