wasmcloud_interface_factorial/
factorial.rs#[allow(unused_imports)]
use async_trait::async_trait;
#[allow(unused_imports)]
use serde::{Deserialize, Serialize};
#[allow(unused_imports)]
use std::{borrow::Borrow, borrow::Cow, io::Write, string::ToString};
#[allow(unused_imports)]
use wasmbus_rpc::{
cbor::*,
common::{
deserialize, message_format, serialize, Context, Message, MessageDispatch, MessageFormat,
SendOpts, Transport,
},
error::{RpcError, RpcResult},
Timestamp,
};
#[allow(dead_code)]
pub const SMITHY_VERSION: &str = "1.0";
#[async_trait]
pub trait Factorial {
fn contract_id() -> &'static str {
"wasmcloud:example:factorial"
}
async fn calculate(&self, ctx: &Context, arg: &u32) -> RpcResult<u64>;
}
#[doc(hidden)]
#[async_trait]
pub trait FactorialReceiver: MessageDispatch + Factorial {
async fn dispatch(&self, ctx: &Context, message: Message<'_>) -> Result<Vec<u8>, RpcError> {
match message.method {
"Calculate" => {
let value: u32 = wasmbus_rpc::common::deserialize(&message.arg)
.map_err(|e| RpcError::Deser(format!("'U32': {}", e)))?;
let resp = Factorial::calculate(self, ctx, &value).await?;
let buf = wasmbus_rpc::common::serialize(&resp)?;
Ok(buf)
}
_ => Err(RpcError::MethodNotHandled(format!(
"Factorial::{}",
message.method
))),
}
}
}
#[derive(Clone, Debug)]
pub struct FactorialSender<T: Transport> {
transport: T,
}
impl<T: Transport> FactorialSender<T> {
pub fn via(transport: T) -> Self {
Self { transport }
}
pub fn set_timeout(&self, interval: std::time::Duration) {
self.transport.set_timeout(interval);
}
}
#[cfg(not(target_arch = "wasm32"))]
impl<'send> FactorialSender<wasmbus_rpc::provider::ProviderTransport<'send>> {
pub fn for_actor(ld: &'send wasmbus_rpc::core::LinkDefinition) -> Self {
Self {
transport: wasmbus_rpc::provider::ProviderTransport::new(ld, None),
}
}
}
#[cfg(target_arch = "wasm32")]
impl FactorialSender<wasmbus_rpc::actor::prelude::WasmHost> {
pub fn to_actor(actor_id: &str) -> Self {
let transport =
wasmbus_rpc::actor::prelude::WasmHost::to_actor(actor_id.to_string()).unwrap();
Self { transport }
}
}
#[cfg(target_arch = "wasm32")]
impl FactorialSender<wasmbus_rpc::actor::prelude::WasmHost> {
pub fn new() -> Self {
let transport = wasmbus_rpc::actor::prelude::WasmHost::to_provider(
"wasmcloud:example:factorial",
"default",
)
.unwrap();
Self { transport }
}
pub fn new_with_link(link_name: &str) -> wasmbus_rpc::error::RpcResult<Self> {
let transport = wasmbus_rpc::actor::prelude::WasmHost::to_provider(
"wasmcloud:example:factorial",
link_name,
)?;
Ok(Self { transport })
}
}
#[async_trait]
impl<T: Transport + std::marker::Sync + std::marker::Send> Factorial for FactorialSender<T> {
#[allow(unused)]
async fn calculate(&self, ctx: &Context, arg: &u32) -> RpcResult<u64> {
let buf = wasmbus_rpc::common::serialize(arg)?;
let resp = self
.transport
.send(
ctx,
Message {
method: "Factorial.Calculate",
arg: Cow::Borrowed(&buf),
},
None,
)
.await?;
let value: u64 = wasmbus_rpc::common::deserialize(&resp)
.map_err(|e| RpcError::Deser(format!("'{}': U64", e)))?;
Ok(value)
}
}