wascc_codec/
lib.rs

1#![doc(html_logo_url = "https://avatars2.githubusercontent.com/u/52050279?s=200&v=4")]
2
3//! # wascc-codec
4//!
5//! This library provides the core set of types and associated functions used for
6//! the common set of functionality between the host runtime and capability providers. Data
7//! types and models that need to be shared between guest and hosts should be _generated_
8//! and should not show up in this crate.
9
10/// The version of the codec as seen on crates.io
11pub const VERSION: &str = env!("CARGO_PKG_VERSION");
12/// The string used for the originator of messages dispatched by the host runtime
13pub const SYSTEM_ACTOR: &str = "system";
14
15#[macro_use]
16extern crate serde_derive;
17
18extern crate rmp_serde as rmps;
19use rmps::{Deserializer, Serializer};
20use serde::{Deserialize, Serialize};
21use std::io::Cursor;
22
23/// The standard function for serializing codec structs into a format that can be
24/// used for message exchange between actor and host. Use of any other function to
25/// serialize could result in breaking incompatibilities.
26pub fn serialize<T>(
27    item: T,
28) -> ::std::result::Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>
29where
30    T: Serialize,
31{
32    let mut buf = Vec::new();
33    item.serialize(&mut Serializer::new(&mut buf).with_struct_map())?;
34    Ok(buf)
35}
36
37/// The standard function for de-serializing codec structs from a format suitable
38/// for message exchange between actor and host. Use of any other function to
39/// deserialize could result in breaking incompatibilities.
40pub fn deserialize<'de, T: Deserialize<'de>>(
41    buf: &[u8],
42) -> ::std::result::Result<T, Box<dyn std::error::Error + Send + Sync>> {
43    let mut de = Deserializer::new(Cursor::new(buf));
44    match Deserialize::deserialize(&mut de) {
45        Ok(t) => Ok(t),
46        Err(e) => Err(format!("Failed to de-serialize: {}", e).into()),
47    }
48}
49
50pub mod capabilities;
51pub mod core;