Skip to content
Merged
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
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/build/templates/new/_Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
ink = { version = "4.1.0", default-features = false }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true }
scale-info = { version = "2.5", default-features = false, features = ["derive"], optional = true }

[dev-dependencies]
ink_e2e = "4.1.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/transcode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ nom = "7.1.3"
nom-supreme = { version = "0.7.0", features = ["error"] }
primitive-types = { version = "0.12.1", default-features = false, features = ["codec", "scale-info", "serde"] }
scale = { package = "parity-scale-codec", version = "3.0.0", features = ["derive"] }
scale-info = { version = "2.4.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.160", default-features = false, features = ["derive"] }
serde_json = "1.0.95"
thiserror = "1.0.40"
Expand Down
48 changes: 24 additions & 24 deletions crates/transcode/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl<'a> Decoder<'a> {
len: usize,
input: &mut &[u8],
) -> Result<Value> {
let type_id = ty.id();
let type_id = ty.id;
let ty = self.registry.resolve(type_id).ok_or_else(|| {
anyhow::anyhow!("Failed to find type with id '{}'", type_id)
})?;
Expand All @@ -105,15 +105,15 @@ impl<'a> Decoder<'a> {
ty: &Type<PortableForm>,
input: &mut &[u8],
) -> Result<Value> {
match ty.type_def() {
match &ty.type_def {
TypeDef::Composite(composite) => {
let ident = ty.path().segments().last().map(|s| s.as_str());
self.decode_composite(ident, composite.fields(), input)
let ident = ty.path.segments.last().map(|s| s.as_str());
self.decode_composite(ident, &composite.fields, input)
}
TypeDef::Tuple(tuple) => {
let mut elems = Vec::new();
for field_type in tuple.fields() {
let value = self.decode(field_type.id(), input)?;
for field_type in &tuple.fields {
let value = self.decode(field_type.id, input)?;
elems.push(value);
}
Ok(Value::Tuple(Tuple::new(
Expand All @@ -123,19 +123,19 @@ impl<'a> Decoder<'a> {
}
TypeDef::Variant(variant) => self.decode_variant_type(variant, input),
TypeDef::Array(array) => {
self.decode_seq(array.type_param(), array.len() as usize, input)
self.decode_seq(&array.type_param, array.len as usize, input)
}
TypeDef::Sequence(sequence) => {
let len = <Compact<u32>>::decode(input)?;
self.decode_seq(sequence.type_param(), len.0 as usize, input)
self.decode_seq(&sequence.type_param, len.0 as usize, input)
}
TypeDef::Primitive(primitive) => self.decode_primitive(primitive, input),
TypeDef::Compact(compact) => self.decode_compact(compact, input),
TypeDef::BitSequence(_) => {
Err(anyhow::anyhow!("bitvec decoding not yet supported"))
}
}
.context(format!("Error decoding type {}: {}", id, ty.path()))
.context(format!("Error decoding type {}: {}", id, ty.path))
}

pub fn decode_composite(
Expand All @@ -150,15 +150,15 @@ impl<'a> Decoder<'a> {
CompositeTypeFields::Named(fields) => {
let mut map = Vec::new();
for field in fields {
let value = self.decode(field.field().ty().id(), input)?;
let value = self.decode(field.field().ty.id, input)?;
map.push((Value::String(field.name().to_string()), value));
}
Ok(Value::Map(Map::new(ident, map.into_iter().collect())))
}
CompositeTypeFields::Unnamed(fields) => {
let mut tuple = Vec::new();
for field in &fields {
let value = self.decode(field.ty().id(), input)?;
let value = self.decode(field.ty.id, input)?;
tuple.push(value);
}
Ok(Value::Tuple(Tuple::new(
Expand All @@ -179,7 +179,7 @@ impl<'a> Decoder<'a> {
) -> Result<Value> {
let discriminant = input.read_byte()?;
let variant = variant_type
.variants()
.variants
.iter()
.find(|v| v.index == discriminant)
.ok_or_else(|| {
Expand All @@ -188,9 +188,9 @@ impl<'a> Decoder<'a> {

let mut named = Vec::new();
let mut unnamed = Vec::new();
for field in variant.fields() {
let value = self.decode(field.ty().id(), input)?;
if let Some(name) = field.name() {
for field in &variant.fields {
let value = self.decode(field.ty.id, input)?;
if let Some(name) = &field.name {
named.push((Value::String(name.to_owned()), value));
} else {
unnamed.push(value);
Expand All @@ -202,11 +202,11 @@ impl<'a> Decoder<'a> {
))
} else if !named.is_empty() {
Ok(Value::Map(Map::new(
Some(variant.name()),
Some(&variant.name),
named.into_iter().collect(),
)))
} else {
Ok(Value::Tuple(Tuple::new(Some(variant.name()), unnamed)))
Ok(Value::Tuple(Tuple::new(Some(&variant.name), unnamed)))
}
}

Expand Down Expand Up @@ -271,28 +271,28 @@ impl<'a> Decoder<'a> {
}
};

let type_id = compact.type_param().id();
let type_id = compact.type_param.id;
let ty = self.registry.resolve(type_id).ok_or_else(|| {
anyhow::anyhow!("Failed to resolve type with id `{:?}`", type_id)
})?;
match ty.type_def() {
match &ty.type_def {
TypeDef::Primitive(primitive) => decode_compact_primitive(primitive),
TypeDef::Composite(composite) => {
match composite.fields() {
match &composite.fields[..] {
[field] => {
let type_id = field.ty().id();
let type_id = field.ty.id;
let field_ty =
self.registry.resolve(type_id).ok_or_else(|| {
anyhow::anyhow!(
"Failed to resolve type with id `{:?}`",
type_id
)
})?;
if let TypeDef::Primitive(primitive) = field_ty.type_def() {
if let TypeDef::Primitive(primitive) = &field_ty.type_def {
let struct_ident =
ty.path().segments().last().map(|s| s.as_str());
ty.path.segments.last().map(|s| s.as_str());
let field_value = decode_compact_primitive(primitive)?;
let compact_composite = match field.name() {
let compact_composite = match &field.name {
Some(name) => {
Value::Map(Map::new(
struct_ident,
Expand Down
46 changes: 23 additions & 23 deletions crates/transcode/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,22 @@ impl<'a> Encoder<'a> {
"Encoding value `{:?}` with type id `{:?}` and definition `{:?}`",
value,
type_id,
ty.type_def(),
ty.type_def,
);
if !self.env_types.try_encode(type_id, value, output)? {
match ty.type_def() {
match &ty.type_def {
TypeDef::Composite(composite) => {
self.encode_composite(composite.fields(), value, output)
self.encode_composite(&composite.fields, value, output)
}
TypeDef::Variant(variant) => {
self.encode_variant_type(variant, value, output)
}
TypeDef::Array(array) => {
self.encode_seq(array.type_param(), value, false, output)
self.encode_seq(&array.type_param, value, false, output)
}
TypeDef::Tuple(tuple) => self.encode_tuple(tuple, value, output),
TypeDef::Sequence(sequence) => {
self.encode_seq(sequence.type_param(), value, true, output)
self.encode_seq(&sequence.type_param, value, true, output)
}
TypeDef::Primitive(primitive) => {
self.encode_primitive(primitive, value, output)
Expand All @@ -119,7 +119,7 @@ impl<'a> Encoder<'a> {
match struct_type {
CompositeTypeFields::Unnamed(fields) => {
for (field, value) in fields.iter().zip(map.values()) {
self.encode(field.ty().id(), value, output)?;
self.encode(field.ty.id, value, output)?;
}
Ok(())
}
Expand All @@ -130,7 +130,7 @@ impl<'a> Encoder<'a> {
let value = map.get_by_str(field_name).ok_or_else(|| {
anyhow::anyhow!("Missing a field named `{}`", field_name)
})?;
self.encode(named_field.field().ty().id(), value, output)
self.encode(named_field.field().ty.id, value, output)
.map_err(|e| {
anyhow::anyhow!(
"Error encoding field `{}`: {}",
Expand All @@ -147,7 +147,7 @@ impl<'a> Encoder<'a> {
match struct_type {
CompositeTypeFields::Unnamed(fields) => {
for (field, value) in fields.iter().zip(tuple.values()) {
self.encode(field.ty().id(), value, output)?;
self.encode(field.ty.id, value, output)?;
}
Ok(())
}
Expand All @@ -159,7 +159,7 @@ impl<'a> Encoder<'a> {
}
v => {
if let Ok(single_field) = fields.iter().exactly_one() {
self.encode(single_field.ty().id(), value, output)
self.encode(single_field.ty.id, value, output)
} else {
Err(anyhow::anyhow!(
"Expected a Map or a Tuple or a single Value for a composite data type, found {:?}",
Expand All @@ -178,14 +178,14 @@ impl<'a> Encoder<'a> {
) -> Result<()> {
match value {
Value::Tuple(tuple_val) => {
for (field_type, value) in tuple.fields().iter().zip(tuple_val.values()) {
self.encode(field_type.id(), value, output)?;
for (field_type, value) in tuple.fields.iter().zip(tuple_val.values()) {
self.encode(field_type.id, value, output)?;
}
Ok(())
}
v => {
if let Ok(single_field) = tuple.fields().iter().exactly_one() {
self.encode(single_field.id(), value, output)
if let Ok(single_field) = tuple.fields.iter().exactly_one() {
self.encode(single_field.id, value, output)
} else {
Err(anyhow::anyhow!(
"Expected a Tuple or a single Value for a tuple data type, found {:?}",
Expand Down Expand Up @@ -217,17 +217,17 @@ impl<'a> Encoder<'a> {
}?;

let (index, variant) = variant_def
.variants()
.variants
.iter()
.find_position(|v| v.name() == &variant_ident)
.find_position(|v| v.name == variant_ident)
.ok_or_else(|| anyhow::anyhow!("No variant '{}' found", variant_ident))?;

let index: u8 = index
.try_into()
.map_err(|_| anyhow::anyhow!("Variant index > 255"))?;
output.push_byte(index);

self.encode_composite(variant.fields(), value, output)
self.encode_composite(&variant.fields, value, output)
}

fn encode_seq<O: Output + Debug>(
Expand All @@ -243,7 +243,7 @@ impl<'a> Encoder<'a> {
Compact(values.len() as u32).encode_to(output);
}
for value in values.elems() {
self.encode(ty.id(), value, output)?;
self.encode(ty.id, value, output)?;
}
}
Value::Hex(hex) => {
Expand Down Expand Up @@ -351,27 +351,27 @@ impl<'a> Encoder<'a> {

let ty = self
.registry
.resolve(compact.type_param().id())
.resolve(compact.type_param.id)
.ok_or_else(|| {
anyhow::anyhow!(
"Failed to resolve type with id '{:?}'",
compact.type_param().id()
compact.type_param.id
)
})?;
match ty.type_def() {
match &ty.type_def {
TypeDef::Primitive(primitive) => encode_compact_primitive(primitive, value),
TypeDef::Composite(composite) => {
match composite.fields() {
match &composite.fields[..] {
[field] => {
let type_id = field.ty().id();
let type_id = field.ty.id;
let field_ty =
self.registry.resolve(type_id).ok_or_else(|| {
anyhow::anyhow!(
"Failed to resolve type with id `{:?}`",
type_id
)
})?;
if let TypeDef::Primitive(primitive) = field_ty.type_def() {
if let TypeDef::Primitive(primitive) = &field_ty.type_def {
let field_values: Vec<_> = match value {
Value::Map(map) => Ok(map.values().collect()),
Value::Tuple(tuple) => Ok(tuple.values().collect()),
Expand Down
7 changes: 2 additions & 5 deletions crates/transcode/src/env_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,14 @@ impl PathKey {
T: TypeInfo,
{
let type_info = T::type_info();
let path = type_info
.path()
.clone()
.into_portable(&mut Default::default());
let path = type_info.path.into_portable(&mut Default::default());
PathKey::from(&path)
}
}

impl From<&Path<PortableForm>> for PathKey {
fn from(path: &Path<PortableForm>) -> Self {
PathKey(path.segments().to_vec())
PathKey(path.segments.to_vec())
}
}

Expand Down
Loading