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
use candid::de::IDLDeserialize;
use candid::utils::ArgumentDecoder;
use candid::{CandidType, Deserialize, Result};

/// Trait allowing encoding and decoding of unsized data.
///
/// See also [SBox].
///
/// By default is implemented for:
/// 1. Every [AsFixedSizeBytes] type
/// 2. `Vec<u8>`
/// 3. `String`
///
/// This trait can be easily implemented using derive macros:
/// 1. [derive::CandidAsDynSizeBytes] implements this trait for types which
/// already implement [candid::CandidType] and [candid::Deserialize].
/// 2. [derive::FixedSizeAsDynSizeBytes] implements this trait for types which already
/// implement [AsFixedSizeBytes].
pub trait AsDynSizeBytes {
    /// Encodes self into vector of bytes
    ///
    /// # Panics
    /// Should panic if data encoding failed.
    fn as_dyn_size_bytes(&self) -> Vec<u8>;

    /// Decodes self from a slice of bytes.
    ///
    /// # Important
    /// The slice *can* have trailing bytes with unmeaningful.
    /// It means, that if your data encoded value is [1, 0, 1, 0], then it should also be able to
    /// decode itself from a slice like [1, 0, 1, 0, 0, 0, 0, 0, 0] or [1, 0, 1, 0, 1, 1, 0, 1].
    ///
    /// # Panics
    /// Should panic if data decoding failed.
    fn from_dyn_size_bytes(buf: &[u8]) -> Self;
}

#[cfg(not(feature = "custom_dyn_encoding"))]
use crate::encoding::AsFixedSizeBytes;

#[cfg(not(feature = "custom_dyn_encoding"))]
use crate::primitive::s_box::SBox;

#[cfg(not(feature = "custom_dyn_encoding"))]
impl<T: AsFixedSizeBytes> AsDynSizeBytes for T {
    #[inline]
    fn as_dyn_size_bytes(&self) -> Vec<u8> {
        let mut v = vec![0u8; T::SIZE];
        self.as_fixed_size_bytes(&mut v);

        v
    }

    #[inline]
    fn from_dyn_size_bytes(buf: &[u8]) -> Self {
        Self::from_fixed_size_bytes(&buf[0..T::SIZE])
    }
}

#[cfg(not(feature = "custom_dyn_encoding"))]
impl AsDynSizeBytes for Vec<u8> {
    #[inline]
    fn as_dyn_size_bytes(&self) -> Vec<u8> {
        let mut v = vec![0u8; usize::SIZE + self.len()];

        self.len().as_fixed_size_bytes(&mut v[0..usize::SIZE]);
        v[usize::SIZE..(usize::SIZE + self.len())].copy_from_slice(&self);

        v
    }

    #[inline]
    fn from_dyn_size_bytes(buf: &[u8]) -> Self {
        let len = usize::from_fixed_size_bytes(&buf[0..usize::SIZE]);
        let mut v = vec![0u8; len];

        v.copy_from_slice(&buf[usize::SIZE..(usize::SIZE + len)]);

        v
    }
}

#[cfg(not(feature = "custom_dyn_encoding"))]
impl AsDynSizeBytes for String {
    #[inline]
    fn as_dyn_size_bytes(&self) -> Vec<u8> {
        let mut v = vec![0u8; usize::SIZE + self.len()];

        self.len().as_fixed_size_bytes(&mut v[0..usize::SIZE]);
        v[usize::SIZE..(usize::SIZE + self.len())].copy_from_slice(self.as_bytes());

        v
    }

    #[inline]
    fn from_dyn_size_bytes(buf: &[u8]) -> Self {
        let len = usize::from_fixed_size_bytes(&buf[0..usize::SIZE]);
        let mut v = vec![0u8; len];

        v.copy_from_slice(&buf[usize::SIZE..(usize::SIZE + len)]);

        String::from_utf8(v).unwrap()
    }
}

pub fn candid_decode_args_allow_trailing<'a, Tuple>(bytes: &'a [u8]) -> Result<Tuple>
where
    Tuple: ArgumentDecoder<'a>,
{
    let mut de = IDLDeserialize::new(bytes)?;
    let res = ArgumentDecoder::decode(&mut de)?;

    Ok(res)
}

pub fn candid_decode_one_allow_trailing<'a, T>(bytes: &'a [u8]) -> Result<T>
where
    T: Deserialize<'a> + CandidType,
{
    let (res,) = candid_decode_args_allow_trailing(bytes)?;
    Ok(res)
}