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
import_stdlib!();

use anyhow::bail;

use crate::{CBOR, CBOREncodable, CBORDecodable, CBORError, CBORCodable, CBORCase};

use super::varint::{EncodeVarInt, MajorType};

/// A CBOR simple value.
#[derive(Clone)]
pub enum Simple {
    /// The boolean value `false`.
    False,
    /// The boolean value `true`.
    True,
    /// The value representing `null` (`None`).
    Null,
    /// A floating point value.
    Float(f64),
}

impl Simple {
    /// Returns the known name of the value, if it has been assigned one.
    pub fn name(&self) -> String {
        format!("{:?}", self)
    }
}

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

    fn cbor_data(&self) -> Vec<u8> {
        match self {
            Self::False => 20u8.encode_varint(MajorType::Simple),
            Self::True => 21u8.encode_varint(MajorType::Simple),
            Self::Null => 22u8.encode_varint(MajorType::Simple),
            Self::Float(v) => v.cbor_data(),
        }
    }
}

impl From<Simple> for CBOR {
    fn from(value: Simple) -> Self {
        value.cbor()
    }
}

impl CBORDecodable for Simple {
    fn from_cbor(cbor: &CBOR) -> anyhow::Result<Self> {
        match cbor.case() {
            CBORCase::Simple(simple) => Ok(simple.clone()),
            _ => bail!(CBORError::WrongType),
        }
    }
}

impl TryFrom<CBOR> for Simple {
    type Error = anyhow::Error;

    fn try_from(value: CBOR) -> Result<Self, Self::Error> {
        Self::from_cbor(&value)
    }
}

impl CBORCodable for Simple { }

impl PartialEq for Simple {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::False, Self::False) => true,
            (Self::True, Self::True) => true,
            (Self::Null, Self::Null) => true,
            (Self::Float(v1), Self::Float(v2)) => v1 == v2,
            _ => false,
        }
    }
}

impl fmt::Debug for Simple {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            Self::False => "false".to_owned(),
            Self::True => "true".to_owned(),
            Self::Null => "null".to_owned(),
            Self::Float(v) => format!("{:?}", v),
        };
        f.write_str(&s)
    }
}

impl fmt::Display for Simple {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            Self::False => "false".to_owned(),
            Self::True => "true".to_owned(),
            Self::Null => "null".to_owned(),
            Self::Float(v) => format!("{:?}", v),
        };
        f.write_str(&s)
    }
}