| 1 | use serde::{de::Deserializer, ser::Serializer, Deserialize, Serialize}; |
| 2 | use serde_cbor_2::{tags::Tagged, Value}; |
| 3 | use std::error::Error; |
| 4 | |
| 5 | /// https://tools.ietf.org/html/rfc7049#section-2.4.1 |
| 6 | #[derive(Debug, PartialEq)] |
| 7 | struct Date(String); |
| 8 | |
| 9 | impl Serialize for Date { |
| 10 | fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> { |
| 11 | Tagged::new(Some(0), &self.0).serialize(s) |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | impl<'de> Deserialize<'de> for Date { |
| 16 | fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { |
| 17 | let tagged = Tagged::<String>::deserialize(deserializer)?; |
| 18 | match tagged.tag { |
| 19 | Some(0) | None => Ok(Date(tagged.value)), |
| 20 | Some(_) => Err(serde::de::Error::custom("unexpected tag")), |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /// https://tools.ietf.org/html/rfc7049#section-2.4.4.3 |
| 26 | #[derive(Debug, PartialEq)] |
| 27 | struct Uri(String); |
| 28 | |
| 29 | impl Serialize for Uri { |
| 30 | fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> { |
| 31 | Tagged::new(Some(32), &self.0).serialize(s) |
| 32 | } |
| 33 | } |
| 34 | impl<'de> Deserialize<'de> for Uri { |
| 35 | fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { |
| 36 | let tagged = Tagged::<String>::deserialize(deserializer)?; |
| 37 | match tagged.tag { |
| 38 | // allow deserialization even if there is no tag. Allows roundtrip via other formats such as json |
| 39 | Some(0) | None => Ok(Uri(tagged.value)), |
| 40 | Some(_) => Err(serde::de::Error::custom("unexpected tag")), |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | #[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 46 | struct Bookmark { |
| 47 | title: String, |
| 48 | link: Uri, |
| 49 | created: Date, |
| 50 | } |
| 51 | |
| 52 | fn main() -> Result<(), Box<dyn Error>> { |
| 53 | let bookmark = Bookmark { |
| 54 | title: "The Example Domain".into(), |
| 55 | link: Uri("http://example.org/".into()), |
| 56 | created: Date("2003-12-13T18:30:02Z".into()), |
| 57 | }; |
| 58 | |
| 59 | // serialize the struct to bytes |
| 60 | let bytes1 = serde_cbor_2::to_vec(&bookmark)?; |
| 61 | // deserialize to a serde_cbor_2::Value |
| 62 | let value1: Value = serde_cbor_2::from_slice(&bytes1)?; |
| 63 | println!("{value1:?}"); |
| 64 | // serialize the value to bytes |
| 65 | let bytes2 = serde_cbor_2::to_vec(&value1)?; |
| 66 | // deserialize to a serde_cbor_2::Value |
| 67 | let value2: Value = serde_cbor_2::from_slice(&bytes2)?; |
| 68 | println!("{value2:?}"); |
| 69 | // deserialize to a Bookmark |
| 70 | let result: Bookmark = serde_cbor_2::from_slice(&bytes2)?; |
| 71 | |
| 72 | // check that the roundtrip was successful |
| 73 | assert_eq!(value1, value2); |
| 74 | assert_eq!(bookmark, result); |
| 75 | |
| 76 | // check that going via a format that does not support tags does work |
| 77 | // let json = serde_json::to_vec(&bookmark)?; |
| 78 | // let result: Bookmark = serde_json::from_slice(&json)?; |
| 79 | // assert_eq!(bookmark, result); |
| 80 | Ok(()) |
| 81 | } |