Skip to content

Commit

Permalink
Bump to scale-type-resolver 0.2 and prep for 0.12 release (#52)
Browse files Browse the repository at this point in the history
* Updates for scale-type-resolver 0.2

* paths are now 'resolver lifetimes

* Bump to 0.12.0, bump scale-bits and scale-type-resolver, and prep for release

* clippy fixes
  • Loading branch information
jsdw authored Apr 29, 2024
1 parent bd0f3e3 commit 34d45a7
Show file tree
Hide file tree
Showing 17 changed files with 273 additions and 211 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ The format is based on [Keep a Changelog].

[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

## 0.12.0 - 2024-04-29

Update the `scale-type-resolver` dependency to 0.2.0 (and bump `scale-bits` for the same reason).

The main changes here are:
- Type IDs are now passed by value rather than reference.
- The `Composite` type handed back in the visitor's `visit_composite()` method now exposes the name and path of the composite type being decoded, if one was provided.

## 0.11.1 - 2024-02-16

- `scale-info` was still being pulled in via `scale-type-resolver`; this has now been fixed
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ members = [
resolver = "2"

[workspace.package]
version = "0.11.1"
version = "0.12.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
Expand All @@ -17,5 +17,5 @@ keywords = ["parity", "scale", "decoding"]
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"]

[workspace.dependencies]
scale-decode = { version = "0.11.1", path = "scale-decode" }
scale-decode-derive = { version = "0.11.1", path = "scale-decode-derive" }
scale-decode = { version = "0.12.0", path = "scale-decode" }
scale-decode-derive = { version = "0.12.0", path = "scale-decode-derive" }
19 changes: 10 additions & 9 deletions scale-decode-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fn generate_enum_impl(
fn visit_variant<'scale, 'info>(
self,
value: &mut #path_to_scale_decode::visitor::types::Variant<'scale, 'info, Self::TypeResolver>,
type_id: &<Self::TypeResolver as #path_to_scale_decode::TypeResolver>::TypeId,
type_id: <Self::TypeResolver as #path_to_scale_decode::TypeResolver>::TypeId,
) -> Result<Self::Value<'scale, 'info>, Self::Error> {
#(
#variant_ifs
Expand All @@ -173,7 +173,7 @@ fn generate_enum_impl(
fn visit_composite<'scale, 'info>(
self,
value: &mut #path_to_scale_decode::visitor::types::Composite<'scale, 'info, Self::TypeResolver>,
_type_id: &<Self::TypeResolver as #path_to_scale_decode::TypeResolver>::TypeId,
_type_id: <Self::TypeResolver as #path_to_scale_decode::TypeResolver>::TypeId,
) -> Result<Self::Value<'scale, 'info>, Self::Error> {
if value.remaining() != 1 {
return self.visit_unexpected(#path_to_scale_decode::visitor::Unexpected::Composite);
Expand All @@ -183,7 +183,7 @@ fn generate_enum_impl(
fn visit_tuple<'scale, 'info>(
self,
value: &mut #path_to_scale_decode::visitor::types::Tuple<'scale, 'info, Self::TypeResolver>,
_type_id: &<Self::TypeResolver as #path_to_scale_decode::TypeResolver>::TypeId,
_type_id: <Self::TypeResolver as #path_to_scale_decode::TypeResolver>::TypeId,
) -> Result<Self::Value<'scale, 'info>, Self::Error> {
if value.remaining() != 1 {
return self.visit_unexpected(#path_to_scale_decode::visitor::Unexpected::Tuple);
Expand Down Expand Up @@ -297,14 +297,14 @@ fn generate_struct_impl(
fn visit_composite<'scale, 'info>(
self,
value: &mut #path_to_scale_decode::visitor::types::Composite<'scale, 'info, Self::TypeResolver>,
type_id: &<Self::TypeResolver as #path_to_scale_decode::TypeResolver>::TypeId,
type_id: <Self::TypeResolver as #path_to_scale_decode::TypeResolver>::TypeId,
) -> Result<Self::Value<'scale, 'info>, Self::Error> {
#visit_composite_body
}
fn visit_tuple<'scale, 'info>(
self,
value: &mut #path_to_scale_decode::visitor::types::Tuple<'scale, 'info, Self::TypeResolver>,
type_id: &<Self::TypeResolver as #path_to_scale_decode::TypeResolver>::TypeId,
type_id: <Self::TypeResolver as #path_to_scale_decode::TypeResolver>::TypeId,
) -> Result<Self::Value<'scale, 'info>, Self::Error> {
#visit_tuple_body
}
Expand All @@ -317,9 +317,9 @@ fn generate_struct_impl(
types: &'info R
) -> Result<Self, #path_to_scale_decode::Error>
{
let mut composite = #path_to_scale_decode::visitor::types::Composite::new(input, fields, types, false);
let mut composite = #path_to_scale_decode::visitor::types::Composite::new(core::iter::empty(), input, fields, types, false);
use #path_to_scale_decode::{ Visitor, IntoVisitor };
let val = <#path_to_type #ty_generics>::into_visitor().visit_composite(&mut composite, &Default::default());
let val = <#path_to_type #ty_generics>::into_visitor().visit_composite(&mut composite, Default::default());

// Consume any remaining bytes and update input:
composite.skip_decoding()?;
Expand Down Expand Up @@ -357,9 +357,10 @@ fn named_field_keyvals<'f>(
true,
// For turning named fields in scale typeinfo into named fields on struct like type:
quote!(#field_ident: {
let val = *vals
let val = vals
.get(&Some(#field_name))
.ok_or_else(|| #path_to_scale_decode::Error::new(#path_to_scale_decode::error::ErrorKind::CannotFindField { name: #field_name.to_string() }))?;
.ok_or_else(|| #path_to_scale_decode::Error::new(#path_to_scale_decode::error::ErrorKind::CannotFindField { name: #field_name.to_string() }))?
.clone();
val.decode_as_type().map_err(|e| e.at_field(#field_name))?
}),
// For turning named fields in scale typeinfo into unnamed fields on tuple like type:
Expand Down
8 changes: 4 additions & 4 deletions scale-decode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ derive = ["dep:scale-decode-derive"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
scale-bits = { version = "0.5.0", default-features = false }
scale-bits = { version = "0.6.0", default-features = false }
scale-decode-derive = { workspace = true, optional = true }
primitive-types = { version = "0.12.0", optional = true, default-features = false }
smallvec = "1.10.0"
derive_more = { version = "0.99.17", default-features = false, features = ["from", "display"] }
scale-type-resolver = { version = "0.1.1", default-features = false }
scale-type-resolver = { version = "0.2.0", default-features = false }

[dev-dependencies]
scale-info = { version = "2.7.0", default-features = false, features = ["bit-vec", "derive"] }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "bit-vec"] }
bitvec = { version = "1.0.1", default-features = false }
trybuild = "1.0.72"
# Enable the scale-info feature for testing.
scale-bits = { version = "0.5.0", default-features = false, features = ["scale-info"] }
scale-bits = { version = "0.6.0", default-features = false, features = ["scale-info"] }
primitive-types = { version = "0.12.0", default-features = false, features = ["scale-info"] }
scale-type-resolver = { version = "0.1.1", default-features = false, features = ["scale-info"] }
scale-type-resolver = { version = "0.2.0", default-features = false, features = ["scale-info"] }
23 changes: 13 additions & 10 deletions scale-decode/examples/enum_decode_as_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<R: TypeResolver> Visitor for FooVisitor<R> {
fn visit_variant<'scale, 'resolver>(
self,
value: &mut scale_decode::visitor::types::Variant<'scale, 'resolver, Self::TypeResolver>,
_type_id: &TypeIdFor<Self>,
_type_id: TypeIdFor<Self>,
) -> Result<Self::Value<'scale, 'resolver>, Self::Error> {
if value.name() == "Bar" {
// Here we choose to support decoding named or unnamed fields into our Bar variant.
Expand All @@ -81,9 +81,12 @@ impl<R: TypeResolver> Visitor for FooVisitor<R> {
let vals: HashMap<Option<&str>, _> = fields
.map(|res| res.map(|item| (item.name(), item)))
.collect::<Result<_, _>>()?;
let bar = *vals.get(&Some("bar")).ok_or_else(|| {
Error::new(ErrorKind::CannotFindField { name: "bar".to_owned() })
})?;
let bar = vals
.get(&Some("bar"))
.ok_or_else(|| {
Error::new(ErrorKind::CannotFindField { name: "bar".to_owned() })
})?
.clone();
Ok(Foo::Bar { bar: bar.decode_as_type().map_err(|e| e.at_field("bar"))? })
}
} else if value.name() == "Wibble" {
Expand Down Expand Up @@ -125,30 +128,30 @@ fn main() {
let (type_id, types) = make_type::<Foo>();

// We can decode via `DecodeAsType`, which is automatically implemented:
let bar_via_decode_as_type = Foo::decode_as_type(&mut &*bar_bytes, &type_id, &types).unwrap();
let bar_via_decode_as_type = Foo::decode_as_type(&mut &*bar_bytes, type_id, &types).unwrap();
let wibble_via_decode_as_type =
Foo::decode_as_type(&mut &*wibble_bytes, &type_id, &types).unwrap();
Foo::decode_as_type(&mut &*wibble_bytes, type_id, &types).unwrap();
let empty_via_decode_as_type =
Foo::decode_as_type(&mut &*empty_bytes, &type_id, &types).unwrap();
Foo::decode_as_type(&mut &*empty_bytes, type_id, &types).unwrap();

// Or we can also manually use our `Visitor` impl:
let bar_via_visitor = scale_decode::visitor::decode_with_visitor(
&mut &*bar_bytes,
&type_id,
type_id,
&types,
FooVisitor::new(),
)
.unwrap();
let wibble_via_visitor = scale_decode::visitor::decode_with_visitor(
&mut &*wibble_bytes,
&type_id,
type_id,
&types,
FooVisitor::new(),
)
.unwrap();
let empty_via_visitor = scale_decode::visitor::decode_with_visitor(
&mut &*empty_bytes,
&type_id,
type_id,
&types,
FooVisitor::new(),
)
Expand Down
20 changes: 11 additions & 9 deletions scale-decode/examples/struct_decode_as_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<R: TypeResolver> Visitor for FooVisitor<R> {
fn visit_composite<'scale, 'resolver>(
self,
value: &mut scale_decode::visitor::types::Composite<'scale, 'resolver, Self::TypeResolver>,
type_id: &TypeIdFor<Self>,
type_id: TypeIdFor<Self>,
) -> Result<Self::Value<'scale, 'resolver>, Self::Error> {
if value.has_unnamed_fields() {
// handle it like a tuple if there are unnamed fields in it:
Expand All @@ -73,12 +73,14 @@ impl<R: TypeResolver> Visitor for FooVisitor<R> {
let vals: HashMap<Option<&str>, _> =
value.map(|res| res.map(|item| (item.name(), item))).collect::<Result<_, _>>()?;

let bar = *vals
let bar = vals
.get(&Some("bar"))
.ok_or_else(|| Error::new(ErrorKind::CannotFindField { name: "bar".to_owned() }))?;
let wibble = *vals
.ok_or_else(|| Error::new(ErrorKind::CannotFindField { name: "bar".to_owned() }))?
.clone();
let wibble = vals
.get(&Some("wibble"))
.ok_or_else(|| Error::new(ErrorKind::CannotFindField { name: "wibble".to_owned() }))?;
.ok_or_else(|| Error::new(ErrorKind::CannotFindField { name: "wibble".to_owned() }))?
.clone();

Ok(Foo {
bar: bar.decode_as_type().map_err(|e| e.at_field("bar"))?,
Expand All @@ -90,7 +92,7 @@ impl<R: TypeResolver> Visitor for FooVisitor<R> {
fn visit_tuple<'scale, 'resolver>(
self,
value: &mut scale_decode::visitor::types::Tuple<'scale, 'resolver, Self::TypeResolver>,
_type_id: &TypeIdFor<Self>,
_type_id: TypeIdFor<Self>,
) -> Result<Self::Value<'scale, 'resolver>, Self::Error> {
if value.remaining() != 2 {
return Err(Error::new(ErrorKind::WrongLength {
Expand Down Expand Up @@ -119,14 +121,14 @@ fn main() {
let (type_id, types) = make_type::<Foo>();

// We can decode via `DecodeAsType`, which is automatically implemented:
let foo_via_decode_as_type = Foo::decode_as_type(&mut &*foo_bytes, &type_id, &types).unwrap();
let foo_via_decode_as_type = Foo::decode_as_type(&mut &*foo_bytes, type_id, &types).unwrap();
// We can also attempt to decode it into any other type; we'll get an error if this fails:
let foo_via_decode_as_type_arc =
<std::sync::Arc<Foo>>::decode_as_type(&mut &*foo_bytes, &type_id, &types).unwrap();
<std::sync::Arc<Foo>>::decode_as_type(&mut &*foo_bytes, type_id, &types).unwrap();
// Or we can also manually use our `Visitor` impl:
let foo_via_visitor = scale_decode::visitor::decode_with_visitor(
&mut &*foo_bytes,
&type_id,
type_id,
&types,
FooVisitor::new(),
)
Expand Down
Loading

0 comments on commit 34d45a7

Please sign in to comment.