From 14889cb91e632cadefc2a9e1930f8d006d2f494b Mon Sep 17 00:00:00 2001 From: Sergei Trofimov Date: Fri, 1 Nov 2024 10:28:15 +0000 Subject: [PATCH] fix: add Null variant to RawValue Add the missing Null variant to RawValue enum. Signed-off-by: Sergei Trofimov --- src/raw.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/raw.rs b/src/raw.rs index 6fed132..c55b5f9 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -14,6 +14,7 @@ use crate::base64::Bytes; /// deserialized raw JSON object or CBOR map #[derive(Debug, PartialEq)] pub enum RawValue { + Null, Integer(i64), Bytes(Bytes), Float(f64), @@ -30,6 +31,7 @@ impl Serialize for RawValue { S: Serializer, { match self { + Self::Null => serializer.serialize_unit(), Self::Integer(i) => serializer.serialize_i64(*i), Self::Bytes(b) => b.serialize(serializer), Self::Float(f) => serializer.serialize_f64(*f), @@ -85,6 +87,10 @@ impl<'de> Visitor<'de> for RawValueVisitor { formatter.write_str("an arbitrary JSON or CBOR structure") } + fn visit_unit(self) -> Result { + Ok(RawValue::Null) + } + fn visit_i8(self, v: i8) -> Result { Ok(RawValue::Integer(v.into())) } @@ -314,5 +320,22 @@ mod test { let rv2: RawValue = from_reader(buf.as_slice()).unwrap(); assert_eq!(rv2, rv); + + let rv = RawValue::Null; + + let val = serde_json::to_string(&rv).unwrap(); + assert_eq!("null", val); + + let rv2: RawValue = serde_json::from_str(&val).unwrap(); + assert_eq!(rv2, RawValue::Null); + + let mut buf: Vec = Vec::new(); + into_writer(&rv, &mut buf).unwrap(); + assert_eq!( + vec![ + 0xf6, // null + ], + buf + ); } }