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
use crate::{CBOR, Tagged};

/// A type that can be encoded as CBOR.
pub trait CBOREncodable {
    /// Returns the value in CBOR symbolic representation.
    fn cbor(&self) -> CBOR;
    /// Returns the value in CBOR binary representation.
    fn cbor_data(&self) -> Vec<u8>;
}

impl CBOREncodable for CBOR {
    fn cbor(&self) -> CBOR {
        self.clone()
    }

    fn cbor_data(&self) -> Vec<u8> {
        match self {
            CBOR::Unsigned(x) => x.cbor_data(),
            CBOR::Negative(x) => {
                assert!(x < &0);
                x.cbor_data()
            },
            CBOR::Bytes(x) => x.cbor_data(),
            CBOR::Text(x) => x.cbor_data(),
            CBOR::Array(x) => x.cbor_data(),
            CBOR::Map(x) => x.cbor_data(),
            CBOR::Tagged(tag, item) => {
                let x = Tagged::new(tag.clone(), *item.clone());
                x.cbor_data()
            },
            CBOR::Simple(x) => x.cbor_data(),
        }
    }
}