wasmcloud_advent_of_code_interface/
adventofcode.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
// This file is @generated by wasmcloud/weld-codegen 0.6.0.
// It is not intended for manual editing.
// namespace: org.example.interfaces.adventofcode

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

/// Description of AdventOfCode service
/// wasmbus.actorReceive
#[async_trait]
pub trait AdventOfCode {
    /// Used to retrive the solution for part one of an AdventOfCode solution
    async fn part_one(&self, ctx: &Context) -> RpcResult<String>;
    /// Used to retrive the solution for part two of an AdventOfCode solution
    async fn part_two(&self, ctx: &Context) -> RpcResult<String>;
}

/// AdventOfCodeReceiver receives messages defined in the AdventOfCode service trait
/// Description of AdventOfCode service
#[doc(hidden)]
#[async_trait]
pub trait AdventOfCodeReceiver: MessageDispatch + AdventOfCode {
    async fn dispatch(&self, ctx: &Context, message: Message<'_>) -> Result<Vec<u8>, RpcError> {
        match message.method {
            "PartOne" => {
                let resp = AdventOfCode::part_one(self, ctx).await?;
                let buf = wasmbus_rpc::common::serialize(&resp)?;

                Ok(buf)
            }
            "PartTwo" => {
                let resp = AdventOfCode::part_two(self, ctx).await?;
                let buf = wasmbus_rpc::common::serialize(&resp)?;

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

/// AdventOfCodeSender sends messages to a AdventOfCode service
/// Description of AdventOfCode service
/// client for sending AdventOfCode messages
#[derive(Debug)]
pub struct AdventOfCodeSender<T: Transport> {
    transport: T,
}

impl<T: Transport> AdventOfCodeSender<T> {
    /// Constructs a AdventOfCodeSender 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(not(target_arch = "wasm32"))]
impl<'send> AdventOfCodeSender<wasmbus_rpc::provider::ProviderTransport<'send>> {
    /// Constructs a Sender using an actor's LinkDefinition,
    /// Uses the provider's HostBridge for rpc
    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 AdventOfCodeSender<wasmbus_rpc::actor::prelude::WasmHost> {
    /// Constructs a client for actor-to-actor messaging
    /// using the recipient actor's public key
    pub fn to_actor(actor_id: &str) -> Self {
        let transport =
            wasmbus_rpc::actor::prelude::WasmHost::to_actor(actor_id.to_string()).unwrap();
        Self { transport }
    }
}
#[async_trait]
impl<T: Transport + std::marker::Sync + std::marker::Send> AdventOfCode for AdventOfCodeSender<T> {
    #[allow(unused)]
    /// Used to retrive the solution for part one of an AdventOfCode solution
    async fn part_one(&self, ctx: &Context) -> RpcResult<String> {
        let buf = *b"";
        let resp = self
            .transport
            .send(
                ctx,
                Message {
                    method: "AdventOfCode.PartOne",
                    arg: Cow::Borrowed(&buf),
                },
                None,
            )
            .await?;

        let value: String = wasmbus_rpc::common::deserialize(&resp)
            .map_err(|e| RpcError::Deser(format!("'{}': String", e)))?;
        Ok(value)
    }
    #[allow(unused)]
    /// Used to retrive the solution for part two of an AdventOfCode solution
    async fn part_two(&self, ctx: &Context) -> RpcResult<String> {
        let buf = *b"";
        let resp = self
            .transport
            .send(
                ctx,
                Message {
                    method: "AdventOfCode.PartTwo",
                    arg: Cow::Borrowed(&buf),
                },
                None,
            )
            .await?;

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