Skip to content

Commit

Permalink
feat: Value representation of Simple values
Browse files Browse the repository at this point in the history
Co-authored-by: Hendrik Wolff <[email protected]>
Co-authored-by: Theodor Straube <[email protected]>
Co-authored-by: moehr1z <[email protected]>
Signed-off-by: Emanuel Pilz <[email protected]>
  • Loading branch information
4 people committed Oct 22, 2024
1 parent 5ab0ccd commit 5bd0c85
Show file tree
Hide file tree
Showing 11 changed files with 667 additions and 76 deletions.
21 changes: 9 additions & 12 deletions ciborium/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,22 @@ where
Header::Simple(simple::FALSE) => self.deserialize_bool(visitor),
Header::Simple(simple::TRUE) => self.deserialize_bool(visitor),
Header::Simple(simple::NULL) => self.deserialize_option(visitor),
Header::Simple(simple::UNDEFINED) => self.deserialize_option(visitor),
h @ Header::Simple(..) => Err(h.expected("known simple value")),
Header::Simple(_) => self.recurse(|me| {
let mut simple_de =
crate::simple::SimpleDeserializer::<_, Self>::new(&mut me.decoder);
visitor.visit_newtype_struct(&mut simple_de)
}),

h @ Header::Break => Err(h.expected("non-break")),
}
}

#[inline]
fn deserialize_bool<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
loop {
let offset = self.decoder.offset();

return match self.decoder.pull()? {
Header::Tag(..) => continue,
Header::Simple(simple::FALSE) => visitor.visit_bool(false),
Header::Simple(simple::TRUE) => visitor.visit_bool(true),
_ => Err(Error::semantic(offset, "expected bool")),
};
}
self.recurse(|me| {
let mut simple_de = crate::simple::SimpleDeserializer::<_, Self>::new(&mut me.decoder);
simple_de.deserialize_bool(visitor)
})
}

#[inline]
Expand Down
1 change: 1 addition & 0 deletions ciborium/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ extern crate alloc;

pub mod de;
pub mod ser;
pub mod simple;
pub mod tag;
pub mod value;

Expand Down
10 changes: 8 additions & 2 deletions ciborium/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,16 @@ where
#[inline]
fn serialize_newtype_struct<U: ?Sized + ser::Serialize>(
self,
_name: &'static str,
name: &'static str,
value: &U,
) -> Result<(), Self::Error> {
value.serialize(self)
match name {
"@@SIMPLE@@" => match value.serialize(crate::simple::Serializer) {
Ok(x) => Ok(self.0.push(Header::Simple(x))?),
_ => Err(Error::Value("expected simple value".into())),
},
_ => value.serialize(self),
}
}

#[inline]
Expand Down
Loading

0 comments on commit 5bd0c85

Please sign in to comment.