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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
#[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";
pub type F32Data = Vec<f32>;
#[doc(hidden)]
#[allow(unused_mut)]
pub fn encode_f32_data<W: wasmbus_rpc::cbor::Write>(
mut e: &mut wasmbus_rpc::cbor::Encoder<W>,
val: &F32Data,
) -> RpcResult<()>
where
<W as wasmbus_rpc::cbor::Write>::Error: std::fmt::Display,
{
e.array(val.len() as u64)?;
for item in val.iter() {
e.f32(*item)?;
}
Ok(())
}
#[doc(hidden)]
pub fn decode_f32_data(d: &mut wasmbus_rpc::cbor::Decoder<'_>) -> Result<F32Data, RpcError> {
let __result = {
if let Some(n) = d.array()? {
let mut arr: Vec<f32> = Vec::with_capacity(n as usize);
for _ in 0..(n as usize) {
arr.push(d.f32()?)
}
arr
} else {
let mut arr: Vec<f32> = Vec::new();
loop {
match d.datatype() {
Err(_) => break,
Ok(wasmbus_rpc::cbor::Type::Break) => break,
Ok(_) => arr.push(d.f32()?),
}
}
arr
}
};
Ok(__result)
}
pub type F64Data = Vec<f64>;
#[doc(hidden)]
#[allow(unused_mut)]
pub fn encode_f64_data<W: wasmbus_rpc::cbor::Write>(
mut e: &mut wasmbus_rpc::cbor::Encoder<W>,
val: &F64Data,
) -> RpcResult<()>
where
<W as wasmbus_rpc::cbor::Write>::Error: std::fmt::Display,
{
e.array(val.len() as u64)?;
for item in val.iter() {
e.f64(*item)?;
}
Ok(())
}
#[doc(hidden)]
pub fn decode_f64_data(d: &mut wasmbus_rpc::cbor::Decoder<'_>) -> Result<F64Data, RpcError> {
let __result = {
if let Some(n) = d.array()? {
let mut arr: Vec<f64> = Vec::with_capacity(n as usize);
for _ in 0..(n as usize) {
arr.push(d.f64()?)
}
arr
} else {
let mut arr: Vec<f64> = Vec::new();
loop {
match d.datatype() {
Err(_) => break,
Ok(wasmbus_rpc::cbor::Type::Break) => break,
Ok(_) => arr.push(d.f64()?),
}
}
arr
}
};
Ok(__result)
}
pub type OptMap = std::collections::HashMap<String, String>;
#[doc(hidden)]
#[allow(unused_mut)]
pub fn encode_opt_map<W: wasmbus_rpc::cbor::Write>(
mut e: &mut wasmbus_rpc::cbor::Encoder<W>,
val: &OptMap,
) -> RpcResult<()>
where
<W as wasmbus_rpc::cbor::Write>::Error: std::fmt::Display,
{
e.map(val.len() as u64)?;
for (k, v) in val {
e.str(k)?;
e.str(v)?;
}
Ok(())
}
#[doc(hidden)]
pub fn decode_opt_map(d: &mut wasmbus_rpc::cbor::Decoder<'_>) -> Result<OptMap, RpcError> {
let __result = {
{
let map_len = d.fixed_map()? as usize;
let mut m: std::collections::HashMap<String, String> =
std::collections::HashMap::with_capacity(map_len);
for _ in 0..map_len {
let k = d.str()?.to_string();
let v = d.str()?.to_string();
m.insert(k, v);
}
m
}
};
Ok(__result)
}
pub type PatternList = Vec<String>;
#[doc(hidden)]
#[allow(unused_mut)]
pub fn encode_pattern_list<W: wasmbus_rpc::cbor::Write>(
mut e: &mut wasmbus_rpc::cbor::Encoder<W>,
val: &PatternList,
) -> RpcResult<()>
where
<W as wasmbus_rpc::cbor::Write>::Error: std::fmt::Display,
{
e.array(val.len() as u64)?;
for item in val.iter() {
e.str(item)?;
}
Ok(())
}
#[doc(hidden)]
pub fn decode_pattern_list(
d: &mut wasmbus_rpc::cbor::Decoder<'_>,
) -> Result<PatternList, RpcError> {
let __result = {
if let Some(n) = d.array()? {
let mut arr: Vec<String> = Vec::with_capacity(n as usize);
for _ in 0..(n as usize) {
arr.push(d.str()?.to_string())
}
arr
} else {
let mut arr: Vec<String> = Vec::new();
loop {
match d.datatype() {
Err(_) => break,
Ok(wasmbus_rpc::cbor::Type::Break) => break,
Ok(_) => arr.push(d.str()?.to_string()),
}
}
arr
}
};
Ok(__result)
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub enum SampleUnion {
One(String),
Two(TestResult),
Three(F32Data),
Four(F64Data),
}
#[doc(hidden)]
#[allow(unused_mut)]
pub fn encode_sample_union<W: wasmbus_rpc::cbor::Write>(
mut e: &mut wasmbus_rpc::cbor::Encoder<W>,
val: &SampleUnion,
) -> RpcResult<()>
where
<W as wasmbus_rpc::cbor::Write>::Error: std::fmt::Display,
{
e.array(2)?;
match val {
SampleUnion::One(v) => {
e.u16(0)?;
e.str(v)?;
}
SampleUnion::Two(v) => {
e.u16(1)?;
encode_test_result(e, v)?;
}
SampleUnion::Three(v) => {
e.u16(2)?;
encode_f32_data(e, v)?;
}
SampleUnion::Four(v) => {
e.u16(3)?;
encode_f64_data(e, v)?;
}
}
Ok(())
}
#[doc(hidden)]
pub fn decode_sample_union(
d: &mut wasmbus_rpc::cbor::Decoder<'_>,
) -> Result<SampleUnion, RpcError> {
let __result = {
let len = d.fixed_array()?;
if len != 2 {
return Err(RpcError::Deser(
"decoding union 'SampleUnion': expected 2-array".to_string(),
));
}
match d.u16()? {
0 => {
let val = d.str()?.to_string();
SampleUnion::One(val)
}
1 => {
let val = decode_test_result(d).map_err(|e| {
format!(
"decoding 'org.wasmcloud.interface.testing#TestResult': {}",
e
)
})?;
SampleUnion::Two(val)
}
2 => {
let val = decode_f32_data(d).map_err(|e| {
format!("decoding 'org.wasmcloud.interface.testing#F32Data': {}", e)
})?;
SampleUnion::Three(val)
}
3 => {
let val = decode_f64_data(d).map_err(|e| {
format!("decoding 'org.wasmcloud.interface.testing#F64Data': {}", e)
})?;
SampleUnion::Four(val)
}
n => {
return Err(RpcError::Deser(format!("invalid field number for union 'org.wasmcloud.interface.testing#SampleUnion':{}", n)));
}
}
};
Ok(__result)
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct TestOptions {
pub patterns: PatternList,
pub options: OptMap,
}
#[doc(hidden)]
#[allow(unused_mut)]
pub fn encode_test_options<W: wasmbus_rpc::cbor::Write>(
mut e: &mut wasmbus_rpc::cbor::Encoder<W>,
val: &TestOptions,
) -> RpcResult<()>
where
<W as wasmbus_rpc::cbor::Write>::Error: std::fmt::Display,
{
e.array(2)?;
encode_pattern_list(e, &val.patterns)?;
encode_opt_map(e, &val.options)?;
Ok(())
}
#[doc(hidden)]
pub fn decode_test_options(
d: &mut wasmbus_rpc::cbor::Decoder<'_>,
) -> Result<TestOptions, RpcError> {
let __result = {
let mut patterns: Option<PatternList> = None;
let mut options: Option<OptMap> = None;
let is_array = match d.datatype()? {
wasmbus_rpc::cbor::Type::Array => true,
wasmbus_rpc::cbor::Type::Map => false,
_ => {
return Err(RpcError::Deser(
"decoding struct TestOptions, expected array or map".to_string(),
))
}
};
if is_array {
let len = d.fixed_array()?;
for __i in 0..(len as usize) {
match __i {
0 => {
patterns = Some(decode_pattern_list(d).map_err(|e| {
format!(
"decoding 'org.wasmcloud.interface.testing#PatternList': {}",
e
)
})?)
}
1 => {
options = Some(decode_opt_map(d).map_err(|e| {
format!("decoding 'org.wasmcloud.interface.testing#OptMap': {}", e)
})?)
}
_ => d.skip()?,
}
}
} else {
let len = d.fixed_map()?;
for __i in 0..(len as usize) {
match d.str()? {
"patterns" => {
patterns = Some(decode_pattern_list(d).map_err(|e| {
format!(
"decoding 'org.wasmcloud.interface.testing#PatternList': {}",
e
)
})?)
}
"options" => {
options = Some(decode_opt_map(d).map_err(|e| {
format!("decoding 'org.wasmcloud.interface.testing#OptMap': {}", e)
})?)
}
_ => d.skip()?,
}
}
}
TestOptions {
patterns: if let Some(__x) = patterns {
__x
} else {
return Err(RpcError::Deser(
"missing field TestOptions.patterns (#0)".to_string(),
));
},
options: if let Some(__x) = options {
__x
} else {
return Err(RpcError::Deser(
"missing field TestOptions.options (#1)".to_string(),
));
},
}
};
Ok(__result)
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct TestResult {
#[serde(default)]
pub name: String,
#[serde(default)]
pub passed: bool,
#[serde(rename = "snapData")]
#[serde(with = "serde_bytes")]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub snap_data: Option<Vec<u8>>,
}
#[doc(hidden)]
#[allow(unused_mut)]
pub fn encode_test_result<W: wasmbus_rpc::cbor::Write>(
mut e: &mut wasmbus_rpc::cbor::Encoder<W>,
val: &TestResult,
) -> RpcResult<()>
where
<W as wasmbus_rpc::cbor::Write>::Error: std::fmt::Display,
{
e.array(3)?;
e.str(&val.name)?;
e.bool(val.passed)?;
if let Some(val) = val.snap_data.as_ref() {
e.bytes(val)?;
} else {
e.null()?;
}
Ok(())
}
#[doc(hidden)]
pub fn decode_test_result(d: &mut wasmbus_rpc::cbor::Decoder<'_>) -> Result<TestResult, RpcError> {
let __result = {
let mut name: Option<String> = None;
let mut passed: Option<bool> = None;
let mut snap_data: Option<Option<Vec<u8>>> = Some(None);
let is_array = match d.datatype()? {
wasmbus_rpc::cbor::Type::Array => true,
wasmbus_rpc::cbor::Type::Map => false,
_ => {
return Err(RpcError::Deser(
"decoding struct TestResult, expected array or map".to_string(),
))
}
};
if is_array {
let len = d.fixed_array()?;
for __i in 0..(len as usize) {
match __i {
0 => name = Some(d.str()?.to_string()),
1 => passed = Some(d.bool()?),
2 => {
snap_data = if wasmbus_rpc::cbor::Type::Null == d.datatype()? {
d.skip()?;
Some(None)
} else {
Some(Some(d.bytes()?.to_vec()))
}
}
_ => d.skip()?,
}
}
} else {
let len = d.fixed_map()?;
for __i in 0..(len as usize) {
match d.str()? {
"name" => name = Some(d.str()?.to_string()),
"passed" => passed = Some(d.bool()?),
"snapData" => {
snap_data = if wasmbus_rpc::cbor::Type::Null == d.datatype()? {
d.skip()?;
Some(None)
} else {
Some(Some(d.bytes()?.to_vec()))
}
}
_ => d.skip()?,
}
}
}
TestResult {
name: if let Some(__x) = name {
__x
} else {
return Err(RpcError::Deser(
"missing field TestResult.name (#0)".to_string(),
));
},
passed: if let Some(__x) = passed {
__x
} else {
return Err(RpcError::Deser(
"missing field TestResult.passed (#1)".to_string(),
));
},
snap_data: snap_data.unwrap(),
}
};
Ok(__result)
}
pub type TestResults = Vec<TestResult>;
#[doc(hidden)]
#[allow(unused_mut)]
pub fn encode_test_results<W: wasmbus_rpc::cbor::Write>(
mut e: &mut wasmbus_rpc::cbor::Encoder<W>,
val: &TestResults,
) -> RpcResult<()>
where
<W as wasmbus_rpc::cbor::Write>::Error: std::fmt::Display,
{
e.array(val.len() as u64)?;
for item in val.iter() {
encode_test_result(e, item)?;
}
Ok(())
}
#[doc(hidden)]
pub fn decode_test_results(
d: &mut wasmbus_rpc::cbor::Decoder<'_>,
) -> Result<TestResults, RpcError> {
let __result = {
if let Some(n) = d.array()? {
let mut arr: Vec<TestResult> = Vec::with_capacity(n as usize);
for _ in 0..(n as usize) {
arr.push(decode_test_result(d).map_err(|e| {
format!(
"decoding 'org.wasmcloud.interface.testing#TestResult': {}",
e
)
})?)
}
arr
} else {
let mut arr: Vec<TestResult> = Vec::new();
loop {
match d.datatype() {
Err(_) => break,
Ok(wasmbus_rpc::cbor::Type::Break) => break,
Ok(_) => arr.push(decode_test_result(d).map_err(|e| {
format!(
"decoding 'org.wasmcloud.interface.testing#TestResult': {}",
e
)
})?),
}
}
arr
}
};
Ok(__result)
}
#[async_trait]
pub trait Testing {
fn contract_id() -> &'static str {
"wasmcloud:testing"
}
async fn start(&self, ctx: &Context, arg: &TestOptions) -> RpcResult<TestResults>;
async fn foo(&self, ctx: &Context) -> RpcResult<SampleUnion>;
}
#[doc(hidden)]
#[async_trait]
pub trait TestingReceiver: MessageDispatch + Testing {
async fn dispatch(&self, ctx: &Context, message: Message<'_>) -> Result<Vec<u8>, RpcError> {
match message.method {
"Start" => {
let value: TestOptions =
wasmbus_rpc::common::decode(&message.arg, &decode_test_options)
.map_err(|e| RpcError::Deser(format!("'TestOptions': {}", e)))?;
let resp = Testing::start(self, ctx, &value).await?;
let mut e = wasmbus_rpc::cbor::vec_encoder(true);
encode_test_results(&mut e, &resp)?;
let buf = e.into_inner();
Ok(buf)
}
"Foo" => {
let resp = Testing::foo(self, ctx).await?;
let mut e = wasmbus_rpc::cbor::vec_encoder(true);
encode_sample_union(&mut e, &resp)?;
let buf = e.into_inner();
Ok(buf)
}
_ => Err(RpcError::MethodNotHandled(format!(
"Testing::{}",
message.method
))),
}
}
}
#[derive(Debug)]
pub struct TestingSender<T: Transport> {
transport: T,
}
impl<T: Transport> TestingSender<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> TestingSender<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 TestingSender<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 TestingSender<wasmbus_rpc::actor::prelude::WasmHost> {
pub fn new() -> Self {
let transport =
wasmbus_rpc::actor::prelude::WasmHost::to_provider("wasmcloud:testing", "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:testing", link_name)?;
Ok(Self { transport })
}
}
#[async_trait]
impl<T: Transport + std::marker::Sync + std::marker::Send> Testing for TestingSender<T> {
#[allow(unused)]
async fn start(&self, ctx: &Context, arg: &TestOptions) -> RpcResult<TestResults> {
let mut e = wasmbus_rpc::cbor::vec_encoder(true);
encode_test_options(&mut e, arg)?;
let buf = e.into_inner();
let resp = self
.transport
.send(
ctx,
Message {
method: "Testing.Start",
arg: Cow::Borrowed(&buf),
},
None,
)
.await?;
let value: TestResults = wasmbus_rpc::common::decode(&resp, &decode_test_results)
.map_err(|e| RpcError::Deser(format!("'{}': TestResults", e)))?;
Ok(value)
}
#[allow(unused)]
async fn foo(&self, ctx: &Context) -> RpcResult<SampleUnion> {
let buf = *b"";
let resp = self
.transport
.send(
ctx,
Message {
method: "Testing.Foo",
arg: Cow::Borrowed(&buf),
},
None,
)
.await?;
let value: SampleUnion = wasmbus_rpc::common::decode(&resp, &decode_sample_union)
.map_err(|e| RpcError::Deser(format!("'{}': SampleUnion", e)))?;
Ok(value)
}
}