wasmcloud_interface_sleepy/
sleepy.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// This file is @generated by wasmcloud/weld-codegen 0.7.0.
// It is not intended for manual editing.
// namespace: jclmnop.provider.sleepy

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

/// Allows actors to `sleep` for a specified duration, or until a desired time.
/// contractId: "jclmnop:sleepy"
/// wasmbus.contractId: jclmnop:sleepy
/// wasmbus.providerReceive
#[async_trait]
pub trait Sleepy {
    /// returns the capability contract id for this interface
    fn contract_id() -> &'static str {
        "jclmnop:sleepy"
    }
    /// Sleep for a specified number of milliseconds
    /// ```ignore
    /// let sleepy = SleepySender::new();
    /// // sleep for 5 seconds
    /// sleepy.sleep(ctx, &5000).await?;
    async fn sleep(&self, ctx: &Context, arg: &u32) -> RpcResult<()>;
    /// Sleep until a specified time, provided as a `wasmbus_rpc::Timestamp` struct.
    /// If the specified time is in the past, the operation will return immediately.
    /// ```ignore
    /// let sleepy = SleepySender::new();
    /// let now = sleepy.now(ctx).await?;
    /// let five_seconds = Timestamp::new(now.sec + 5, now.nsec);
    /// // sleep until 5 seconds from now
    /// sleepy.sleep_until(ctx, &five_seconds).await
    async fn sleep_until(&self, ctx: &Context, arg: &Timestamp) -> RpcResult<()>;
    /// Returns the current time as a `wasmbus_rpc::Timestamp` struct.
    /// ```ignore
    /// let sleepy = SleepySender::new();
    /// let now = sleepy.now(ctx).await?;
    async fn now(&self, ctx: &Context) -> RpcResult<Timestamp>;
}

/// SleepyReceiver receives messages defined in the Sleepy service trait
/// Allows actors to `sleep` for a specified duration, or until a desired time.
/// contractId: "jclmnop:sleepy"
#[doc(hidden)]
#[async_trait]
pub trait SleepyReceiver: MessageDispatch + Sleepy {
    async fn dispatch(&self, ctx: &Context, message: Message<'_>) -> Result<Vec<u8>, RpcError> {
        match message.method {
            "Sleep" => {
                let value: u32 = wasmbus_rpc::common::deserialize(&message.arg)
                    .map_err(|e| RpcError::Deser(format!("'U32': {}", e)))?;

                let _resp = Sleepy::sleep(self, ctx, &value).await?;
                let buf = Vec::new();
                Ok(buf)
            }
            "SleepUntil" => {
                let value: Timestamp = wasmbus_rpc::common::deserialize(&message.arg)
                    .map_err(|e| RpcError::Deser(format!("'Timestamp': {}", e)))?;

                let _resp = Sleepy::sleep_until(self, ctx, &value).await?;
                let buf = Vec::new();
                Ok(buf)
            }
            "Now" => {
                let resp = Sleepy::now(self, ctx).await?;
                let buf = wasmbus_rpc::common::serialize(&resp)?;

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

/// SleepySender sends messages to a Sleepy service
/// Allows actors to `sleep` for a specified duration, or until a desired time.
/// contractId: "jclmnop:sleepy"
/// client for sending Sleepy messages
#[derive(Clone, Debug)]
pub struct SleepySender<T: Transport> {
    transport: T,
}

impl<T: Transport> SleepySender<T> {
    /// Constructs a SleepySender 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 SleepySender<wasmbus_rpc::actor::prelude::WasmHost> {
    /// Constructs a client for sending to a Sleepy provider
    /// implementing the 'jclmnop:sleepy' capability contract, with the "default" link
    pub fn new() -> Self {
        let transport =
            wasmbus_rpc::actor::prelude::WasmHost::to_provider("jclmnop:sleepy", "default")
                .unwrap();
        Self { transport }
    }

    /// Constructs a client for sending to a Sleepy provider
    /// implementing the 'jclmnop:sleepy' 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("jclmnop:sleepy", link_name)?;
        Ok(Self { transport })
    }
}
#[async_trait]
impl<T: Transport + std::marker::Sync + std::marker::Send> Sleepy for SleepySender<T> {
    #[allow(unused)]
    /// Sleep for a specified number of milliseconds
    /// ```ignore
    /// let sleepy = SleepySender::new();
    /// // sleep for 5 seconds
    /// sleepy.sleep(ctx, &5000).await?;
    async fn sleep(&self, ctx: &Context, arg: &u32) -> RpcResult<()> {
        let buf = wasmbus_rpc::common::serialize(arg)?;

        let resp = self
            .transport
            .send(
                ctx,
                Message {
                    method: "Sleepy.Sleep",
                    arg: Cow::Borrowed(&buf),
                },
                None,
            )
            .await?;
        Ok(())
    }
    #[allow(unused)]
    /// Sleep until a specified time, provided as a `wasmbus_rpc::Timestamp` struct.
    /// If the specified time is in the past, the operation will return immediately.
    /// ```ignore
    /// let sleepy = SleepySender::new();
    /// let now = sleepy.now(ctx).await?;
    /// let five_seconds = Timestamp::new(now.sec + 5, now.nsec);
    /// // sleep until 5 seconds from now
    /// sleepy.sleep_until(ctx, &five_seconds).await
    async fn sleep_until(&self, ctx: &Context, arg: &Timestamp) -> RpcResult<()> {
        let buf = wasmbus_rpc::common::serialize(arg)?;

        let resp = self
            .transport
            .send(
                ctx,
                Message {
                    method: "Sleepy.SleepUntil",
                    arg: Cow::Borrowed(&buf),
                },
                None,
            )
            .await?;
        Ok(())
    }
    #[allow(unused)]
    /// Returns the current time as a `wasmbus_rpc::Timestamp` struct.
    /// ```ignore
    /// let sleepy = SleepySender::new();
    /// let now = sleepy.now(ctx).await?;
    async fn now(&self, ctx: &Context) -> RpcResult<Timestamp> {
        let buf = *b"";
        let resp = self
            .transport
            .send(
                ctx,
                Message {
                    method: "Sleepy.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)
    }
}