1use dcbor::prelude::*;
2use thiserror::Error;
3
4use crate::{ParseError, parse_dcbor_item};
5
6#[derive(Debug, Error, Clone, PartialEq)]
7#[rustfmt::skip]
8pub enum Error {
9 #[error("Invalid odd map length")]
10 OddMapLength,
11 #[error("Duplicate map key")]
12 DuplicateMapKey,
13 #[error("Invalid CBOR item: {0}")]
14 ParseError(#[from] ParseError),
15}
16
17pub type Result<T> = std::result::Result<T, Error>;
18
19pub fn compose_dcbor_array(array: &[&str]) -> Result<CBOR> {
32 let mut result = Vec::new();
33 for item in array {
34 let cbor = parse_dcbor_item(item)?;
35 result.push(cbor);
36 }
37 Ok(result.into())
38}
39
40pub fn compose_dcbor_map(array: &[&str]) -> Result<CBOR> {
56 if !array.len().is_multiple_of(2) {
57 return Err(Error::OddMapLength);
58 }
59
60 let mut map = Map::new();
61
62 for i in (0..array.len()).step_by(2) {
63 let key = parse_dcbor_item(array[i])?;
64 let value = parse_dcbor_item(array[i + 1])?;
65
66 if map.contains_key(key.clone()) {
68 return Err(Error::DuplicateMapKey);
69 }
70
71 map.insert(key, value);
72 }
73
74 Ok(map.into())
75}