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
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0

//! Implements serializer and deserializer for BinProt variable length natural integer (Nat0)

use std::marker::PhantomData;

use crate::{ReadBinProtExt, WriteBinProtExt};

use num::{FromPrimitive, Unsigned};
use serde::de::{self, Deserializer, Visitor};
use serde::ser::Serializer;
use std::io::Cursor;

/// Serializer for variable length natural integer
pub fn serialize<T, S>(n: &T, s: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
    T: Unsigned + Into<u64> + Copy,
{
    let mut bytes = Vec::new();
    bytes.bin_write_nat0(*n).unwrap();
    s.serialize_bytes(&bytes)
}

struct Nat0Visitor<T>(PhantomData<T>);

impl<T> Nat0Visitor<T> {
    pub fn new() -> Self {
        Nat0Visitor::<T>(PhantomData)
    }
}

impl<'de, T> Visitor<'de> for Nat0Visitor<T>
where
    T: Unsigned + FromPrimitive,
{
    type Value = T;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("A bin_prot encoded integer (1, 3, 5, or 9 bytes depending on size)")
    }

    fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        let mut rdr = Cursor::new(value);
        rdr.bin_read_nat0().map_err(de::Error::custom)
    }
}

/// Deserializer for variable length natural integer
pub fn deserialize<'de, D, T>(d: D) -> Result<T, D::Error>
where
    D: Deserializer<'de>,
    T: Unsigned + FromPrimitive,
{
    d.deserialize_bytes(Nat0Visitor::new())
}