Skip to content

Commit f48bbb3

Browse files
committed
Fix CBOR ser/de for Monad anc Coll.
Signed-off-by: Ionut Mihalcea <[email protected]>
1 parent f9de484 commit f48bbb3

File tree

4 files changed

+23
-31
lines changed

4 files changed

+23
-31
lines changed

src/cmw.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ impl CMW {
130130
}
131131
}
132132

133-
pub(crate) fn unmarshal_from_decoder(decoder: &mut Decoder<'_>) -> Result<Self, Error> {
133+
pub(crate) fn unmarshal_from_cbor_decoder(decoder: &mut Decoder<'_>) -> Result<Self, Error> {
134134
// Decode the first byte to determine the type
135135
let start = decoder.input()[decoder.position()];
136136
if start_cbor_record(start) || start_cbor_tag(start) {
137137
Monad::unmarshal_from_cbor_decoder(decoder).map(|m| m.into())
138138
} else if start_cbor_collection(start) {
139-
Collection::unmarshal_from_decoder(decoder).map(|c| c.into())
139+
Collection::unmarshal_from_cbor_decoder(decoder).map(|c| c.into())
140140
} else {
141141
Err(Error::InvalidData(format!(
142142
"want CBOR map, CBOR array or CBOR Tag start symbols, got: 0x{:02x}",
@@ -166,7 +166,7 @@ impl CMW {
166166

167167
impl<'b> Decode<'b, ()> for CMW {
168168
fn decode(d: &mut Decoder<'b>, _ctx: &mut ()) -> Result<Self, minicbor::decode::Error> {
169-
CMW::unmarshal_from_decoder(d).map_err(|e| {
169+
CMW::unmarshal_from_cbor_decoder(d).map_err(|e| {
170170
minicbor::decode::Error::custom(Error::InvalidData(format!(
171171
"Failed to decode CMW from bytes: {e}"
172172
)))

src/collection.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,19 @@ impl FromStr for Type {
9999
}
100100

101101
/// The Collection structure.
102-
#[derive(Clone, Debug, PartialEq)]
102+
#[derive(Clone, Debug)]
103103
pub struct Collection {
104104
cmap: BTreeMap<Label, CMW>,
105105
ctyp: Option<Type>,
106106
pub(crate) format: Option<Format>,
107107
}
108108

109+
impl PartialEq for Collection {
110+
fn eq(&self, other: &Self) -> bool {
111+
self.ctyp == other.ctyp && self.cmap == other.cmap
112+
}
113+
}
114+
109115
impl Collection {
110116
/// Create new collection with optional type.
111117
pub fn new(ctyp: Option<Type>, format: Option<Format>) -> Result<Self, Error> {
@@ -273,10 +279,10 @@ impl Collection {
273279
/// Deserialize from CBOR bytes.
274280
pub fn unmarshal_cbor(b: &[u8]) -> Result<Self, Error> {
275281
let mut decoder = Decoder::new(b);
276-
Collection::unmarshal_from_decoder(&mut decoder)
282+
Self::unmarshal_from_cbor_decoder(&mut decoder)
277283
}
278284

279-
pub(crate) fn unmarshal_from_decoder(decoder: &mut Decoder<'_>) -> Result<Self, Error> {
285+
pub(crate) fn unmarshal_from_cbor_decoder(decoder: &mut Decoder<'_>) -> Result<Self, Error> {
280286
let mut col = Collection::new(None, None)?;
281287
let _ = decoder
282288
.map()

src/monad.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -239,24 +239,8 @@ impl Monad {
239239

240240
/// CBOR deserialization.
241241
pub fn unmarshal_cbor(b: &[u8]) -> Result<Self, Error> {
242-
if b.is_empty() {
243-
return Err(Error::InvalidData("empty buffer".into()));
244-
}
245-
let first = b[0];
246-
if start_cbor_record(first) {
247-
let mut decoder = Decoder::new(b);
248-
// Decode array
249-
Monad::unmarshal_record_from_cbor_decoder(&mut decoder)
250-
} else if start_cbor_tag(first) {
251-
let mut decoder = Decoder::new(b);
252-
// Decode tag
253-
Monad::unmarshal_tag_from_cbor_decoder(&mut decoder)
254-
} else {
255-
Err(Error::InvalidData(format!(
256-
"want CBOR map, CBOR array or CBOR Tag start symbols, got: 0x{:02x}",
257-
first
258-
)))
259-
}
242+
let mut decoder = Decoder::new(b);
243+
Self::unmarshal_from_cbor_decoder(&mut decoder)
260244
}
261245

262246
pub(crate) fn unmarshal_from_cbor_decoder(decoder: &mut Decoder<'_>) -> Result<Self, Error> {

tests/lib.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ fn test_parse_cbor_collection() {
2929
}
3030

3131
#[test]
32-
#[ignore = "Fails due to items being arranged in a different order"]
3332
fn test_encode_cbor_collection() {
3433
// Read testdata file as binary data
3534
let data = fs::read("testdata/collection-cbor-ok.cbor")
3635
.expect("Failed to read testdata/collection-cbor-ok.cbor");
3736

37+
// Parse the file contents into a CMW instance.
38+
let parsed_cmw: CMW = CMW::unmarshal_cbor(&data).expect("Failed to parse CBOR into CMW");
39+
3840
let cmw_1 = Monad::new_media_type(
3941
Mime::from_str("application/signed-corim+cbor")
4042
.expect("Failed to create media type for CMW 1"),
@@ -44,7 +46,7 @@ fn test_encode_cbor_collection() {
4446
.expect("Failed to create Monad");
4547

4648
let mut cmw_2 =
47-
Monad::new_cf(30001, vec![0x23, 0x47, 0xda, 0x55], None).expect("Failed to create Monad 2");
49+
Monad::new_cf(29884, vec![0x23, 0x47, 0xda, 0x55], None).expect("Failed to create Monad 2");
4850
cmw_2.use_cbor_tag_format();
4951

5052
let cmw_3 =
@@ -60,13 +62,13 @@ fn test_encode_cbor_collection() {
6062
collection
6163
.add_item(cmw::collection::Label::Str("s".into()), cmw_3.into())
6264
.expect("Failed to add item3");
63-
let cmw = CMW::Collection(collection);
64-
let data_encoded = cmw.marshal_cbor().expect("Failed to marshal CMW to CBOR");
65+
let constructed_cmw = CMW::Collection(collection);
6566

6667
// Ensure the encoded data matches the original data
67-
assert_eq!(data, data_encoded, "Encoded data does not match original");
68-
println!("Encoded CMW: {:?}", data_encoded);
69-
println!("Parsed CMW: {:?}", data);
68+
assert_eq!(
69+
parsed_cmw, constructed_cmw,
70+
"Parsed and constructed CMWs don't match"
71+
);
7072
}
7173

7274
#[test]

0 commit comments

Comments
 (0)