mavryk_data_encoding 0.6.0

Utilities for encoding/decoding data compatible with mavryk data encoding.
Documentation
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
// Copyright (c) SimpleStaking, Viable Systems, Trili Tech and Tezedge Contributors
// SPDX-License-Identifier: MIT

//! Defines types of the intermediate data format.

use std::str::FromStr;

use crate::enc::BinWriter;
use crate::encoding::Encoding;
use crate::encoding::HasEncoding;
use crate::has_encoding;
use crate::nom::NomReader;

use hex::FromHexError;
use num_bigint::Sign;
use serde::{Deserialize, Serialize};

#[cfg(feature = "fuzzing")]
use crate::fuzzing::bigint::BigIntMutator;

/// This is a wrapper for [num_bigint::BigInt] type.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BigInt(pub num_bigint::BigInt);

impl From<num_bigint::BigInt> for BigInt {
    fn from(from: num_bigint::BigInt) -> Self {
        BigInt(from)
    }
}

impl From<BigInt> for num_bigint::BigInt {
    fn from(from: BigInt) -> Self {
        from.0
    }
}

impl From<&num_bigint::BigInt> for BigInt {
    fn from(from: &num_bigint::BigInt) -> Self {
        BigInt(from.clone())
    }
}

impl From<&BigInt> for num_bigint::BigInt {
    fn from(from: &BigInt) -> Self {
        from.0.clone()
    }
}

/// Zarith number
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Zarith(pub num_bigint::BigInt);

impl From<num_bigint::BigInt> for Zarith {
    fn from(from: num_bigint::BigInt) -> Self {
        Zarith(from)
    }
}

impl From<Zarith> for num_bigint::BigInt {
    fn from(from: Zarith) -> Self {
        from.0
    }
}

impl From<&num_bigint::BigInt> for Zarith {
    fn from(from: &num_bigint::BigInt) -> Self {
        Zarith(from.clone())
    }
}

impl From<&Zarith> for num_bigint::BigInt {
    fn from(from: &Zarith) -> Self {
        from.0.clone()
    }
}

impl From<Zarith> for BigInt {
    fn from(source: Zarith) -> Self {
        Self(source.0)
    }
}

impl From<&Zarith> for BigInt {
    fn from(source: &Zarith) -> Self {
        Self(source.0.clone())
    }
}

has_encoding!(Zarith, ZARITH_ENCODING, { Encoding::Z });

/// Mumav number
#[cfg_attr(feature = "fuzzing", derive(fuzzcheck::DefaultMutator))]
#[derive(Clone, Debug)]
pub struct Mumav(
    #[cfg_attr(feature = "fuzzing", field_mutator(BigIntMutator))] pub num_bigint::BigInt,
);

impl<'de> Deserialize<'de> for Mumav {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        if deserializer.is_human_readable() {
            let string: String = serde::Deserialize::deserialize(deserializer)?;
            let big_int: num_bigint::BigInt = string
                .parse()
                .map_err(|err| serde::de::Error::custom(format!("cannot parse big int: {err}")))?;
            if big_int.sign() == Sign::Minus {
                return Err(serde::de::Error::custom("negative number for natural"));
            }
            Ok(Self(big_int))
        } else {
            Ok(Self(serde::Deserialize::deserialize(deserializer)?))
        }
    }
}

impl Serialize for Mumav {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        if serializer.is_human_readable() {
            let string = self.0.to_string();
            string.serialize(serializer)
        } else {
            self.0.serialize(serializer)
        }
    }
}

impl From<num_bigint::BigInt> for Mumav {
    fn from(from: num_bigint::BigInt) -> Self {
        Mumav(from)
    }
}

impl From<Mumav> for num_bigint::BigInt {
    fn from(from: Mumav) -> Self {
        from.0
    }
}

impl From<&num_bigint::BigInt> for Mumav {
    fn from(from: &num_bigint::BigInt) -> Self {
        Mumav(from.clone())
    }
}

impl From<&Mumav> for num_bigint::BigInt {
    fn from(from: &Mumav) -> Self {
        from.0.clone()
    }
}

impl From<Mumav> for BigInt {
    fn from(source: Mumav) -> Self {
        Self(source.0)
    }
}

impl From<&Mumav> for BigInt {
    fn from(source: &Mumav) -> Self {
        Self(source.0.clone())
    }
}

has_encoding!(Mumav, MUMAV_ENCODING, { Encoding::Mumav });

#[derive(Clone, PartialEq, Eq)]
//#[cfg_attr(feature = "fuzzing", derive(fuzzcheck::DefaultMutator))]
pub struct SizedBytes<const SIZE: usize>(pub [u8; SIZE]);

impl<const SIZE: usize> std::fmt::Display for SizedBytes<SIZE> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", hex::encode(self.0))
    }
}

impl<const SIZE: usize> std::fmt::Debug for SizedBytes<SIZE> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "bytes: {}", hex::encode(self.0))
    }
}

impl<const SIZE: usize> AsRef<[u8]> for SizedBytes<SIZE> {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl<const SIZE: usize> AsMut<[u8]> for SizedBytes<SIZE> {
    fn as_mut(&mut self) -> &mut [u8] {
        &mut self.0
    }
}

impl<const SIZE: usize> From<[u8; SIZE]> for SizedBytes<SIZE> {
    fn from(bytes: [u8; SIZE]) -> Self {
        Self(bytes)
    }
}

impl<const SIZE: usize> From<SizedBytes<SIZE>> for [u8; SIZE] {
    fn from(bytes: SizedBytes<SIZE>) -> Self {
        bytes.0
    }
}

impl<const SIZE: usize> Serialize for SizedBytes<SIZE> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        if serializer.is_human_readable() {
            serializer.serialize_str(&hex::encode(self.0.as_ref()))
        } else {
            serializer.serialize_newtype_struct(stringify!(Bytes<SIZE>), self.0.as_ref())
        }
    }
}

struct BytesVisitor<const SIZE: usize>;

impl<'de, const SIZE: usize> serde::de::Visitor<'de> for BytesVisitor<SIZE> {
    type Value = SizedBytes<{ SIZE }>;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("eigher sequence of bytes or hex encoded data expected")
    }

    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        let mut bytes = [0_u8; SIZE];
        hex::decode_to_slice(v, &mut bytes)
            .map_err(|e| E::custom(format!("error constructing bytes from hex: {e}")))?;
        Ok(bytes.into())
    }

    fn visit_newtype_struct<E>(self, e: E) -> Result<Self::Value, E::Error>
    where
        E: serde::Deserializer<'de>,
    {
        let mut bytes = [0_u8; SIZE];
        match <Vec<u8> as serde::Deserialize>::deserialize(e) {
            Ok(val) if val.len() == SIZE => bytes.copy_from_slice(&val),
            Ok(val) => {
                return Err(serde::de::Error::custom(format!(
                    "invalid lenght, expected {SIZE}, got {len}",
                    len = val.len()
                )))
            }
            Err(err) => {
                return Err(err);
            }
        };
        Ok(bytes.into())
    }

    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::SeqAccess<'de>,
    {
        let mut bytes = [0_u8; SIZE];
        match seq.next_element::<Vec<u8>>() {
            Ok(Some(val)) if val.len() == SIZE => bytes.copy_from_slice(&val),
            Ok(Some(val)) => {
                return Err(serde::de::Error::custom(format!(
                    "invalid lenght, expected {SIZE}, got {len}",
                    len = val.len()
                )))
            }
            Ok(None) => return Err(serde::de::Error::custom("no bytes".to_string())),
            Err(err) => return Err(err),
        };
        Ok(bytes.into())
    }
}

impl<'de, const SIZE: usize> Deserialize<'de> for SizedBytes<SIZE> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        if deserializer.is_human_readable() {
            deserializer.deserialize_str(BytesVisitor)
        } else {
            deserializer.deserialize_newtype_struct("Bytes<SIZE>", BytesVisitor)
        }
    }
}

impl<'a, const SIZE: usize> NomReader<'a> for SizedBytes<SIZE> {
    fn nom_read(input: &[u8]) -> crate::nom::NomResult<Self> {
        use crate::nom;
        let (input, slice) = nom::sized(SIZE, nom::bytes)(input)?;
        let mut bytes = [0; SIZE];
        bytes.copy_from_slice(&slice);
        Ok((input, bytes.into()))
    }
}

impl<const SIZE: usize> BinWriter for SizedBytes<SIZE> {
    fn bin_write(&self, bytes: &mut Vec<u8>) -> crate::enc::BinResult {
        use crate::enc;
        enc::put_bytes(&self.0, bytes);
        Ok(())
    }
}

impl<const SIZE: usize> HasEncoding for SizedBytes<SIZE> {
    fn encoding() -> Encoding {
        Encoding::sized(SIZE, Encoding::Bytes)
    }
}

/// Sequence of bytes bounded by maximum size
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
#[cfg_attr(feature = "fuzzing", derive(fuzzcheck::DefaultMutator))]
pub struct Bytes(Vec<u8>);

#[derive(Debug, thiserror::Error)]
pub enum BytesDecodeError {
    #[error(transparent)]
    Hex(#[from] FromHexError),
}

impl Bytes {
    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl std::fmt::Debug for Bytes {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("Bytes").field(&self.to_string()).finish()
    }
}

impl std::fmt::Display for Bytes {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        hex::encode(&self.0).fmt(f)
    }
}

impl From<Vec<u8>> for Bytes {
    fn from(source: Vec<u8>) -> Self {
        Self(source)
    }
}

impl From<Bytes> for Vec<u8> {
    fn from(source: Bytes) -> Self {
        source.0
    }
}

impl FromStr for Bytes {
    type Err = BytesDecodeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self(hex::decode(s)?))
    }
}

impl AsRef<[u8]> for Bytes {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl AsRef<Vec<u8>> for Bytes {
    fn as_ref(&self) -> &Vec<u8> {
        &self.0
    }
}

impl HasEncoding for Bytes {
    fn encoding() -> Encoding {
        Encoding::list(Encoding::Uint8)
    }
}

impl<'a> NomReader<'a> for Bytes {
    fn nom_read(input: &[u8]) -> crate::nom::NomResult<Self> {
        use crate::nom::bytes;
        let (input, b) = bytes(input)?;
        Ok((input, Self(b)))
    }
}

impl BinWriter for Bytes {
    fn bin_write(&self, output: &mut Vec<u8>) -> crate::enc::BinResult {
        crate::enc::put_bytes(self.0.as_ref(), output);
        Ok(())
    }
}

impl serde::Serialize for Bytes {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        if serializer.is_human_readable() {
            let hex_bytes = hex::encode(&self.0);
            serde::Serialize::serialize(&hex_bytes, serializer)
        } else {
            serde::Serialize::serialize(&self.0, serializer)
        }
    }
}

impl<'de> serde::Deserialize<'de> for Bytes {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        if deserializer.is_human_readable() {
            let hex_bytes: String = serde::Deserialize::deserialize(deserializer)?;
            let bytes = hex::decode(&hex_bytes).map_err(|err| {
                serde::de::Error::custom(format!("error decoding from hex string: {err}"))
            })?;
            Ok(Self(bytes))
        } else {
            let bytes = serde::Deserialize::deserialize(deserializer)?;
            Ok(Self(bytes))
        }
    }
}

/// Represents `true` value in binary format.
pub const BYTE_VAL_TRUE: u8 = 0xFF;
/// Represents `false` value in binary format.
pub const BYTE_VAL_FALSE: u8 = 0;
/// Represents `Some(x)` value in binary format.
pub const BYTE_VAL_SOME: u8 = 1;
/// Represents `None` value in binary format.
pub const BYTE_VAL_NONE: u8 = 0;
/// TE-172 - Represents 'Optional field' in binary format.
pub const BYTE_FIELD_SOME: u8 = 0xFF;
/// TE-172 - Represents `None` for 'Optional field' in binary format.
pub const BYTE_FIELD_NONE: u8 = 0;

/// Represents data in the intermediate form.
///
/// Rust struct is first converted to this intermediate form is then used for produce binary or json output.
/// Also this intermediate form is used when rust type is being created from a binary or json input.
///
/// # How it works:
///
/// Imagine we have struct `MyStruct` we want to serialize.
/// ```rust
/// struct MyStruct {
///   count: i32,
///   diameter: f32
/// }
/// let my_struct = MyStruct { count: 1, diameter: 102.95 };
/// ```
///
/// First we need to convert it to intermediate form represented by the [Value] type.
/// Structure will be converted to:
/// ```rust
/// use crate::mavryk_data_encoding::types::Value;
/// let intermediate = Value::Record(vec![
///     ("count".into(), Value::Int32(1)),
///     ("diameter".into(), Value::Float(102.95))
/// ]);
/// ```
///
/// After that the intermediate form can be converted to binary by passing it to [crate::binary_writer::BinaryWriter].
#[derive(PartialEq, Debug)]
pub enum Value {
    /// Nothing: data is omitted from binary.
    Unit,
    /// Signed 8 bit integer (data is encoded as a byte in binary and an integer in JSON).
    Int8(i8),
    /// Unsigned 8 bit integer (data is encoded as a byte in binary and an integer in JSON).
    Uint8(u8),
    /// Signed 16 bit integer (data is encoded as a short in binary and an integer in JSON).
    Int16(i16),
    /// Unsigned 16 bit integer (data is encoded as a short in binary and an integer in JSON).
    Uint16(u16),
    /// Signed 31 bit integer, which corresponds to type int on 32-bit OCaml systems (data is encoded as a 32 bit int in binary and an integer in JSON).
    Int31(i32),
    /// Signed 32 bit integer (data is encoded as a 32-bit int in binary and an integer in JSON).
    Int32(i32),
    /// Signed 64 bit integer (data is encoded as a 64-bit int in binary and a decimal string in JSON).
    Int64(i64),
    /// Integer with bounds in a given range. Both bounds are inclusive.
    RangedInt(i32),
    /// Encoding of floating point number (encoded as a floating point number in JSON and a double in binary).
    Float(f64),
    /// Float with bounds in a given range. Both bounds are inclusive.
    RangedFloat(f64),
    /// Encoding of a boolean (data is encoded as a byte in binary and a boolean in JSON).
    Bool(bool),
    /// Encoding of a string
    /// - encoded as a byte sequence in binary prefixed by the length
    /// of the string
    /// - encoded as a string in JSON.
    String(String),
    /// Encoding of arbitrary bytes (encoded via hex in JSON and directly as a sequence byte in binary).
    Bytes(Vec<u8>),
    /// Combinator to make an optional value
    /// (represented as a 1-byte tag followed by the data (or nothing) in binary
    ///  and either the raw value or an empty object in JSON).
    Option(Option<Box<Value>>),
    /// List combinator.
    /// - encoded as an array in JSON
    /// - encoded as the concatenation of all the element in binary
    /// in binary prefixed by its length in bytes
    List(Vec<Value>),
    /// Enum value with name and/or ordinal number
    Enum(Option<String>, Option<u32>),
    /// Tag value with variant id and tag inner value
    Tag(String, Box<Value>),
    /// A Record is represented by a vector of (`<record name>`, `value`).
    /// This allows schema-less encoding.
    ///
    /// See [Record](types.Record) for a more user-friendly support.
    Record(Vec<(String, Value)>),
    /// Tuple is heterogeneous collection of values, it should have fixed amount of elements
    Tuple(Vec<Value>),
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn bytes_to_string() {
        let bytes = Bytes(vec![0xde, 0xad, 0xbe, 0xef]);
        let bytes_hex = bytes.to_string();
        assert_eq!(&bytes_hex, "deadbeef");
    }

    #[test]
    fn bytes_parse() {
        let bytes: Bytes = "deadbeef".parse().unwrap();
        assert_eq!(bytes, Bytes(vec![0xde, 0xad, 0xbe, 0xef]));
    }

    #[test]
    fn bytes_parse_error() {
        let _ = "deadbeefe".parse::<Bytes>().expect_err("");
        let _ = "morebeef".parse::<Bytes>().expect_err("");
    }

    #[test]
    fn bytes_to_json() {
        let bytes = Bytes(vec![0xde, 0xad, 0xbe, 0xef]);
        let json = serde_json::to_value(bytes).unwrap();
        assert!(matches!(json.as_str(), Some("deadbeef")))
    }

    #[test]
    fn bytes_from_json() {
        let json = serde_json::json!("deadbeef");
        let bytes: Bytes = serde_json::from_value(json).unwrap();
        assert_eq!(bytes, Bytes(vec![0xde, 0xad, 0xbe, 0xef]));
    }
}