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
use crate::ByteArray;
use std::marker::PhantomData;
use std::num::NonZeroUsize;

/// A dynamically created buffer for T
#[derive(Clone, Default)]
pub struct ByteBuilder<T> {
    buffer: Vec<u8>,
    phantom: PhantomData<T>,
    expected: Option<NonZeroUsize>,
}

impl<T> From<ByteBuilder<T>> for Vec<u8> {
    fn from(bb: ByteBuilder<T>) -> Vec<u8> {
        bb.buffer
    }
}

impl<T> ByteBuilder<T> {
    /// Create an unconstrained Builder
    pub fn new() -> Self {
        ByteBuilder {
            buffer: Vec::new(),
            phantom: PhantomData,
            expected: None,
        }
    }

    /// Create a builder of fixed size
    pub fn new_fixed(size: NonZeroUsize) -> Self {
        ByteBuilder {
            buffer: Vec::with_capacity(size.get()),
            phantom: PhantomData,
            expected: Some(size),
        }
    }

    /// Append an u8 in the builder
    pub fn u8(self, v: u8) -> Self {
        let mut buf = self.buffer;
        buf.push(v);
        ByteBuilder {
            buffer: buf,
            phantom: self.phantom,
            expected: self.expected,
        }
    }
    /// Append bytes in the builder
    pub fn bytes(self, v: &[u8]) -> Self {
        let mut buf = self.buffer;
        buf.extend_from_slice(v);
        ByteBuilder {
            buffer: buf,
            phantom: self.phantom,
            expected: self.expected,
        }
    }

    /// fold over an iterator
    pub fn fold<F, I>(self, l: I, f: F) -> Self
    where
        I: Iterator,
        F: FnMut(Self, I::Item) -> Self,
    {
        l.fold(self, f)
    }

    /// Write an iterator of maximum 255 items using the closure `f`.
    ///
    /// Note that the buffer contains a byte to represent the size
    /// of the list.
    pub fn iter8<F, I>(self, l: I, f: F) -> Self
    where
        I: IntoIterator,
        I::IntoIter: ExactSizeIterator,
        F: FnMut(Self, I::Item) -> Self,
    {
        let l = l.into_iter();
        let len = l.len();
        assert!(len <= u8::MAX as usize);
        let bb = self.u8(len as u8);
        l.fold(bb, f)
    }

    /// Write an iterator of maximum 2^16 - 1 items using the closure `f`.
    ///
    /// Note that the buffer contains 2 bytes to represent the size
    /// of the list.
    pub fn iter16<F, I>(self, l: I, f: F) -> Self
    where
        I: IntoIterator,
        I::IntoIter: ExactSizeIterator,
        F: FnMut(Self, I::Item) -> Self,
    {
        let l = l.into_iter();
        let len = l.len();
        assert!(len <= u16::MAX as usize);
        let bb = self.u16(len as u16);
        l.fold(bb, f)
    }

    /// Write an iterator of maximum 2^32 - 1 items using the closure `f`.
    ///
    /// Note that the buffer contains 4 bytes to represent the size
    /// of the list.
    pub fn iter32<F, I>(self, l: I, f: F) -> Self
    where
        I: IntoIterator,
        I::IntoIter: ExactSizeIterator,
        F: FnMut(Self, I::Item) -> Self,
    {
        let l = l.into_iter();
        let len = l.len();
        assert!(len <= u32::MAX as usize);
        let bb = self.u32(len as u32);
        l.fold(bb, f)
    }

    #[allow(clippy::should_implement_trait)]
    pub fn sub<F, U>(self, f: F) -> Self
    where
        F: Fn(ByteBuilder<U>) -> ByteBuilder<U>,
    {
        let res = f(ByteBuilder {
            buffer: self.buffer,
            phantom: PhantomData,
            expected: None,
        });
        ByteBuilder {
            buffer: res.buffer,
            phantom: self.phantom,
            expected: self.expected,
        }
    }

    /// Append an u16 in the builder
    pub fn u16(self, v: u16) -> Self {
        self.bytes(&v.to_be_bytes())
    }

    /// Append an u32 in the builder
    pub fn u32(self, v: u32) -> Self {
        self.bytes(&v.to_be_bytes())
    }

    /// Append an u64 in the builder
    pub fn u64(self, v: u64) -> Self {
        self.bytes(&v.to_be_bytes())
    }

    /// Append an u128 in the builder
    pub fn u128(self, v: u128) -> Self {
        self.bytes(&v.to_be_bytes())
    }

    /// Call 'f' on bytebuilder and the value if the value is present, then return
    /// the bytebuilder, otherwise just return the bytebuilder
    pub fn option<F, V>(self, value: Option<V>, f: F) -> Self
    where
        F: FnOnce(Self, V) -> Self,
    {
        self.option_or_else(value, |bb| bb, f)
    }

    /// Run the first closure if the value is not present, or the second closure with
    /// the present parameter.
    ///
    /// this is loosely based on Option::map_or_else
    ///
    /// The following call:
    ///
    /// ```ignore
    /// byte_builder.option_or_else(value, none_call, some_call);
    /// ```
    ///
    /// equivalent to the construction:
    ///
    /// ```ignore
    /// if let Some(value) = value {
    ///    some_call(byte_builder, value)
    /// } else {
    ///    none_call(byte_builder)
    /// }
    /// ```
    pub fn option_or_else<F, G, V>(self, value: Option<V>, default: G, f: F) -> Self
    where
        F: FnOnce(Self, V) -> Self,
        G: FnOnce(Self) -> Self,
    {
        match value {
            None => default(self),
            Some(v) => f(self, v),
        }
    }

    /// Finalize the buffer and return a fixed ByteArray of T
    pub fn finalize(self) -> ByteArray<T> {
        match self.expected {
            None => ByteArray::from_vec(self.buffer),
            Some(expected_sz) => {
                if expected_sz.get() == self.buffer.len() {
                    ByteArray::from_vec(self.buffer)
                } else {
                    panic!(
                        "internal-error: bytebuilder: expected size {} but got {}",
                        expected_sz.get(),
                        self.buffer.len()
                    )
                }
            }
        }
    }

    /// Finalize the buffer and return a fixed ByteArray of T
    pub fn finalize_as_vec(self) -> Vec<u8> {
        self.buffer
    }
}