chore(deps): Upgrade to minicbor 0.17 (breaking changes) (#109)

This commit is contained in:
Santiago Carmuega 2022-06-02 09:49:55 -03:00 committed by GitHub
parent a13a305d4f
commit 3260abcb98
18 changed files with 653 additions and 490 deletions

View file

@ -4,6 +4,6 @@ pub use minicbor;
/// Round-trip friendly common helper structs
pub mod utils;
pub trait Fragment: Sized + for<'b> minicbor::Decode<'b> + minicbor::Encode {}
pub trait Fragment: Sized + for<'b> minicbor::Decode<'b, ()> + minicbor::Encode<()> {}
impl<T> Fragment for T where T: for<'b> minicbor::Decode<'b> + minicbor::Encode + Sized {}
impl<T> Fragment for T where T: for<'b> minicbor::Decode<'b, ()> + minicbor::Encode<()> + Sized {}

View file

@ -6,8 +6,11 @@ use minicbor::{data::Tag, Decode, Encode};
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord)]
pub struct SkipCbor<const N: usize> {}
impl<'b, const N: usize> minicbor::Decode<'b> for SkipCbor<N> {
fn decode(d: &mut minicbor::Decoder<'b>) -> Result<Self, minicbor::decode::Error> {
impl<'b, C, const N: usize> minicbor::Decode<'b, C> for SkipCbor<N> {
fn decode(
d: &mut minicbor::Decoder<'b>,
_ctx: &mut C,
) -> Result<Self, minicbor::decode::Error> {
{
let probe = d.probe();
println!("skipped cbor value {}: {:?}", N, probe.datatype()?);
@ -18,10 +21,11 @@ impl<'b, const N: usize> minicbor::Decode<'b> for SkipCbor<N> {
}
}
impl<const N: usize> minicbor::Encode for SkipCbor<N> {
impl<C, const N: usize> minicbor::Encode<C> for SkipCbor<N> {
fn encode<W: minicbor::encode::Write>(
&self,
_e: &mut minicbor::Encoder<W>,
_ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
todo!()
}
@ -50,15 +54,15 @@ impl<K, V> Deref for KeyValuePairs<K, V> {
}
}
impl<'b, K, V> minicbor::decode::Decode<'b> for KeyValuePairs<K, V>
impl<'b, C, K, V> minicbor::decode::Decode<'b, C> for KeyValuePairs<K, V>
where
K: Encode + Decode<'b>,
V: Encode + Decode<'b>,
K: Encode<C> + Decode<'b, C>,
V: Encode<C> + Decode<'b, C>,
{
fn decode(d: &mut minicbor::Decoder<'b>) -> Result<Self, minicbor::decode::Error> {
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
let datatype = d.datatype()?;
let items: Result<Vec<_>, _> = d.map_iter::<K, V>()?.collect();
let items: Result<Vec<_>, _> = d.map_iter_with::<C, K, V>(ctx)?.collect();
let items = items?;
match datatype {
@ -71,30 +75,31 @@ where
}
}
impl<K, V> minicbor::encode::Encode for KeyValuePairs<K, V>
impl<C, K, V> minicbor::encode::Encode<C> for KeyValuePairs<K, V>
where
K: Encode,
V: Encode,
K: Encode<C>,
V: Encode<C>,
{
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut minicbor::Encoder<W>,
ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
match self {
KeyValuePairs::Def(x) => {
e.map(x.len() as u64)?;
for (k, v) in x.iter() {
k.encode(e)?;
v.encode(e)?;
k.encode(e, ctx)?;
v.encode(e, ctx)?;
}
}
KeyValuePairs::Indef(x) => {
e.begin_map()?;
for (k, v) in x.iter() {
k.encode(e)?;
v.encode(e)?;
k.encode(e, ctx)?;
v.encode(e, ctx)?;
}
e.end()?;
@ -123,16 +128,16 @@ impl<A> Deref for MaybeIndefArray<A> {
}
}
impl<'b, A> minicbor::decode::Decode<'b> for MaybeIndefArray<A>
impl<'b, C, A> minicbor::decode::Decode<'b, C> for MaybeIndefArray<A>
where
A: minicbor::decode::Decode<'b>,
A: minicbor::decode::Decode<'b, C>,
{
fn decode(d: &mut minicbor::Decoder<'b>) -> Result<Self, minicbor::decode::Error> {
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
let datatype = d.datatype()?;
match datatype {
minicbor::data::Type::Array => Ok(Self::Def(d.decode()?)),
minicbor::data::Type::ArrayIndef => Ok(Self::Indef(d.decode()?)),
minicbor::data::Type::Array => Ok(Self::Def(d.decode_with(ctx)?)),
minicbor::data::Type::ArrayIndef => Ok(Self::Indef(d.decode_with(ctx)?)),
_ => Err(minicbor::decode::Error::message(
"unknown data type of maybe indef array",
)),
@ -140,17 +145,18 @@ where
}
}
impl<A> minicbor::encode::Encode for MaybeIndefArray<A>
impl<C, A> minicbor::encode::Encode<C> for MaybeIndefArray<A>
where
A: minicbor::encode::Encode,
A: minicbor::encode::Encode<C>,
{
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut minicbor::Encoder<W>,
ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
match self {
MaybeIndefArray::Def(x) => {
e.encode(x)?;
e.encode_with(x, ctx)?;
}
// TODO: this seemed necesary on alonzo, but breaks on byron. We need to double check.
//MaybeIndefArray::Indef(x) if x.is_empty() => {
@ -160,7 +166,7 @@ where
e.begin_array()?;
for v in x.iter() {
e.encode(v)?;
e.encode_with(v, ctx)?;
}
e.end()?;
@ -191,30 +197,31 @@ impl<P> Deref for OrderPreservingProperties<P> {
}
}
impl<'b, P> minicbor::decode::Decode<'b> for OrderPreservingProperties<P>
impl<'b, C, P> minicbor::decode::Decode<'b, C> for OrderPreservingProperties<P>
where
P: Decode<'b>,
P: Decode<'b, C>,
{
fn decode(d: &mut minicbor::Decoder<'b>) -> Result<Self, minicbor::decode::Error> {
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
let len = d.map()?.unwrap_or_default();
let components: Result<_, _> = (0..len).map(|_| d.decode()).collect();
let components: Result<_, _> = (0..len).map(|_| d.decode_with(ctx)).collect();
Ok(Self(components?))
}
}
impl<P> minicbor::encode::Encode for OrderPreservingProperties<P>
impl<C, P> minicbor::encode::Encode<C> for OrderPreservingProperties<P>
where
P: Encode,
P: Encode<C>,
{
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut minicbor::Encoder<W>,
ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
e.map(self.0.len() as u64)?;
for component in &self.0 {
e.encode(component)?;
e.encode_with(component, ctx)?;
}
Ok(())
@ -225,28 +232,29 @@ where
#[derive(Debug)]
pub struct CborWrap<T>(pub T);
impl<'b, T> minicbor::Decode<'b> for CborWrap<T>
impl<'b, C, T> minicbor::Decode<'b, C> for CborWrap<T>
where
T: minicbor::Decode<'b>,
T: minicbor::Decode<'b, C>,
{
fn decode(d: &mut minicbor::Decoder<'b>) -> Result<Self, minicbor::decode::Error> {
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
d.tag()?;
let cbor = d.bytes()?;
let wrapped = minicbor::decode(cbor)?;
let wrapped = minicbor::decode_with(cbor, ctx)?;
Ok(CborWrap(wrapped))
}
}
impl<T> minicbor::Encode for CborWrap<T>
impl<C, T> minicbor::Encode<C> for CborWrap<T>
where
T: minicbor::Encode,
T: minicbor::Encode<C>,
{
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut minicbor::Encoder<W>,
ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
let buf = minicbor::to_vec(&self.0).map_err(|_| {
let buf = minicbor::to_vec_with(&self.0, ctx).map_err(|_| {
minicbor::encode::Error::message("error encoding cbor-wrapped structure")
})?;
@ -274,27 +282,28 @@ impl<I, const T: u64> TagWrap<I, T> {
}
}
impl<'b, I, const T: u64> minicbor::Decode<'b> for TagWrap<I, T>
impl<'b, C, I, const T: u64> minicbor::Decode<'b, C> for TagWrap<I, T>
where
I: minicbor::Decode<'b>,
I: minicbor::Decode<'b, C>,
{
fn decode(d: &mut minicbor::Decoder<'b>) -> Result<Self, minicbor::decode::Error> {
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
d.tag()?;
Ok(TagWrap(d.decode()?))
Ok(TagWrap(d.decode_with(ctx)?))
}
}
impl<I, const T: u64> minicbor::Encode for TagWrap<I, T>
impl<C, I, const T: u64> minicbor::Encode<C> for TagWrap<I, T>
where
I: minicbor::Encode,
I: minicbor::Encode<C>,
{
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut minicbor::Encoder<W>,
ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
e.tag(Tag::Unassigned(T))?;
e.encode(&self.0)?;
e.encode_with(&self.0, ctx)?;
Ok(())
}
@ -306,17 +315,21 @@ where
#[derive(Debug)]
pub struct EmptyMap;
impl<'b> minicbor::decode::Decode<'b> for EmptyMap {
fn decode(d: &mut minicbor::Decoder<'b>) -> Result<Self, minicbor::decode::Error> {
impl<'b, C> minicbor::decode::Decode<'b, C> for EmptyMap {
fn decode(
d: &mut minicbor::Decoder<'b>,
_ctx: &mut C,
) -> Result<Self, minicbor::decode::Error> {
d.skip()?;
Ok(EmptyMap)
}
}
impl minicbor::encode::Encode for EmptyMap {
impl<C> minicbor::encode::Encode<C> for EmptyMap {
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut minicbor::Encoder<W>,
_ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
e.map(0)?;
@ -340,16 +353,16 @@ impl<T> Deref for ZeroOrOneArray<T> {
}
}
impl<'b, T> minicbor::decode::Decode<'b> for ZeroOrOneArray<T>
impl<'b, C, T> minicbor::decode::Decode<'b, C> for ZeroOrOneArray<T>
where
T: Decode<'b>,
T: Decode<'b, C>,
{
fn decode(d: &mut minicbor::Decoder<'b>) -> Result<Self, minicbor::decode::Error> {
fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result<Self, minicbor::decode::Error> {
let len = d.array()?;
match len {
Some(0) => Ok(ZeroOrOneArray(None)),
Some(1) => Ok(ZeroOrOneArray(Some(d.decode()?))),
Some(1) => Ok(ZeroOrOneArray(Some(d.decode_with(ctx)?))),
Some(_) => Err(minicbor::decode::Error::message(
"found invalid len for zero-or-one pattern",
)),
@ -360,18 +373,19 @@ where
}
}
impl<T> minicbor::encode::Encode for ZeroOrOneArray<T>
impl<C, T> minicbor::encode::Encode<C> for ZeroOrOneArray<T>
where
T: minicbor::Encode,
T: minicbor::Encode<C>,
{
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut minicbor::Encoder<W>,
ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
match &self.0 {
Some(x) => {
e.array(1)?;
e.encode(x)?;
e.encode_with(x, ctx)?;
}
None => {
e.array(0)?;
@ -392,8 +406,11 @@ pub enum AnyUInt {
U64(u64),
}
impl<'b> minicbor::decode::Decode<'b> for AnyUInt {
fn decode(d: &mut minicbor::Decoder<'b>) -> Result<Self, minicbor::decode::Error> {
impl<'b, C> minicbor::decode::Decode<'b, C> for AnyUInt {
fn decode(
d: &mut minicbor::Decoder<'b>,
_ctx: &mut C,
) -> Result<Self, minicbor::decode::Error> {
match d.datatype()? {
minicbor::data::Type::U8 => match d.u8()? {
x @ 0..=0x17 => Ok(AnyUInt::MajorByte(x)),
@ -409,39 +426,60 @@ impl<'b> minicbor::decode::Decode<'b> for AnyUInt {
}
}
impl minicbor::encode::Encode for AnyUInt {
impl<C> minicbor::encode::Encode<C> for AnyUInt {
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut minicbor::Encoder<W>,
_ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
match self {
AnyUInt::MajorByte(x) => {
let b = &x.to_be_bytes()[..];
e.encode(minicbor::data::Cbor::from(b))?;
e.writer_mut()
.write_all(b)
.map_err(minicbor::encode::Error::write)?;
Ok(())
}
AnyUInt::U8(x) => {
let x = x.to_be_bytes();
let b = &[[24u8], x].concat()[..];
e.encode(minicbor::data::Cbor::from(b))?;
e.writer_mut()
.write_all(b)
.map_err(minicbor::encode::Error::write)?;
Ok(())
}
AnyUInt::U16(x) => {
let x = &x.to_be_bytes()[..];
let b = &[&[25u8], x].concat()[..];
e.encode(minicbor::data::Cbor::from(b))?;
e.writer_mut()
.write_all(b)
.map_err(minicbor::encode::Error::write)?;
Ok(())
}
AnyUInt::U32(x) => {
let x = &x.to_be_bytes()[..];
let b = &[&[26u8], x].concat()[..];
e.encode(minicbor::data::Cbor::from(b))?;
e.writer_mut()
.write_all(b)
.map_err(minicbor::encode::Error::write)?;
Ok(())
}
AnyUInt::U64(x) => {
let x = &x.to_be_bytes()[..];
let b = &[&[27u8], x].concat()[..];
e.encode(minicbor::data::Cbor::from(b))?;
e.writer_mut()
.write_all(b)
.map_err(minicbor::encode::Error::write)?;
Ok(())
}
}