use dcbor::prelude::*;
use crate::{ parse_dcbor_item, ParseError };
use thiserror::Error;
#[derive(Debug, Error, Clone, PartialEq)]
#[rustfmt::skip]
pub enum Error {
#[error("Invalid odd map length")]
OddMapLength,
#[error("Invalid CBOR item: {0}")]
ParseError(#[from] ParseError),
}
pub type Result<T> = std::result::Result<T, Error>;
pub fn compose_dcbor_array(array: &[&str]) -> Result<CBOR> {
let mut result = Vec::new();
for item in array {
let cbor = parse_dcbor_item(item)?;
result.push(cbor);
}
Ok(result.into())
}
pub fn compose_dcbor_map(array: &[&str]) -> Result<CBOR> {
if array.len() % 2 != 0 {
return Err(Error::OddMapLength);
}
let mut map = Map::new();
for i in (0..array.len()).step_by(2) {
let key = parse_dcbor_item(array[i])?;
let value = parse_dcbor_item(array[i + 1])?;
map.insert(key, value);
}
Ok(map.into())
}