Skip to content

Commit

Permalink
fix: add Null variant to RawValue
Browse files Browse the repository at this point in the history
Add the missing Null variant to RawValue enum.

Signed-off-by: Sergei Trofimov <sergei.trofimov@arm.com>
  • Loading branch information
setrofim committed Nov 5, 2024
1 parent 9eec506 commit 14889cb
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -85,6 +87,10 @@ impl<'de> Visitor<'de> for RawValueVisitor {
formatter.write_str("an arbitrary JSON or CBOR structure")
}

fn visit_unit<E: de::Error>(self) -> Result<Self::Value, E> {
Ok(RawValue::Null)
}

fn visit_i8<E: de::Error>(self, v: i8) -> Result<Self::Value, E> {
Ok(RawValue::Integer(v.into()))
}
Expand Down Expand Up @@ -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<u8> = Vec::new();
into_writer(&rv, &mut buf).unwrap();
assert_eq!(
vec![
0xf6, // null
],
buf
);
}
}

0 comments on commit 14889cb

Please sign in to comment.