Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Value representation of Simple values #137

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ciborium-ll/src/hdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ impl TryFrom<Title> for Header {

Title(Major::Other, Minor::More) => Self::Break,
Title(Major::Other, Minor::This(x)) => Self::Simple(x),
Title(Major::Other, Minor::Next1(x)) => Self::Simple(x[0]),
// Two-byte sequences for simple values below 32 are not considered well-formed
Title(Major::Other, Minor::Next1([0..=31])) => Err(InvalidError(()))?,
Title(Major::Other, Minor::Next1([x])) => Self::Simple(x),
Title(Major::Other, Minor::Next2(x)) => Self::Float(f16::from_be_bytes(x).into()),
Title(Major::Other, Minor::Next4(x)) => Self::Float(f32::from_be_bytes(x).into()),
Title(Major::Other, Minor::Next8(x)) => Self::Float(f64::from_be_bytes(x)),
Expand Down Expand Up @@ -140,6 +142,11 @@ impl From<Header> for Title {

Header::Simple(x) => match x {
x @ 0..=23 => Title(Major::Other, Minor::This(x)),
// Simple(24) is irrepresentable
// Simple(25) to Simple(27) are floats
// Simple(28) to Simple(30) are reserved
// Should this panic or error (i.e. use TryFrom instead of From) ?
// See: https://www.rfc-editor.org/rfc/rfc8949.html#section-3.3
x => Title(Major::Other, Minor::Next1([x])),
},

Expand Down
11 changes: 9 additions & 2 deletions ciborium-ll/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,19 @@ mod tests {
(Header::Float(INFINITY), "fb7ff0000000000000", false),
(Header::Float(NAN), "fb7ff8000000000000", false),
(Header::Float(-INFINITY), "fbfff0000000000000", false),
(Header::Simple(0), "e0", true),
(Header::Simple(1), "e1", true),
(Header::Simple(16), "f0", true),
(Header::Simple(17), "f1", true),
(Header::Simple(18), "f2", true),
(Header::Simple(19), "f3", true),
(Header::Simple(simple::FALSE), "f4", true),
(Header::Simple(simple::TRUE), "f5", true),
(Header::Simple(simple::NULL), "f6", true),
(Header::Simple(simple::UNDEFINED), "f7", true),
(Header::Simple(16), "f0", true),
(Header::Simple(24), "f818", true),
(Header::Break, "ff", true),
(Header::Simple(32), "f820", true),
(Header::Simple(64), "f840", true),
(Header::Simple(255), "f8ff", true),
(Header::Tag(0), "c0", true),
(Header::Tag(1), "c1", true),
Expand Down
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
Loading