1// SPDX-License-Identifier: Apache-2.0
2
3extern crate alloc;
4
5use ciborium::{de::from_reader, ser::into_writer, tag::*, value::Value};
6use rstest::rstest;
7use serde::{de::DeserializeOwned, Serialize};
8
9use core::fmt::Debug;
10
11#[rstest(item, bytes, value, encode, success,
12 case(Captured(Some(6), true), "c6f5", Value::Tag(6, Value::Bool(true).into()), true, true),
13 case(Captured(None, true), "f5", Value::Bool(true), true, true),
14
15 case(Required::<_, 6>(true), "c6f5", Value::Tag(6, Value::Bool(true).into()), true, true),
16 case(Required::<_, 6>(true), "c7f5", Value::Tag(7, Value::Bool(true).into()), false, false),
17 case(Required::<_, 6>(true), "f5", Value::Bool(true), false, false),
18
19 case(Accepted::<_, 6>(true), "c6f5", Value::Tag(6, Value::Bool(true).into()), true, true),
20 case(Accepted::<_, 6>(true), "c7f5", Value::Tag(7, Value::Bool(true).into()), false, false),
21 case(Accepted::<_, 6>(true), "f5", Value::Bool(true), false, true),
22)]
23fn test<T: Serialize + DeserializeOwned + Debug + Eq>(
24 item: T,
25 bytes: &str,
26 value: Value,
27 encode: bool,
28 success: bool,
29) {
30 let bytes = hex::decode(bytes).unwrap();
31
32 if encode {
33 // Encode into bytes
34 let mut encoded = Vec::new();
35 into_writer(&item, &mut encoded).unwrap();
36 assert_eq!(bytes, encoded);
37
38 // Encode into value
39 assert_eq!(value, Value::serialized(&item).unwrap());
40 }
41
42 // Decode from bytes
43 match from_reader(&bytes[..]) {
44 Ok(x) if success => assert_eq!(item, x),
45 Ok(..) => panic!("unexpected success"),
46 Err(e) if success => Err(e).unwrap(),
47 Err(..) => (),
48 }
49
50 // Decode from value
51 match value.deserialized() {
52 Ok(x) if success => assert_eq!(item, x),
53 Ok(..) => panic!("unexpected success"),
54 Err(e) if success => Err(e).unwrap(),
55 Err(..) => (),
56 }
57}
58