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
//! Generic types for encoding XDR variable length arrays and strings

use core::convert::AsRef;
use sp_std::{vec, vec::Vec};

use super::streams::{DecodeError, ReadStream, WriteStream};
use super::xdr_codec::XdrCodec;
use crate::StellarSdkError;

/// Type for binary data whose length is not predefined but bounded by a constant
///
/// The const generic `N` specifies the maxmimum number of bytes a value of this
/// type is allowed to have.
#[allow(dead_code)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct LimitedVarOpaque<const N: i32>(Vec<u8>);

impl<const N: i32> LimitedVarOpaque<N> {
    /// Construct a new `LimitedVarOpaque` from a byte vector
    ///
    /// The length of the byte vector must not exceed `N`. Otherwise this function returns
    /// an error.
    pub fn new(vec: Vec<u8>) -> Result<Self, StellarSdkError> {
        match vec.len() > N as usize {
            true => Err(StellarSdkError::ExceedsMaximumLength {
                requested_length: vec.len(),
                allowed_length: N,
            }),
            false => Ok(LimitedVarOpaque(vec)),
        }
    }

    pub fn new_empty() -> Self {
        LimitedVarOpaque(vec![])
    }

    /// Returns a reference to the raw byte vector
    pub fn get_vec(&self) -> &Vec<u8> {
        &self.0
    }

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

impl<const N: i32> XdrCodec for LimitedVarOpaque<N> {
    /// The XDR encoder implementation for `LimitedVarOpaque`
    fn to_xdr_buffered(&self, write_stream: &mut WriteStream) {
        write_stream.write_next_u32(self.0.len() as u32);
        write_stream.write_next_binary_data(&self.0[..]);
    }

    /// The XDR decoder implementation for `LimitedVarOpaque`
    fn from_xdr_buffered<R: AsRef<[u8]>>(
        read_stream: &mut ReadStream<R>,
    ) -> Result<Self, DecodeError> {
        let length = read_stream.read_next_u32()? as i32;
        match length > N {
            true => Err(DecodeError::VarOpaqueExceedsMaxLength {
                at_position: read_stream.get_position(),
                max_length: N,
                actual_length: length,
            }),
            false => Ok(
                LimitedVarOpaque::new(read_stream.read_next_binary_data(length as usize)?).unwrap(),
            ),
        }
    }
}

/// Type for binary data whose length is not predefined and not bounded
///
/// Actually an `UnlimitedVarOpaque` is limited: it must not have more than
/// `i32::MAX` bytes.
#[allow(dead_code)]
pub type UnlimitedVarOpaque = LimitedVarOpaque<{ i32::MAX }>;

/// Type for an ASCII string whose length is not predefined but bounded by a constant
///
/// The const generic `N` specifies the maxmimum number of ASCII characters a value of this
/// type is allowed to have.
#[allow(dead_code)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct LimitedString<const N: i32>(Vec<u8>);

impl<const N: i32> LimitedString<N> {
    /// Construct a new `LimitedString` from a byte vector
    ///
    /// The byte vector represents an ASCII string.
    /// The length of the byte vector must not exceed `N`. Otherwise this function returns
    /// an error
    pub fn new(vec: Vec<u8>) -> Result<Self, StellarSdkError> {
        match vec.len() > N as usize {
            true => Err(StellarSdkError::ExceedsMaximumLength {
                requested_length: vec.len(),
                allowed_length: N,
            }),
            false => Ok(LimitedString(vec)),
        }
    }

    /// Returns a reference to the raw byte vector
    pub fn get_vec(&self) -> &Vec<u8> {
        &self.0
    }

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

impl<const N: i32> XdrCodec for LimitedString<N> {
    /// The XDR encoder implementation for `LimitedString`
    fn to_xdr_buffered(&self, write_stream: &mut WriteStream) {
        write_stream.write_next_u32(self.0.len() as u32);
        write_stream.write_next_binary_data(&self.0[..]);
    }

    /// The XDR decoder implementation for `LimitedString`
    fn from_xdr_buffered<R: AsRef<[u8]>>(
        read_stream: &mut ReadStream<R>,
    ) -> Result<Self, DecodeError> {
        let length = read_stream.read_next_u32()? as i32;
        match length > N {
            true => Err(DecodeError::StringExceedsMaxLength {
                at_position: read_stream.get_position(),
                max_length: N,
                actual_length: length,
            }),
            false => Ok(
                LimitedString::new(read_stream.read_next_binary_data(length as usize)?).unwrap(),
            ),
        }
    }
}

/// Type for an ASCII string whose length is not predefined and not bounded
///
/// Actually an `UnlimitedString` is limited: it must not have more than
/// `i32::MAX` characters.
#[allow(dead_code)]
pub type UnlimitedString = LimitedString<{ i32::MAX }>;

/// Type for an array whose length is not predefined but bounded by a constant
///
/// The generic variable `T` specifies the types of the elements of this array.
/// The const generic `N` specifies the maxmimum number of elements a value of this
/// type is allowed to have.
#[allow(dead_code)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct LimitedVarArray<T, const N: i32>(Vec<T>);

impl<T, const N: i32> LimitedVarArray<T, N> {
    /// Construct a new `LimitedVarArray` from a vector
    ///
    /// The length of the vector must not exceed `N`. Otherwise this function returns
    /// an error
    pub fn new(vec: Vec<T>) -> Result<Self, StellarSdkError> {
        match vec.len() > N as usize {
            true => Err(StellarSdkError::ExceedsMaximumLength {
                requested_length: vec.len(),
                allowed_length: N,
            }),
            false => Ok(LimitedVarArray(vec)),
        }
    }

    pub fn new_empty() -> Self {
        LimitedVarArray(vec![])
    }

    /// Returns a reference to the byte vector
    pub fn get_vec(&self) -> &Vec<T> {
        &self.0
    }

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

    /// Add an element to the byte vector
    ///
    /// Return an `Err` if the array already has the maximal number of elements.
    pub fn push(&mut self, item: T) -> Result<(), StellarSdkError> {
        if self.0.len() >= N as usize - 1 {
            return Err(StellarSdkError::ExceedsMaximumLength {
                requested_length: self.0.len() + 1,
                allowed_length: N,
            });
        }

        self.0.push(item);
        Ok(())
    }
}

impl<T: XdrCodec, const N: i32> XdrCodec for LimitedVarArray<T, N> {
    /// The XDR encoder implementation for `LimitedVarArray`
    fn to_xdr_buffered(&self, write_stream: &mut WriteStream) {
        write_stream.write_next_u32(self.0.len() as u32);
        for item in self.0.iter() {
            item.to_xdr_buffered(write_stream);
        }
    }

    /// The XDR decoder implementation for `LimitedVarArray`
    fn from_xdr_buffered<R: AsRef<[u8]>>(
        read_stream: &mut ReadStream<R>,
    ) -> Result<Self, DecodeError> {
        let length = read_stream.read_next_u32()? as i32;
        match length > N {
            true => Err(DecodeError::VarArrayExceedsMaxLength {
                at_position: read_stream.get_position(),
                max_length: N,
                actual_length: length,
            }),
            false => {
                let mut result = Vec::<T>::with_capacity(length as usize);
                for _ in 0..length {
                    result.push(T::from_xdr_buffered(read_stream)?)
                }
                Ok(LimitedVarArray::new(result).unwrap())
            }
        }
    }
}

/// Type for an XDR array whose length is not predefined and not bounded
///
/// Actually an `UnlimitedVarArray` is limited: it must not have more than
/// `i32::MAX` characters.
#[allow(dead_code)]
pub type UnlimitedVarArray<T> = LimitedVarArray<T, { i32::MAX }>;