| 1 | #[cfg(feature = "tags")] |
| 2 | mod tagtests { |
| 3 | use serde_cbor_2::value::Value; |
| 4 | use serde_cbor_2::{from_slice, to_vec}; |
| 5 | |
| 6 | fn decode_hex(s: &str) -> std::result::Result<Vec<u8>, std::num::ParseIntError> { |
| 7 | (0..s.len()) |
| 8 | .step_by(2) |
| 9 | .map(|i| u8::from_str_radix(&s[i..i + 2], 16)) |
| 10 | .collect() |
| 11 | } |
| 12 | |
| 13 | // get bytes from http://cbor.me/ trees |
| 14 | fn parse_cbor_me(example: &str) -> std::result::Result<Vec<u8>, std::num::ParseIntError> { |
| 15 | let hex = example |
| 16 | .split('\n') |
| 17 | .flat_map(|line| line.split('#').take(1)) |
| 18 | .collect::<Vec<&str>>() |
| 19 | .join("") |
| 20 | .replace(' ', ""); |
| 21 | decode_hex(&hex) |
| 22 | } |
| 23 | |
| 24 | #[test] |
| 25 | fn tagged_cbor_roundtrip() { |
| 26 | let data = r#" |
| 27 | C1 # tag(1) |
| 28 | 82 # array(2) |
| 29 | C2 # tag(2) |
| 30 | 63 # text(3) |
| 31 | 666F6F # "foo" |
| 32 | C3 # tag(3) |
| 33 | A1 # map(1) |
| 34 | C4 # tag(4) |
| 35 | 61 # text(1) |
| 36 | 61 # "a" |
| 37 | C5 # tag(5) |
| 38 | 61 # text(1) |
| 39 | 62 # "b" |
| 40 | "#; |
| 41 | let bytes1 = parse_cbor_me(data).unwrap(); |
| 42 | let value1: Value = from_slice(&bytes1).unwrap(); |
| 43 | let bytes2 = to_vec(&value1).unwrap(); |
| 44 | let value2: Value = from_slice(&bytes2).unwrap(); |
| 45 | assert_eq!(bytes1, bytes2); |
| 46 | assert_eq!(value1, value2); |
| 47 | } |
| 48 | } |