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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
use crate::{tag::Tag, Simple, error::Error, decode::decode_cbor, CBOREncodable};
use super::{Map, string_util::flanked};
/// A symbolic representation of CBOR data.
#[derive(Clone)]
pub enum CBOR {
/// Unsigned integer (major type 0).
Unsigned(u64),
/// Negative integer (major type 1).
Negative(i64),
/// Byte string (major type 2).
ByteString(Vec<u8>),
/// UTF-8 string (major type 3).
Text(String),
/// Array (major type 4).
Array(Vec<CBOR>),
/// Map (major type 5).
Map(Map),
/// Tagged value (major type 6).
Tagged(Tag, Box<CBOR>),
/// Simple value (major type 7).
Simple(Simple)
}
/// Affordances for decoding CBOR from binary representation.
impl CBOR {
/// Decodes the given date into CBOR symbolic representation.
pub fn from_data(data: &[u8]) -> Result<CBOR, Error> {
decode_cbor(data)
}
/// Decodes the given data into CBOR symbolic representation given as a hexadecimal string.
///
/// Panics if the string is not well-formed hexadecimal with no spaces or
/// other characters.
pub fn from_hex(hex: &str) -> Result<CBOR, Error> {
let data = hex::decode(hex).unwrap();
Self::from_data(&data)
}
}
impl CBOR {
/// Create a new CBOR value representing a byte string.
pub fn byte_string<T>(data: T) -> CBOR where T: AsRef<[u8]> {
CBOR::ByteString(data.as_ref().to_vec())
}
/// Create a new CBOR value representing a byte string given as a hexadecimal string.
pub fn byte_string_hex(hex: &str) -> CBOR {
Self::byte_string(hex::decode(hex).unwrap())
}
/// Extract the CBOR value as a byte string.
///
/// Returns `Some` if the value is a byte string, `None` otherwise.
pub fn as_byte_string(&self) -> Option<&[u8]> {
match self {
Self::ByteString(b) => Some(b),
_ => None
}
}
/// Extract the CBOR value as a byte string.
///
/// Returns `Ok` if the value is a byte string, `Err` otherwise.
pub fn expect_byte_string(&self) -> Result<&[u8], Error> {
self.as_byte_string().ok_or(Error::WrongType)
}
/// Extract the CBOR value as a text string.
///
/// Returns `Some` if the value is a text string, `None` otherwise.
pub fn as_text(&self) -> Option<&str> {
match self {
Self::Text(t) => Some(t),
_ => None
}
}
/// Extract the CBOR value as a text string.
///
/// Returns `Ok` if the value is a text string, `Err` otherwise.
pub fn expect_text(&self) -> Result<&str, Error> {
self.as_text().ok_or(Error::WrongType)
}
/// Extract the CBOR value as an array.
///
/// Returns `Some` if the value is an array, `None` otherwise.
pub fn as_array(&self) -> Option<&Vec<CBOR>> {
match self {
Self::Array(a) => Some(a),
_ => None
}
}
/// Extract the CBOR value as an array.
///
/// Returns `Ok` if the value is an array, `Err` otherwise.
pub fn expect_array(&self) -> Result<&Vec<CBOR>, Error> {
self.as_array().ok_or(Error::WrongType)
}
/// Extract the CBOR value as a map.
///
/// Returns `Some` if the value is a map, `None` otherwise.
pub fn as_map(&self) -> Option<&Map> {
match self {
Self::Map(m) => Some(m),
_ => None
}
}
/// Extract the CBOR value as a map.
///
/// Returns `Ok` if the value is a map, `Err` otherwise.
pub fn expect_map(&self) -> Result<&Map, Error> {
self.as_map().ok_or(Error::WrongType)
}
/// Create a new CBOR value representing a tagged value.
pub fn tagged_value<T, I>(tag: T, item: I) -> CBOR
where T: Into<Tag>, I: CBOREncodable
{
CBOR::Tagged(tag.into(), Box::new(item.cbor()))
}
/// Extract the CBOR value as a tagged value.
///
/// Returns `Some` if the value is a tagged value, `None` otherwise.
pub fn as_tagged_value(&self) -> Option<(&Tag, &CBOR)> {
match self {
Self::Tagged(t, v) => Some((t, v)),
_ => None
}
}
/// Extract the CBOR value as a tagged value.
///
/// Returns `Ok` if the value is a tagged value with the expected tag, `Err`
/// otherwise.
pub fn expect_tagged_value<T>(&self, expected_tag: T) -> Result<&CBOR, Error>
where T: Into<Tag>
{
match self.as_tagged_value() {
Some((tag, value)) if tag == &expected_tag.into() => Ok(value),
_ => Err(Error::WrongType)
}
}
/// Extract the CBOR value as a simple value.
///
/// Returns `Some` if the value is a simple value, `None` otherwise.
pub fn as_simple_value(&self) -> Option<&Simple> {
match self {
Self::Simple(s) => Some(s),
_ => None
}
}
/// Extract the CBOR value as a simple value.
///
/// Returns `Ok` if the value is a simple value, `Err` otherwise.
pub fn expect_simple_value(&self) -> Result<&Simple, Error> {
self.as_simple_value().ok_or(Error::WrongType)
}
}
/// Associated constants for common CBOR simple values.
impl CBOR {
/// The CBOR simple value representing `false`.
pub const FALSE: CBOR = CBOR::Simple(Simple::False);
/// The CBOR simple value representing `true`.
pub const TRUE: CBOR = CBOR::Simple(Simple::True);
/// The CBOR simple value representing `null` (`None`).
pub const NULL: CBOR = CBOR::Simple(Simple::Null);
}
impl PartialEq for CBOR {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Unsigned(l0), Self::Unsigned(r0)) => l0 == r0,
(Self::Negative(l0), Self::Negative(r0)) => l0 == r0,
(Self::ByteString(l0), Self::ByteString(r0)) => l0 == r0,
(Self::Text(l0), Self::Text(r0)) => l0 == r0,
(Self::Array(l0), Self::Array(r0)) => l0 == r0,
(Self::Map(l0), Self::Map(r0)) => l0 == r0,
(Self::Tagged(l0, l1), Self::Tagged(r0, r1)) => l0 == r0 && l1 == r1,
(Self::Simple(l0), Self::Simple(r0)) => l0 == r0,
_ => false,
}
}
}
fn format_string(s: &str) -> String {
let mut result = "".to_string();
for c in s.chars() {
if c == '"' {
result.push_str(r#"\""#);
} else {
result.push(c);
}
}
flanked(&result, r#"""#, r#"""#)
}
fn format_array(a: &[CBOR]) -> String {
let s: Vec<String> = a.iter().map(|x| format!("{}", x)).collect();
flanked(&s.join(", "), "[", "]")
}
fn format_map(m: &Map) -> String {
let s: Vec<String> = m.iter().map(|x| format!("{}: {}", x.0, x.1)).collect();
flanked(&s.join(", "), "{", "}")
}
impl std::fmt::Debug for CBOR {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Unsigned(x) => f.debug_tuple("unsigned").field(x).finish(),
Self::Negative(x) => f.debug_tuple("negative").field(x).finish(),
Self::ByteString(x) => f.write_fmt(format_args!("bytes({})", hex::encode(x))),
Self::Text(x) => f.debug_tuple("text").field(x).finish(),
Self::Array(x) => f.debug_tuple("array").field(x).finish(),
Self::Map(x) => f.debug_tuple("map").field(x).finish(),
Self::Tagged(tag, item) => f.write_fmt(format_args!("tagged({}, {:?})", tag, item)),
Self::Simple(x) => f.write_fmt(format_args!("simple({})", x.name())),
}
}
}
impl std::fmt::Display for CBOR {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
CBOR::Unsigned(x) => format!("{}", x),
CBOR::Negative(x) => format!("{}", x),
CBOR::ByteString(x) => format!("h'{}'", hex::encode(x)),
CBOR::Text(x) => format_string(x),
CBOR::Array(x) => format_array(x),
CBOR::Map(x) => format_map(x),
CBOR::Tagged(tag, item) => format!("{}({})", tag, item),
CBOR::Simple(x) => format!("{}", x),
};
f.write_str(&s)
}
}