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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//! This module is experimental!

use super::TypeError;
use crate::{Cbor, Encoder, ItemKind, TaggedItem, Writer};
use std::{borrow::Cow, collections::BTreeMap, error::Error};

#[derive(Debug)]
pub enum CodecError {
    TypeError(TypeError),
    TupleSize { expected: usize, found: usize },
    Custom(Box<dyn Error + Send + Sync>),
    String(String),
}

impl CodecError {
    pub fn type_error(target: &'static str, item: &TaggedItem<'_>) -> Self {
        Self::TypeError(TypeError {
            target,
            kind: item.kind().into(),
            tags: item.tags().into(),
        })
    }

    pub fn tuple_size(expected: usize, found: usize) -> Self {
        Self::TupleSize { expected, found }
    }

    pub fn custom(err: impl Error + Send + Sync + 'static) -> Self {
        Self::Custom(Box::new(err))
    }

    pub fn str(err: impl Into<String>) -> Self {
        Self::String(err.into())
    }
}

impl std::fmt::Display for CodecError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CodecError::TypeError(e) => write!(f, "{}", e),
            CodecError::TupleSize { expected, found } => write!(
                f,
                "wrong tuple size: expected {}, found {}",
                expected, found
            ),
            CodecError::Custom(err) => write!(f, "codec error: {}", err),
            CodecError::String(err) => write!(f, "codec error: {}", err),
        }
    }
}
impl Error for CodecError {}

impl From<TypeError> for CodecError {
    fn from(te: TypeError) -> Self {
        Self::TypeError(te)
    }
}

pub type Result<T> = std::result::Result<T, CodecError>;

pub trait WriteCbor {
    fn write_cbor<W: Writer>(&self, w: W) -> W::Output;
}

pub trait ReadCbor {
    fn fmt(f: &mut impl std::fmt::Write) -> std::fmt::Result;

    fn name() -> String {
        let mut s = String::new();
        Self::fmt(&mut s).unwrap();
        s
    }

    fn read_cbor(cbor: &Cbor) -> Result<Self>
    where
        Self: Sized;
}

pub trait ReadCborBorrowed<'a>: ToOwned + 'a {
    fn read_cbor_borrowed(cbor: &'a Cbor) -> Result<Cow<'a, Self>>;
}

impl<T: WriteCbor> WriteCbor for Vec<T> {
    fn write_cbor<W: Writer>(&self, w: W) -> W::Output {
        w.encode_array(|mut b| {
            for item in self {
                b = item.write_cbor(b);
            }
        })
    }
}

impl<T: ReadCbor> ReadCbor for Vec<T> {
    fn fmt(f: &mut impl std::fmt::Write) -> std::fmt::Result {
        write!(f, "Vec<")?;
        T::fmt(f)?;
        write!(f, ">")?;
        Ok(())
    }

    fn read_cbor(cbor: &Cbor) -> Result<Self>
    where
        Self: Sized,
    {
        let d = cbor.decode();
        let a = d
            .as_array()
            .ok_or_else(|| CodecError::type_error("Vec", &cbor.tagged_item()))?;
        let mut v = Vec::with_capacity(a.len());
        for item in a {
            v.push(T::read_cbor(item.as_ref())?);
        }
        Ok(v)
    }
}

impl WriteCbor for Vec<u8> {
    fn write_cbor<W: Writer>(&self, w: W) -> W::Output {
        w.encode_bytes(self)
    }
}

impl WriteCbor for [u8] {
    fn write_cbor<W: Writer>(&self, w: W) -> W::Output {
        w.encode_bytes(self)
    }
}

impl<'a> ReadCborBorrowed<'a> for [u8] {
    fn read_cbor_borrowed(cbor: &'a Cbor) -> Result<Cow<'a, Self>> {
        cbor.decode()
            .to_bytes()
            .ok_or_else(|| CodecError::type_error("byte slice", &cbor.tagged_item()))
    }
}

impl ReadCbor for Vec<u8> {
    fn read_cbor(cbor: &Cbor) -> Result<Self>
    where
        Self: Sized,
    {
        Ok(<[u8]>::read_cbor_borrowed(cbor)?.into_owned())
    }

    fn fmt(f: &mut impl std::fmt::Write) -> std::fmt::Result {
        write!(f, "Vec<u8>")
    }
}

impl WriteCbor for String {
    fn write_cbor<W: Writer>(&self, w: W) -> W::Output {
        w.encode_str(self)
    }
}

impl<'a> WriteCbor for &'a str {
    fn write_cbor<W: Writer>(&self, w: W) -> W::Output {
        w.encode_str(self)
    }
}

impl<'a> ReadCborBorrowed<'a> for str {
    fn read_cbor_borrowed(cbor: &'a Cbor) -> Result<Cow<'a, Self>> {
        cbor.decode()
            .to_str()
            .ok_or_else(|| CodecError::type_error("String", &cbor.tagged_item()))
    }
}

impl ReadCbor for String {
    fn fmt(f: &mut impl std::fmt::Write) -> std::fmt::Result {
        write!(f, "String")
    }

    fn read_cbor(cbor: &Cbor) -> Result<Self>
    where
        Self: Sized,
    {
        Ok(str::read_cbor_borrowed(cbor)?.into_owned())
    }
}

impl<T: WriteCbor> WriteCbor for Option<T> {
    fn write_cbor<W: Writer>(&self, w: W) -> W::Output {
        if let Some(this) = self {
            this.write_cbor(w)
        } else {
            w.encode_null()
        }
    }
}

impl<T: ReadCbor> ReadCbor for Option<T> {
    fn read_cbor(cbor: &Cbor) -> Result<Self>
    where
        Self: Sized,
    {
        if let ItemKind::Null = cbor.tagged_item().kind() {
            Ok(None)
        } else {
            Ok(Some(T::read_cbor(cbor)?))
        }
    }

    fn fmt(f: &mut impl std::fmt::Write) -> std::fmt::Result {
        write!(f, "Option<")?;
        T::fmt(f)?;
        write!(f, ">")?;
        Ok(())
    }
}

impl<K: WriteCbor, V: WriteCbor> WriteCbor for BTreeMap<K, V> {
    fn write_cbor<W: Writer>(&self, w: W) -> W::Output {
        w.encode_dict(|w| {
            for (k, v) in self {
                w.with_cbor_key(|w| k.write_cbor(w), |w| v.write_cbor(w));
            }
        })
    }
}

impl<K: ReadCbor + Ord, V: ReadCbor> ReadCbor for BTreeMap<K, V> {
    fn read_cbor(cbor: &Cbor) -> Result<Self>
    where
        Self: Sized,
    {
        let mut map = BTreeMap::new();
        for (k, v) in cbor
            .decode()
            .to_dict()
            .ok_or_else(|| CodecError::type_error("BTreeMap", &cbor.tagged_item()))?
        {
            map.insert(K::read_cbor(k.as_ref())?, V::read_cbor(v.as_ref())?);
        }
        Ok(map)
    }

    fn fmt(f: &mut impl std::fmt::Write) -> std::fmt::Result {
        write!(f, "BTreeMap<")?;
        K::fmt(f)?;
        write!(f, ", ")?;
        V::fmt(f)?;
        write!(f, ">")?;
        Ok(())
    }
}

impl WriteCbor for u64 {
    fn write_cbor<W: Writer>(&self, w: W) -> W::Output {
        w.encode_u64(*self)
    }
}

impl ReadCbor for u64 {
    fn read_cbor(cbor: &Cbor) -> Result<Self>
    where
        Self: Sized,
    {
        let item = cbor.tagged_item();
        match item.kind() {
            ItemKind::Pos(x) => Ok(x),
            _ => Err(CodecError::type_error("u64", &item)),
        }
    }

    fn fmt(f: &mut impl std::fmt::Write) -> std::fmt::Result {
        write!(f, "u64")
    }
}

macro_rules! tuple {
    ($($t:ident),+) => {
        impl<$($t: WriteCbor),*> WriteCbor for ($($t),*) {
            #[allow(unused_assignments, non_snake_case)]
            fn write_cbor<W: Writer>(&self, w: W) -> W::Output {
                w.encode_array(|mut w| {
                    let ($($t),*) = self;
                    $(w = $t.write_cbor(w);)*
                })
            }
        }
        impl<$($t: ReadCbor),*> ReadCbor for ($($t),*) {
            #[allow(unused_assignments, non_snake_case)]
            fn read_cbor(cbor: &Cbor) -> Result<Self> {
                let d = cbor.decode().to_array().ok_or_else(|| CodecError::type_error("tuple", &cbor.tagged_item()))?;
                let len = $({const $t: usize = 1; $t}+)* 0;
                if d.len() < len {
                    return Err(CodecError::tuple_size(len, d.len()));
                }
                let mut idx = 0;
                $(
                    let $t = $t::read_cbor(d[idx].as_ref())?;
                    idx += 1;
                )*
                Ok(($($t),*))
            }

            fn fmt(f: &mut impl ::std::fmt::Write) -> std::fmt::Result {
                write!(f, "(")?;
                $(
                    $t::fmt(f)?;
                    write!(f, ", ")?;
                )*
                write!(f, ")")?;
                Ok(())
            }
        }
    };
}

tuple!(T0, T1);
tuple!(T0, T1, T2);
tuple!(T0, T1, T2, T3);
tuple!(T0, T1, T2, T3, T4);
tuple!(T0, T1, T2, T3, T4, T5);
tuple!(T0, T1, T2, T3, T4, T5, T6);
tuple!(T0, T1, T2, T3, T4, T5, T6, T7);
tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8);
tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9);

impl<T: ?Sized + WriteCbor> WriteCbor for &T {
    fn write_cbor<W: Writer>(&self, w: W) -> W::Output {
        (*self).write_cbor(w)
    }
}

#[macro_export]
macro_rules! cbor_via {
    ($t:ty => $u:ty: |$x:pat| -> $xx:expr, |$y:pat| -> $yy:expr) => {
        impl $crate::codec::WriteCbor for $t {
            fn write_cbor<W: $crate::Writer>(&self, w: W) -> W::Output {
                let $x: &$t = self;
                let u: $u = $xx;
                $crate::codec::WriteCbor::write_cbor(&u, w)
            }
        }
        impl $crate::codec::ReadCbor for $t {
            fn read_cbor(cbor: &$crate::Cbor) -> $crate::codec::Result<Self>
            where
                Self: Sized,
            {
                let $y = <$u>::read_cbor(cbor)?;
                $yy
            }

            fn fmt(f: &mut impl ::std::fmt::Write) -> std::fmt::Result {
                write!(f, stringify!($t))
            }
        }
    };
    ($t:ty => $u:ty: INTO, $($rest:tt)*) => {
        cbor_via!($t => $u: |x| -> x.into(), $($rest)*);
    };
    ($t:ty => $u:ty: |$x:ident| -> $xx:expr, FROM) => {
        cbor_via!($t => $u: |$x| -> $xx, |x| -> Ok(x.into()));
    };
    ($t:ty => $u:ty) => {
        cbor_via!($t => $u: INTO, FROM);
    };
}

#[cfg(feature = "libipld14")]
mod impl_libipld14 {
    use super::*;
    use libipld14::{
        cbor::DagCborCodec,
        prelude::{Codec, Encode},
        store::StoreParams,
        Block, Cid, Ipld,
    };
    use smallvec::SmallVec;

    impl WriteCbor for Cid {
        fn write_cbor<W: Writer>(&self, w: W) -> W::Output {
            let mut bytes = SmallVec::<[u8; 128]>::new();
            self.write_bytes(&mut bytes).expect("writing to SmallVec");
            w.write_bytes_chunked([&[0][..], &*bytes], [42])
        }
    }

    impl ReadCbor for Cid {
        fn fmt(f: &mut impl std::fmt::Write) -> std::fmt::Result {
            write!(f, "Cid")
        }

        fn read_cbor(cbor: &Cbor) -> Result<Self>
        where
            Self: Sized,
        {
            let decoded = cbor.tagged_item();
            if let (Some(42), ItemKind::Bytes(b)) = (decoded.tags().single(), decoded.kind()) {
                let b = b.as_cow();
                if b.is_empty() {
                    Err(CodecError::str("Cid cannot be empty"))
                } else if b[0] != 0 {
                    Err(CodecError::str("Cid must use identity encoding"))
                } else {
                    Cid::read_bytes(&b[1..]).map_err(CodecError::custom)
                }
            } else {
                Err(CodecError::type_error("Cid", &decoded))
            }
        }
    }

    impl<S: StoreParams> WriteCbor for Block<S> {
        fn write_cbor<W: Writer>(&self, w: W) -> W::Output {
            (self.cid(), self.data()).write_cbor(w)
        }
    }

    impl<S: StoreParams> ReadCbor for Block<S> {
        fn fmt(f: &mut impl std::fmt::Write) -> std::fmt::Result {
            write!(f, "Block")
        }

        fn read_cbor(cbor: &Cbor) -> Result<Self>
        where
            Self: Sized,
        {
            let (cid, data) = <(Cid, Vec<u8>)>::read_cbor(cbor)?;
            Self::new(cid, data).map_err(|err| CodecError::str(err.to_string()))
        }
    }

    impl WriteCbor for Ipld {
        fn write_cbor<W: Writer>(&self, mut w: W) -> W::Output {
            w.bytes(|b| self.encode(DagCborCodec, b))
                .expect("WriteCbor for Ipld");
            w.into_output()
        }
    }

    impl ReadCbor for Ipld {
        fn fmt(f: &mut impl std::fmt::Write) -> std::fmt::Result {
            write!(f, "Ipld")
        }

        fn read_cbor(cbor: &Cbor) -> Result<Self>
        where
            Self: Sized,
        {
            DagCborCodec
                .decode(cbor.as_slice())
                .map_err(|err| CodecError::Custom(err.into()))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::CborBuilder;
    use std::convert::TryFrom;

    #[derive(Debug, PartialEq)]
    struct X(u64);
    impl From<u64> for X {
        fn from(x: u64) -> Self {
            X(x)
        }
    }
    impl From<&X> for u64 {
        fn from(x: &X) -> Self {
            x.0
        }
    }
    mod priv1 {
        use super::X;
        cbor_via!(X => u64);
    }

    #[derive(Debug)]
    struct Z;
    impl std::fmt::Display for Z {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "Z")
        }
    }
    impl Error for Z {}
    #[derive(Debug, PartialEq)]
    struct Y(u64);
    impl TryFrom<u64> for Y {
        type Error = Z;
        fn try_from(y: u64) -> std::result::Result<Self, Z> {
            Ok(Y(y))
        }
    }
    mod priv2 {
        use crate::codec::CodecError;
        use std::convert::TryInto;

        cbor_via!(super::Y => u64: |x| -> x.0, |x| -> x.try_into().map_err(CodecError::custom));
    }

    #[test]
    fn via() {
        assert_eq!(X::name(), "X");
        let bytes = X(5).write_cbor(CborBuilder::default());
        let x = X::read_cbor(&*bytes).unwrap();
        assert_eq!(x, X(5));

        assert_eq!(Y::name(), "super::Y");
        let bytes = Y(5).write_cbor(CborBuilder::default());
        let y = Y::read_cbor(&*bytes).unwrap();
        assert_eq!(y, Y(5));
    }

    #[test]
    fn tuple() {
        assert_eq!(<(String, u64)>::name(), "(String, u64, )");
        let bytes = ("hello".to_owned(), 42u64).write_cbor(CborBuilder::default());
        let tuple = <(String, u64)>::read_cbor(&*bytes).unwrap();
        assert_eq!(tuple, ("hello".to_owned(), 42u64));
    }

    #[test]
    fn vec() {
        assert_eq!(<Vec<String>>::name(), "Vec<String>");
        let x = vec!["hello".to_owned(), "world".to_owned()];
        let bytes = x.write_cbor(CborBuilder::default());
        let vec = <Vec<String>>::read_cbor(&*bytes).unwrap();
        assert_eq!(vec, x);
    }

    #[test]
    fn option() {
        assert_eq!(<Option<String>>::name(), "Option<String>");
        let x = Some("hello".to_owned());
        let bytes = x.write_cbor(CborBuilder::default());
        let opt = <Option<String>>::read_cbor(&*bytes).unwrap();
        assert_eq!(opt, x);

        let x = None;
        let bytes = x.write_cbor(CborBuilder::default());
        let opt = <Option<String>>::read_cbor(&*bytes).unwrap();
        assert_eq!(opt, x);
    }

    #[test]
    fn int() {
        assert_eq!(u64::name(), "u64");
        let bytes = 42u64.write_cbor(CborBuilder::default());
        let x = u64::read_cbor(&*bytes).unwrap();
        assert_eq!(x, 42);
    }
}