Skip to content

Commit

Permalink
fix: Encode EmptyCaveat as {} instead of null
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Oct 18, 2023
1 parent eb58bbe commit 7da838e
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/semantics/caveat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fmt;
use downcast_rs::{impl_downcast, Downcast};
use dyn_clone::{clone_trait_object, DynClone};
use erased_serde::serialize_trait_object;
use serde::{Deserialize, Serialize};
use serde::{de::Visitor, ser::SerializeMap, Deserialize, Serialize};

/// A caveat defined as part of a semantics
pub trait Caveat: Send + Sync + DynClone + Downcast + erased_serde::Serialize + 'static {
Expand All @@ -27,7 +27,7 @@ impl fmt::Debug for dyn Caveat {
}

/// A caveat that is always valid
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct EmptyCaveat;

impl Caveat for EmptyCaveat {
Expand All @@ -53,3 +53,42 @@ impl Caveat for Box<dyn Caveat> {
(**self).is_valid_attenuation(other)
}
}

impl<'de> Deserialize<'de> for EmptyCaveat {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct NoFieldsVisitor;

impl<'de> Visitor<'de> for NoFieldsVisitor {
type Value = EmptyCaveat;

fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("an empty object")
}

fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: serde::de::MapAccess<'de>,
{
if let Some(field) = map.next_key()? {
return Err(serde::de::Error::unknown_field(field, &[]));
}

Ok(EmptyCaveat)
}
}

deserializer.deserialize_map(NoFieldsVisitor)
}
}

impl Serialize for EmptyCaveat {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_map(Some(0))?.end()
}
}

0 comments on commit 7da838e

Please sign in to comment.