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
use super::super::decode::{Decode, DecodeError};
use super::super::encode::Encode;
use super::super::header::Value;
use super::super::prim::{CborData, CborSlice};
use super::super::reader::{Reader, ReaderError};
use super::super::writer::Writer;
use std::borrow::{Borrow, ToOwned};

#[derive(Debug, Clone, Copy)]
pub enum StructureLength {
    Indefinite,
    Definite(Value),
}

impl StructureLength {
    pub fn is_indefinite(self) -> bool {
        match self {
            StructureLength::Indefinite => true,
            StructureLength::Definite(_) => false,
        }
    }
}

impl From<Option<Value>> for StructureLength {
    fn from(v: Option<Value>) -> StructureLength {
        match v {
            None => StructureLength::Indefinite,
            Some(val) => StructureLength::Definite(val),
        }
    }
}

impl From<u64> for StructureLength {
    fn from(v: u64) -> StructureLength {
        StructureLength::Definite(Value::canonical(v))
    }
}

/// CBOR Array with references to elements
#[derive(Debug, Clone)]
pub struct Array<'a> {
    pub(crate) len_encoding: StructureLength,
    pub(crate) elements: Vec<&'a CborSlice>,
}

/// CBOR Array with owned elements
#[derive(Debug, Clone)]
pub struct ArrayOwned {
    pub(crate) len_encoding: StructureLength,
    pub(crate) elements: Vec<CborData>,
}

/// CBOR Array builder, when constructing
pub struct ArrayBuilder {
    elements: Vec<CborData>,
}

/// CBOR Map with references to keys and values
#[derive(Debug, Clone)]
pub struct Map<'a> {
    pub(crate) len_encoding: StructureLength,
    pub(crate) elements: Vec<(&'a CborSlice, &'a CborSlice)>,
}

/// CBOR Map with owned keys and values
#[derive(Debug, Clone)]
pub struct MapOwned {
    pub(crate) len_encoding: StructureLength,
    pub(crate) elements: Vec<(CborData, CborData)>,
}

/// CBOR Tag Value in a Tag
#[derive(Debug, Clone, Copy)]
pub struct TagValue(pub(crate) Value);

/// CBOR Tag with reference to the tagged element
#[derive(Debug, Clone)]
pub struct Tag<'a> {
    pub(crate) tag_val: TagValue,
    pub(crate) data: &'a CborSlice,
}

/// CBOR Tag with owned tagged element
#[derive(Debug, Clone)]
pub struct TagOwned {
    pub(crate) tag_val: TagValue,
    pub(crate) data: CborData,
}

impl<'a> Array<'a> {
    pub fn len(&self) -> usize {
        self.elements.len()
    }

    pub fn iter(&'a self) -> impl Iterator<Item = Reader<'a>> {
        self.elements.iter().map(|v| v.reader())
    }

    pub fn to_vec<F, T: Decode>(&self, f: F) -> Result<Vec<T>, DecodeError>
    where
        F: for<'b> Fn(&mut Reader<'b>) -> Result<T, DecodeError>,
    {
        let mut output = Vec::with_capacity(self.len());
        for element in self.elements.iter() {
            let mut reader = Reader::new(element.as_ref());
            let value = f(&mut reader)?;
            output.push(value)
        }
        Ok(output)
    }

    pub fn owned(&self) -> ArrayOwned {
        ArrayOwned {
            len_encoding: self.len_encoding.clone(),
            elements: self
                .elements
                .iter()
                .map(|slice| (*slice).to_owned())
                .collect::<Vec<CborData>>(),
        }
    }
}

impl ArrayOwned {
    pub fn len(&self) -> usize {
        self.elements.len()
    }

    pub fn iter<'a>(&'a self) -> impl Iterator<Item = Reader<'a>> {
        self.elements
            .iter()
            .map(|v| v.borrow())
            .map(|v: &'a CborSlice| v.reader())
    }

    pub fn to_vec<'a, F, T: Decode>(&'a self, f: F) -> Result<Vec<T>, DecodeError>
    where
        F: Fn(&mut Reader<'a>) -> Result<T, DecodeError>,
    {
        let mut output = Vec::with_capacity(self.len());
        for element in self.elements.iter() {
            let mut reader = Reader::new(element.as_ref());
            let value = f(&mut reader)?;
            output.push(value)
        }
        Ok(output)
    }

    pub fn borrow<'a>(&'a self) -> Array<'a> {
        Array {
            len_encoding: self.len_encoding.clone(),
            elements: self
                .elements
                .iter()
                .map(|v| v.borrow())
                .collect::<Vec<&'a CborSlice>>(),
        }
    }
}

impl ArrayBuilder {
    /// Create a new array builder
    pub fn new() -> Self {
        Self { elements: vec![] }
    }

    /// Append a new data into the builder
    pub fn append(&mut self, data: CborData) {
        self.elements.push(data)
    }

    pub fn append_encodable<T: Encode>(&mut self, t: &T) {
        let mut writer = Writer::new();
        writer.encode(t);
        self.append(writer.finalize_data())
    }

    pub fn finite(self) -> ArrayOwned {
        ArrayOwned {
            len_encoding: StructureLength::from(self.elements.len() as u64),
            elements: self.elements,
        }
    }

    pub fn indefinite(self) -> ArrayOwned {
        ArrayOwned {
            len_encoding: StructureLength::Indefinite,
            elements: self.elements,
        }
    }
}

impl<'a> std::ops::Index<usize> for Array<'a> {
    type Output = &'a CborSlice;

    fn index(&self, index: usize) -> &Self::Output {
        &self.elements[index]
    }
}

impl std::ops::Index<usize> for ArrayOwned {
    type Output = CborData;

    fn index(&self, index: usize) -> &Self::Output {
        &self.elements[index]
    }
}

impl<'a> Map<'a> {
    pub fn len(&self) -> usize {
        self.elements.len()
    }

    pub fn iter(&'a self) -> impl Iterator<Item = (Reader<'a>, Reader<'a>)> {
        self.elements.iter().map(|(k, v)| (k.reader(), v.reader()))
    }

    pub fn keys(&'a self) -> impl Iterator<Item = Reader<'a>> {
        self.elements.iter().map(|(k, _v)| (k.reader()))
    }

    pub fn values(&'a self) -> impl Iterator<Item = Reader<'a>> {
        self.elements.iter().map(|(_k, v)| (v.reader()))
    }

    pub fn to_vec<F, G, K: Decode, V: Decode>(&self, f: F, g: G) -> Result<Vec<(K, V)>, DecodeError>
    where
        F: for<'b> Fn(&mut Reader<'b>) -> Result<K, DecodeError>,
        G: for<'b> Fn(&mut Reader<'b>) -> Result<V, DecodeError>,
    {
        let mut output = Vec::with_capacity(self.len());
        for (k, v) in self.elements.iter() {
            let mut reader_k = Reader::new(k.as_ref());
            let key = f(&mut reader_k)?;

            let mut reader_v = Reader::new(v.as_ref());
            let value = g(&mut reader_v)?;
            output.push((key, value))
        }
        Ok(output)
    }

    pub fn owned(&self) -> MapOwned {
        MapOwned {
            len_encoding: self.len_encoding.clone(),
            elements: self
                .elements
                .iter()
                .map(|(slice1, slice2)| ((*slice1).to_owned(), (*slice2).to_owned()))
                .collect::<Vec<(CborData, CborData)>>(),
        }
    }
}

impl MapOwned {
    pub fn len(&self) -> usize {
        self.elements.len()
    }

    pub fn iter<'a>(&'a self) -> impl Iterator<Item = (Reader<'a>, Reader<'a>)> {
        self.elements.iter().map(|(k, v)| (k.read(), v.read()))
    }

    pub fn keys<'a>(&'a self) -> impl Iterator<Item = Reader<'a>> {
        self.elements.iter().map(|(k, _v)| k.read())
    }

    pub fn values<'a>(&'a self) -> impl Iterator<Item = Reader<'a>> {
        self.elements.iter().map(|(_k, v)| v.read())
    }

    pub fn to_vec<F, G, K: Decode, V: Decode>(&self, f: F, g: G) -> Result<Vec<(K, V)>, DecodeError>
    where
        F: for<'b> Fn(&mut Reader<'b>) -> Result<K, DecodeError>,
        G: for<'b> Fn(&mut Reader<'b>) -> Result<V, DecodeError>,
    {
        let mut output = Vec::with_capacity(self.len());
        for (k, v) in self.elements.iter() {
            let mut reader_k = Reader::new(k.as_ref());
            let key = f(&mut reader_k)?;

            let mut reader_v = Reader::new(v.as_ref());
            let value = g(&mut reader_v)?;
            output.push((key, value))
        }
        Ok(output)
    }

    pub fn borrow<'a>(&'a self) -> Map<'a> {
        Map {
            len_encoding: self.len_encoding.clone(),
            elements: self
                .elements
                .iter()
                .map(|(k, v)| (k.borrow(), v.borrow()))
                .collect::<Vec<(&'a CborSlice, &'a CborSlice)>>(),
        }
    }
}

impl<'a> std::ops::Index<usize> for Map<'a> {
    type Output = (&'a CborSlice, &'a CborSlice);

    fn index(&self, index: usize) -> &Self::Output {
        &self.elements[index]
    }
}

impl TagValue {
    pub fn to_u64(&self) -> u64 {
        self.0.to_u64()
    }

    pub fn from_u64(v: u64) -> Self {
        Self(Value::canonical(v))
    }
}

impl<'a> Tag<'a> {
    pub fn tag_repr(&self) -> TagValue {
        self.tag_val
    }

    pub fn value(&self) -> u64 {
        self.tag_val.to_u64()
    }

    pub fn data(&self) -> &'a CborSlice {
        &self.data
    }

    pub fn reader(&self) -> Reader<'a> {
        self.data.reader()
    }

    pub fn read_data<F, T>(&self, f: F) -> Result<T, ReaderError>
    where
        F: FnOnce(&mut Reader<'a>) -> Result<T, ReaderError>,
    {
        let mut reader: Reader<'a> = self.data.reader();
        let t = f(&mut reader)?;
        reader.expect_finished()?;
        Ok(t)
    }

    pub fn decode_data<T: Decode>(&self) -> Result<T, DecodeError> {
        let mut reader: Reader<'a> = self.data.reader();
        let t = <T>::decode(&mut reader)?;
        reader.expect_finished()?;
        Ok(t)
    }

    pub fn owned(&self) -> TagOwned {
        TagOwned {
            tag_val: self.tag_val,
            data: self.data.to_owned(),
        }
    }
}

impl TagOwned {
    pub fn value(&self) -> u64 {
        self.tag_val.to_u64()
    }

    pub fn data(&self) -> &CborData {
        &self.data
    }

    pub fn read_data<'a>(&'a self) -> Reader<'a> {
        self.data.read()
    }

    pub fn borrow<'a>(&'a self) -> Tag<'a> {
        Tag {
            tag_val: self.tag_val,
            data: self.data.borrow(),
        }
    }
}