libipld_cbor/
lib.rs

1//! CBOR codec.
2#![deny(missing_docs)]
3#![deny(warnings)]
4
5use core::convert::TryFrom;
6use libipld_core::codec::{Codec, Decode, Encode};
7pub use libipld_core::error::{Result, UnsupportedCodec};
8
9pub mod cbor;
10pub mod decode;
11pub mod encode;
12pub mod error;
13
14/// CBOR codec.
15#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
16pub struct DagCborCodec;
17
18impl Codec for DagCborCodec {}
19
20impl From<DagCborCodec> for u64 {
21    fn from(_: DagCborCodec) -> Self {
22        0x71
23    }
24}
25
26impl TryFrom<u64> for DagCborCodec {
27    type Error = UnsupportedCodec;
28
29    fn try_from(_: u64) -> core::result::Result<Self, Self::Error> {
30        Ok(Self)
31    }
32}
33
34/// Marker trait for types supporting the `DagCborCodec`.
35pub trait DagCbor: Encode<DagCborCodec> + Decode<DagCborCodec> {}
36
37impl<T: Encode<DagCborCodec> + Decode<DagCborCodec>> DagCbor for T {}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42    use libipld_core::cid::Cid;
43    use libipld_core::codec::assert_roundtrip;
44    use libipld_core::ipld::Ipld;
45    use libipld_core::multihash::{Code, MultihashDigest};
46    use libipld_macro::ipld;
47    use std::collections::HashSet;
48
49    #[test]
50    fn test_encode_decode_cbor() {
51        let cid = Cid::new_v1(0, Code::Blake3_256.digest(&b"cid"[..]));
52        let ipld = ipld!({
53          "number": 1,
54          "list": [true, null, false],
55          "bytes": vec![0, 1, 2, 3],
56          "map": { "float": 0.0, "string": "hello" },
57          "link": cid,
58        });
59        let bytes = DagCborCodec.encode(&ipld).unwrap();
60        let ipld2 = DagCborCodec.decode(&bytes).unwrap();
61        assert_eq!(ipld, ipld2);
62    }
63
64    #[test]
65    fn test_references() {
66        let cid = Cid::new_v1(0, Code::Blake3_256.digest(&b"0"[..]));
67        let ipld = ipld!({
68            "list": [true, cid],
69        });
70        let bytes = DagCborCodec.encode(&ipld).unwrap();
71        let mut set = HashSet::new();
72        DagCborCodec
73            .references::<Ipld, _>(&bytes, &mut set)
74            .unwrap();
75        assert!(set.contains(&cid));
76    }
77
78    #[test]
79    fn test_encode_max() {
80        assert_roundtrip(DagCborCodec, &i8::MAX, &Ipld::Integer(i8::MAX as i128));
81        assert_roundtrip(DagCborCodec, &i16::MAX, &Ipld::Integer(i16::MAX as i128));
82        assert_roundtrip(DagCborCodec, &i32::MAX, &Ipld::Integer(i32::MAX as i128));
83        assert_roundtrip(DagCborCodec, &i64::MAX, &Ipld::Integer(i64::MAX as i128));
84        assert_roundtrip(DagCborCodec, &u8::MAX, &Ipld::Integer(u8::MAX as i128));
85        assert_roundtrip(DagCborCodec, &u16::MAX, &Ipld::Integer(u16::MAX as i128));
86        assert_roundtrip(DagCborCodec, &u32::MAX, &Ipld::Integer(u32::MAX as i128));
87        assert_roundtrip(DagCborCodec, &u64::MAX, &Ipld::Integer(u64::MAX as i128));
88    }
89
90    #[test]
91    fn test_encode_min() {
92        assert_roundtrip(DagCborCodec, &i8::MIN, &Ipld::Integer(i8::MIN as i128));
93        assert_roundtrip(DagCborCodec, &i16::MIN, &Ipld::Integer(i16::MIN as i128));
94        assert_roundtrip(DagCborCodec, &i32::MIN, &Ipld::Integer(i32::MIN as i128));
95        assert_roundtrip(DagCborCodec, &i64::MIN, &Ipld::Integer(i64::MIN as i128));
96        assert_roundtrip(DagCborCodec, &u8::MIN, &Ipld::Integer(u8::MIN as i128));
97        assert_roundtrip(DagCborCodec, &u16::MIN, &Ipld::Integer(u16::MIN as i128));
98        assert_roundtrip(DagCborCodec, &u32::MIN, &Ipld::Integer(u32::MIN as i128));
99        assert_roundtrip(DagCborCodec, &u64::MIN, &Ipld::Integer(u64::MIN as i128));
100    }
101}