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
use crate::cbor_encodable::CBOREncodable;
use super::{cbor::CBOR, varint::{EncodeVarInt, MajorType}};
#[derive(Clone)]
pub enum Simple {
Value(u64),
False,
True,
Null,
Float(f64),
}
impl Simple {
pub fn new<T>(v: T) -> Simple where T: Into<Simple> {
v.into()
}
pub fn name(&self) -> String {
format!("{:?}", self)
}
}
impl CBOREncodable for Simple {
fn cbor(&self) -> CBOR {
CBOR::Simple(self.clone())
}
fn cbor_data(&self) -> Vec<u8> {
match self {
Self::Value(v) => v.encode_varint(MajorType::Simple),
Self::False => Simple::new(20).cbor_data(),
Self::True => Simple::new(21).cbor_data(),
Self::Null => Simple::new(22).cbor_data(),
Self::Float(v) => v.cbor_data(),
}
}
}
impl PartialEq for Simple {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Value(v1), Self::Value(v2)) => v1 == v2,
(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 std::fmt::Debug for Simple {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Self::Value(v) => format!("{:?}", v),
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 std::fmt::Display for Simple {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Self::Value(v) => format!("simple({})", v),
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 From<u64> for Simple {
fn from(value: u64) -> Self {
Simple::Value(value)
}
}
impl From<i32> for Simple {
fn from(value: i32) -> Self {
Simple::Value(value as u64)
}
}