wasmbus_rpc/
cbor.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
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
//! cbor encoder and decoder
//!
//! This module wraps the underlying cbor implementation (currently minicbor)
//! so that third parties do not need to have any additional dependencies,
//! and to give us freedom to change the implementation in the future.
//!
#![allow(dead_code)]

use crate::error::{RpcError, RpcResult};
use std::fmt::Debug;

#[derive(Clone)]
pub struct Decoder<'b> {
    inner: minicbor::Decoder<'b>,
}

/// A non-allocating CBOR decoder
impl<'b> Decoder<'b> {
    /// Construct a Decoder for the given byte slice
    pub fn new(bytes: &'b [u8]) -> Self {
        Self { inner: minicbor::Decoder::new(bytes) }
    }

    /// Decode a bool value
    pub fn bool(&mut self) -> RpcResult<bool> {
        Ok(self.inner.bool()?)
    }

    /// Decode a u8 value
    pub fn u8(&mut self) -> RpcResult<u8> {
        Ok(self.inner.u8()?)
    }

    /// Decode a u16 value
    pub fn u16(&mut self) -> RpcResult<u16> {
        Ok(self.inner.u16()?)
    }

    /// Decode a u32 value
    pub fn u32(&mut self) -> RpcResult<u32> {
        Ok(self.inner.u32()?)
    }

    /// Decode a u64 value
    pub fn u64(&mut self) -> RpcResult<u64> {
        Ok(self.inner.u64()?)
    }

    /// Decode an i8 value
    pub fn i8(&mut self) -> RpcResult<i8> {
        Ok(self.inner.i8()?)
    }

    /// Decode an i16 value
    pub fn i16(&mut self) -> RpcResult<i16> {
        Ok(self.inner.i16()?)
    }

    /// Decode an i32 value
    pub fn i32(&mut self) -> RpcResult<i32> {
        Ok(self.inner.i32()?)
    }

    /// Decode an i64 value
    pub fn i64(&mut self) -> RpcResult<i64> {
        Ok(self.inner.i64()?)
    }

    /// Decode an f32 value
    pub fn f32(&mut self) -> RpcResult<f32> {
        Ok(self.inner.f32()?)
    }

    /// Decode an f64 value
    pub fn f64(&mut self) -> RpcResult<f64> {
        Ok(self.inner.f64()?)
    }

    /// Decode a char value
    pub fn char(&mut self) -> RpcResult<char> {
        Ok(self.inner.char()?)
    }

    /// Decode a string slice
    pub fn str(&mut self) -> RpcResult<&'b str> {
        Ok(self.inner.str()?)
    }

    /// Decode a null
    pub fn null(&mut self) -> RpcResult<()> {
        Ok(self.inner.null()?)
    }

    /// Decode a byte slice
    pub fn bytes(&mut self) -> RpcResult<&'b [u8]> {
        Ok(self.inner.bytes()?)
    }

    /// Begin decoding an array. If the length is known,
    /// it is returned as `Some`. For indefinite arrays, `None` is returned.
    pub fn array(&mut self) -> RpcResult<Option<u64>> {
        Ok(self.inner.array()?)
    }

    /// Begin decoding an array of known length
    pub fn fixed_array(&mut self) -> RpcResult<u64> {
        self.inner
            .array()?
            .ok_or_else(|| RpcError::Deser("indefinite array not expected".to_string()))
    }

    /// Begin decoding a map. If the size is known,
    /// it is returned as `Some`. For indefinite maps, `None` is returned.
    pub fn map(&mut self) -> RpcResult<Option<u64>> {
        Ok(self.inner.map()?)
    }

    /// Begin decoding a map of known size
    pub fn fixed_map(&mut self) -> RpcResult<u64> {
        self.inner
            .map()?
            .ok_or_else(|| RpcError::Deser("indefinite map not expected".to_string()))
    }

    /// Inspect the CBOR type at the current position
    pub fn datatype(&mut self) -> RpcResult<Type> {
        Ok(self.inner.datatype()?.into())
    }

    /// Skip over the current value
    pub fn skip(&mut self) -> RpcResult<()> {
        Ok(self.inner.skip()?)
    }

    // Pierce the veil.
    // This module exposes public functions to support code generated
    // by `weld-codegen`. Its purpose is to create an abstraction layer
    // around a cbor implementation. This function breaks that abstraction,
    // and any use of it outside the wasmbus-rpc crate risks breaking
    // if there is a change to the underlying implementation.
    //#[doc(hidden)]
    //pub(crate) fn inner(&mut self) -> &mut minicbor::Decoder {
    //    &self.inner
    //}
}

impl<'b> Debug for Decoder<'b> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> core::fmt::Result {
        self.inner.fmt(f)
    }
}

/// A type that can be decoded from CBOR.
pub trait Decode<'b>: Sized {
    /// Decode a value using the given `Decoder`.
    fn decode(d: &mut Decoder<'b>) -> Result<Self, RpcError>;

    /// If possible, return a nil value of `Self`.
    ///
    /// This method is primarily used by `minicbor-derive` and allows
    /// creating a special value denoting the absence of a "real" value if
    /// no CBOR value is present. The canonical example of a type where
    /// this is sensible is the `Option` type, whose `Decode::nil` method
    /// would return `Some(None)`.
    ///
    /// With the exception of `Option<_>` all types `T` are considered
    /// mandatory by default, i.e. `T::nil()` returns `None`. Missing values
    /// of `T` therefore cause decoding errors in derived `Decode`
    /// implementations.
    ///
    /// NB: A type implementing `Decode` with an overriden `Decode::nil`
    /// method should also override `Encode::is_nil` if it implements `Encode`
    /// at all.
    fn nil() -> Option<Self> {
        None
    }
}

pub trait MDecodeOwned<C>: for<'de> crate::minicbor::Decode<'de, C> {}
impl<T, C> MDecodeOwned<C> for T where T: for<'de> crate::minicbor::Decode<'de, C> {}

pub trait DecodeOwned: for<'de> crate::cbor::Decode<'de> {}
impl<T> DecodeOwned for T where T: for<'de> crate::cbor::Decode<'de> {}

//pub trait DecodeOwned: for<'de> Decode<'de> {}
//impl<T> DecodeOwned for T where T: for<'de> Decode<'de> {}

pub use minicbor::encode::Write;

pub struct Encoder<W: Write> {
    pub inner: minicbor::Encoder<W>,
}

pub fn vec_encoder(header: bool) -> Encoder<Vec<u8>> {
    let mut buf = Vec::new();
    if header {
        crate::common::MessageFormat::Cbor.write_header(&mut buf).unwrap();
    }
    Encoder { inner: minicbor::Encoder::new(buf) }
}

/// A non-allocating CBOR encoder
impl<W> Encoder<W>
where
    W::Error: std::fmt::Display,
    W: Write,
{
    /// Constructs an Encoder around the writer
    pub fn new(writer: W) -> Self {
        Self { inner: minicbor::Encoder::new(writer) }
    }

    /// Encode a bool value
    pub fn bool(&mut self, x: bool) -> RpcResult<&mut Self> {
        self.inner.bool(x)?;
        Ok(self)
    }

    /// Encode a u8 value
    pub fn u8(&mut self, x: u8) -> RpcResult<&mut Self> {
        self.inner.u8(x)?;
        Ok(self)
    }

    /// Encode a u16 value
    pub fn u16(&mut self, x: u16) -> RpcResult<&mut Self> {
        self.inner.u16(x)?;
        Ok(self)
    }

    /// Encode a u32 value
    pub fn u32(&mut self, x: u32) -> RpcResult<&mut Self> {
        self.inner.u32(x)?;
        Ok(self)
    }

    /// Encode a u64 value
    pub fn u64(&mut self, x: u64) -> RpcResult<&mut Self> {
        self.inner.u64(x)?;
        Ok(self)
    }

    /// Encode an i8 value
    pub fn i8(&mut self, x: i8) -> RpcResult<&mut Self> {
        self.inner.i8(x)?;
        Ok(self)
    }

    /// Encode an i16 value
    pub fn i16(&mut self, x: i16) -> RpcResult<&mut Self> {
        self.inner.i16(x)?;
        Ok(self)
    }

    /// Encode an i32 value
    pub fn i32(&mut self, x: i32) -> RpcResult<&mut Self> {
        self.inner.i32(x)?;
        Ok(self)
    }

    /// Encode an i64 value
    pub fn i64(&mut self, x: i64) -> RpcResult<&mut Self> {
        self.inner.i64(x)?;
        Ok(self)
    }

    /// Encode an f32 value
    pub fn f32(&mut self, x: f32) -> RpcResult<&mut Self> {
        self.inner.f32(x)?;
        Ok(self)
    }

    /// Encode an f64 value
    pub fn f64(&mut self, x: f64) -> RpcResult<&mut Self> {
        self.inner.f64(x)?;
        Ok(self)
    }

    /// Encode a char value
    pub fn char(&mut self, x: char) -> RpcResult<&mut Self> {
        self.inner.char(x)?;
        Ok(self)
    }

    /// Encode a byte slice
    pub fn bytes(&mut self, x: &[u8]) -> RpcResult<&mut Self> {
        self.inner.bytes(x)?;
        Ok(self)
    }

    /// Encode a string slice
    pub fn str(&mut self, x: &str) -> RpcResult<&mut Self> {
        self.inner.str(x)?;
        Ok(self)
    }

    /// Encode a null
    pub fn null(&mut self) -> RpcResult<&mut Self> {
        self.inner.null()?;
        Ok(self)
    }

    /// Begin encoding an array with `len` elements
    pub fn array(&mut self, len: u64) -> RpcResult<&mut Self> {
        self.inner.array(len)?;
        Ok(self)
    }

    /// Begin encoding an array with indefinite length
    pub fn begin_array(&mut self) -> RpcResult<&mut Self> {
        self.inner.begin_array()?;
        Ok(self)
    }

    /// Begin encoding a map with `len` elements
    pub fn map(&mut self, len: u64) -> RpcResult<&mut Self> {
        self.inner.map(len)?;
        Ok(self)
    }

    /// Begin encoding a map with indefinite length
    pub fn begin_map(&mut self) -> RpcResult<&mut Self> {
        self.inner.begin_map()?;
        Ok(self)
    }

    /// Begin encoding a byte slice with indefinite length
    /// Use Encoder::end to terminate
    pub fn begin_bytes(&mut self) -> RpcResult<&mut Self> {
        self.inner.begin_bytes()?;
        Ok(self)
    }

    /// Begin encoding an indefinite number of string slices
    /// Use Encoder::end to terminate
    pub fn begin_str(&mut self) -> RpcResult<&mut Self> {
        self.inner.begin_str()?;
        Ok(self)
    }

    /// Terminate an indefinite collection
    pub fn end(&mut self) -> RpcResult<&mut Self> {
        self.inner.end()?;
        Ok(self)
    }

    /// Returns the inner writer
    pub fn into_inner(self) -> W {
        self.inner.into_writer()
    }

    /// Write a tag
    pub fn tag(&mut self, tag: u64) -> RpcResult<&mut Self> {
        self.inner.tag(minicbor::data::Tag::Unassigned(tag))?;
        Ok(self)
    }

    // Pierce the veil.
    // This module exposes public functions to support code generated
    // by `weld-codegen`. Its purpose is to create an abstraction layer
    // around a cbor implementation. This function breaks that abstraction,
    // and any use of it outside the wasmbus-rpc crate risks breaking
    // if there is a change to the underlying implementation.
    //#[doc(hidden)]
    //pub(crate) fn inner(&mut self) -> &mut minicbor::Encoder {
    //    &self.inner
    //}
}

/// CBOR data types.
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
pub enum Type {
    Bool,
    Null,
    Undefined,
    U8,
    U16,
    U32,
    U64,
    I8,
    I16,
    I32,
    I64,
    Int,
    F16,
    F32,
    F64,
    Simple,
    Bytes,
    BytesIndef,
    String,
    StringIndef,
    Array,
    ArrayIndef,
    Map,
    MapIndef,
    Break,
    Tag,
    Unknown(u8),
}

use minicbor::data::Type as MT;
impl From<MT> for Type {
    fn from(t: MT) -> Type {
        match t {
            MT::Bool => Type::Bool,
            MT::Null => Type::Null,
            MT::Undefined => Type::Undefined,
            MT::U8 => Type::U8,
            MT::U16 => Type::U16,
            MT::U32 => Type::U32,
            MT::U64 => Type::U64,
            MT::I8 => Type::I8,
            MT::I16 => Type::I16,
            MT::I32 => Type::I32,
            MT::I64 => Type::I64,
            MT::Int => Type::Int,
            MT::F16 => Type::F16,
            MT::F32 => Type::F32,
            MT::F64 => Type::F64,
            MT::Simple => Type::Simple,
            MT::Bytes => Type::Bytes,
            MT::BytesIndef => Type::BytesIndef,
            MT::String => Type::String,
            MT::StringIndef => Type::StringIndef,
            MT::Array => Type::Array,
            MT::ArrayIndef => Type::ArrayIndef,
            MT::Map => Type::Map,
            MT::MapIndef => Type::MapIndef,
            MT::Tag => Type::Tag,
            MT::Break => Type::Break,
            MT::Unknown(x) => Type::Unknown(x),
        }
    }
}

impl std::fmt::Display for Type {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Type::Bool => f.write_str("bool"),
            Type::Null => f.write_str("null"),
            Type::Undefined => f.write_str("undefined"),
            Type::U8 => f.write_str("u8"),
            Type::U16 => f.write_str("u16"),
            Type::U32 => f.write_str("u32"),
            Type::U64 => f.write_str("u64"),
            Type::I8 => f.write_str("i8"),
            Type::I16 => f.write_str("i16"),
            Type::I32 => f.write_str("i32"),
            Type::I64 => f.write_str("i64"),
            Type::Int => f.write_str("int"),
            Type::F16 => f.write_str("f16"),
            Type::F32 => f.write_str("f32"),
            Type::F64 => f.write_str("f64"),
            Type::Simple => f.write_str("simple"),
            Type::Bytes => f.write_str("bytes"),
            Type::BytesIndef => f.write_str("indefinite bytes"),
            Type::String => f.write_str("string"),
            Type::StringIndef => f.write_str("indefinite string"),
            Type::Array => f.write_str("array"),
            Type::ArrayIndef => f.write_str("indefinite array"),
            Type::Map => f.write_str("map"),
            Type::MapIndef => f.write_str("indefinite map"),
            Type::Tag => f.write_str("tag"),
            Type::Break => f.write_str("break"),
            Type::Unknown(n) => write!(f, "{n:#x}"),
        }
    }
}

#[inline]
pub fn decode_u8(d: &mut Decoder<'_>) -> RpcResult<u8> {
    d.u8()
}
#[inline]
pub fn decode_u16(d: &mut Decoder<'_>) -> RpcResult<u16> {
    d.u16()
}
#[inline]
pub fn decode_u32(d: &mut Decoder<'_>) -> RpcResult<u32> {
    d.u32()
}
#[inline]
pub fn decode_u64(d: &mut Decoder<'_>) -> RpcResult<u64> {
    d.u64()
}
#[inline]
pub fn decode_i8(d: &mut Decoder<'_>) -> RpcResult<i8> {
    d.i8()
}
#[inline]
pub fn decode_i16(d: &mut Decoder<'_>) -> RpcResult<i16> {
    d.i16()
}
#[inline]
pub fn decode_i32(d: &mut Decoder<'_>) -> RpcResult<i32> {
    d.i32()
}
#[inline]
pub fn decode_i64(d: &mut Decoder<'_>) -> RpcResult<i64> {
    d.i64()
}
#[inline]
pub fn decode_boolean(d: &mut Decoder<'_>) -> RpcResult<bool> {
    d.bool()
}
#[inline]
pub fn decode_string(d: &mut Decoder<'_>) -> RpcResult<String> {
    Ok(d.str()?.to_string())
}
#[inline]
pub fn decode_null(d: &mut Decoder<'_>) -> RpcResult<()> {
    d.null()
}
#[inline]
pub fn decode_blob(d: &mut Decoder<'_>) -> RpcResult<Vec<u8>> {
    Ok(d.bytes()?.to_vec())
}
#[inline]
pub fn decode_byte(d: &mut Decoder<'_>) -> RpcResult<u8> {
    d.u8()
}
#[inline]
pub fn decode_char(d: &mut Decoder<'_>) -> RpcResult<char> {
    d.char()
}
#[inline]
pub fn decode_integer(d: &mut Decoder<'_>) -> RpcResult<i32> {
    d.i32()
}
#[inline]
pub fn decode_long(d: &mut Decoder<'_>) -> RpcResult<i64> {
    d.i64()
}
#[inline]
pub fn decode_float(d: &mut Decoder<'_>) -> RpcResult<f32> {
    d.f32()
}
#[inline]
pub fn decode_double(d: &mut Decoder<'_>) -> RpcResult<f64> {
    d.f64()
}