cbor_core/
simple_value.rs1use crate::{DataType, Error, Result};
2
3#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
13#[repr(transparent)]
14pub struct SimpleValue(pub(crate) u8);
15
16impl SimpleValue {
17 pub const FALSE: Self = SimpleValue(20);
19 pub const TRUE: Self = SimpleValue(21);
21 pub const NULL: Self = SimpleValue(22);
23
24 pub fn new(value: impl TryInto<Self, Error = Error>) -> Self {
33 value.try_into().unwrap()
34 }
35
36 pub const fn from_u8(value: u8) -> Result<Self> {
43 let valid_range = value <= 23 || value >= 32;
44 if valid_range {
45 Ok(Self(value))
46 } else {
47 Err(Error::InvalidSimpleValue)
48 }
49 }
50
51 #[inline]
52 #[must_use]
53 pub const fn from_bool(value: bool) -> Self {
55 if value { Self::TRUE } else { Self::FALSE }
56 }
57
58 #[inline]
59 #[must_use]
60 pub const fn data_type(&self) -> DataType {
62 match self.0 {
63 20 | 21 => DataType::Bool,
64 22 => DataType::Null,
65 _ => DataType::Simple,
66 }
67 }
68
69 pub const fn to_bool(&self) -> Result<bool> {
76 match *self {
77 Self::FALSE => Ok(false),
78 Self::TRUE => Ok(true),
79 _ => Err(Error::InvalidSimpleValue),
80 }
81 }
82
83 #[must_use]
85 pub const fn to_u8(&self) -> u8 {
86 self.0
87 }
88}
89
90impl TryFrom<u8> for SimpleValue {
91 type Error = Error;
92
93 #[inline]
94 fn try_from(value: u8) -> std::result::Result<Self, Self::Error> {
95 Self::from_u8(value)
96 }
97}
98
99impl From<SimpleValue> for u8 {
100 #[inline]
101 fn from(value: SimpleValue) -> Self {
102 value.to_u8()
103 }
104}
105
106impl From<bool> for SimpleValue {
107 #[inline]
108 fn from(value: bool) -> Self {
109 Self::from_bool(value)
110 }
111}
112
113impl TryFrom<SimpleValue> for bool {
114 type Error = Error;
115
116 fn try_from(value: SimpleValue) -> std::result::Result<Self, Self::Error> {
117 value.to_bool()
118 }
119}
120
121impl From<()> for SimpleValue {
122 #[inline]
123 fn from(_: ()) -> Self {
124 Self::NULL
125 }
126}