wasmcloud-interface-timing 0.1.2

Interface for actors to perform time related operations
Documentation
// This file is @generated by wasmcloud/weld-codegen 0.7.0.
// It is not intended for manual editing.
// namespace: org.wasmcloud.interface.timing

#[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";

/// Provides the capability to read the system time.
/// wasmbus.contractId: wasmcloud:timing
/// wasmbus.providerReceive
#[async_trait]
pub trait Timing {
    /// returns the capability contract id for this interface
    fn contract_id() -> &'static str {
        "wasmcloud:timing"
    }
    /// Returns the current time as a `wasmbus_rpc::Timestamp` struct.
    ///
    /// The returned timestamp has nanosecond precision, so care should be taken
    /// to avoid timing attacks. If the timestamp will be made visible to users,
    /// it's recommended to reduce the precision by truncating or removing the
    /// `nsec` field.
    /// ```ignore
    /// let timing = TimingSender::new();
    /// let now = timing.now(ctx).await?;
    async fn now(&self, ctx: &Context) -> RpcResult<Timestamp>;
}

/// TimingReceiver receives messages defined in the Timing service trait
/// Provides the capability to read the system time.
#[doc(hidden)]
#[async_trait]
pub trait TimingReceiver: MessageDispatch + Timing {
    async fn dispatch(&self, ctx: &Context, message: Message<'_>) -> Result<Vec<u8>, RpcError> {
        match message.method {
            "Now" => {
                let resp = Timing::now(self, ctx).await?;
                let buf = wasmbus_rpc::common::serialize(&resp)?;

                Ok(buf)
            }
            _ => Err(RpcError::MethodNotHandled(format!(
                "Timing::{}",
                message.method
            ))),
        }
    }
}

/// TimingSender sends messages to a Timing service
/// Provides the capability to read the system time.
/// client for sending Timing messages
#[derive(Clone, Debug)]
pub struct TimingSender<T: Transport> {
    transport: T,
}

impl<T: Transport> TimingSender<T> {
    /// Constructs a TimingSender with the specified transport
    pub fn via(transport: T) -> Self {
        Self { transport }
    }

    pub fn set_timeout(&self, interval: std::time::Duration) {
        self.transport.set_timeout(interval);
    }
}

#[cfg(target_arch = "wasm32")]
impl TimingSender<wasmbus_rpc::actor::prelude::WasmHost> {
    /// Constructs a client for sending to a Timing provider
    /// implementing the 'wasmcloud:timing' capability contract, with the "default" link
    pub fn new() -> Self {
        let transport =
            wasmbus_rpc::actor::prelude::WasmHost::to_provider("wasmcloud:timing", "default")
                .unwrap();
        Self { transport }
    }

    /// Constructs a client for sending to a Timing provider
    /// implementing the 'wasmcloud:timing' capability contract, with the specified link name
    pub fn new_with_link(link_name: &str) -> wasmbus_rpc::error::RpcResult<Self> {
        let transport =
            wasmbus_rpc::actor::prelude::WasmHost::to_provider("wasmcloud:timing", link_name)?;
        Ok(Self { transport })
    }
}
#[async_trait]
impl<T: Transport + std::marker::Sync + std::marker::Send> Timing for TimingSender<T> {
    #[allow(unused)]
    /// Returns the current time as a `wasmbus_rpc::Timestamp` struct.
    ///
    /// The returned timestamp has nanosecond precision, so care should be taken
    /// to avoid timing attacks. If the timestamp will be made visible to users,
    /// it's recommended to reduce the precision by truncating or removing the
    /// `nsec` field.
    /// ```ignore
    /// let timing = TimingSender::new();
    /// let now = timing.now(ctx).await?;
    async fn now(&self, ctx: &Context) -> RpcResult<Timestamp> {
        let buf = *b"";
        let resp = self
            .transport
            .send(
                ctx,
                Message {
                    method: "Timing.Now",
                    arg: Cow::Borrowed(&buf),
                },
                None,
            )
            .await?;

        let value: Timestamp = wasmbus_rpc::common::deserialize(&resp)
            .map_err(|e| RpcError::Deser(format!("'{}': Timestamp", e)))?;
        Ok(value)
    }
}