Skip to content

Commit

Permalink
Adjust repo to scale-info pub interface (#16)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
  • Loading branch information
lexnv committed May 16, 2023
1 parent 9164b12 commit 81f5343
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion scale-decode/examples/enum_decode_as_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,5 @@ fn make_type<T: scale_info::TypeInfo + 'static>() -> (u32, scale_info::PortableR
let id = types.register_type(&m);
let portable_registry: scale_info::PortableRegistry = types.into();

(id.id(), portable_registry)
(id.id, portable_registry)
}
2 changes: 1 addition & 1 deletion scale-decode/examples/struct_decode_as_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,5 @@ fn make_type<T: scale_info::TypeInfo + 'static>() -> (u32, scale_info::PortableR
let id = types.register_type(&m);
let portable_registry: scale_info::PortableRegistry = types.into();

(id.id(), portable_registry)
(id.id, portable_registry)
}
2 changes: 1 addition & 1 deletion scale-decode/examples/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,5 +311,5 @@ fn make_type<T: scale_info::TypeInfo + 'static>() -> (u32, scale_info::PortableR
let id = types.register_type(&m);
let portable_registry: scale_info::PortableRegistry = types.into();

(id.id(), portable_registry)
(id.id, portable_registry)
}
10 changes: 5 additions & 5 deletions scale-decode/src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ macro_rules! decode_inner_type_when_one_tuple_entry {
// a tuple or composite value. Else, fall back to default behaviour.
// This ensures that this function strictly improves on the default
// which would be to fail.
let inner_type_id = match ty.type_def() {
let inner_type_id = match ty.type_def {
scale_info::TypeDef::Composite(_) => {
return DecodeAsTypeResult::Skipped(self);
}
Expand Down Expand Up @@ -698,7 +698,7 @@ mod test {
let id = types.register_type(&m);
let portable_registry: scale_info::PortableRegistry = types.into();

(id.id(), portable_registry)
(id.id, portable_registry)
}

// For most of our tests, we'll assert that whatever type we encode, we can decode back again to the given type.
Expand Down Expand Up @@ -756,12 +756,12 @@ mod test {

let (ty, types) = make_type::<Foo>();

let new_foo = match types.resolve(ty).unwrap().type_def() {
let new_foo = match &types.resolve(ty).unwrap().type_def {
scale_info::TypeDef::Composite(c) => {
Foo::decode_as_fields(foo_encoded_cursor, c.fields(), &types).unwrap()
Foo::decode_as_fields(foo_encoded_cursor, c.fields.as_slice(), &types).unwrap()
}
scale_info::TypeDef::Tuple(t) => {
Foo::decode_as_field_ids(foo_encoded_cursor, t.fields(), &types).unwrap()
Foo::decode_as_field_ids(foo_encoded_cursor, t.fields.as_slice(), &types).unwrap()
}
_ => {
panic!("Expected composite or tuple type def")
Expand Down
26 changes: 13 additions & 13 deletions scale-decode/src/visitor/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ pub fn decode_with_visitor<'scale, 'info, V: Visitor>(

let ty = types.resolve(ty_id).ok_or(DecodeError::TypeIdNotFound(ty_id))?;
let ty_id = TypeId(ty_id);
let path = ty.path();
let path = &ty.path;

match ty.type_def() {
match &ty.type_def {
TypeDef::Composite(inner) => {
decode_composite_value(data, ty_id, path, inner, types, visitor)
}
Expand All @@ -69,7 +69,7 @@ fn decode_composite_value<'scale, 'info, V: Visitor>(
types: &'info PortableRegistry,
visitor: V,
) -> Result<V::Value<'scale, 'info>, V::Error> {
let mut items = Composite::new(data, path, ty.fields(), types);
let mut items = Composite::new(data, path, &ty.fields, types);
let res = visitor.visit_composite(&mut items, ty_id);

// Skip over any bytes that the visitor chose not to decode:
Expand Down Expand Up @@ -104,7 +104,7 @@ fn decode_sequence_value<'scale, 'info, V: Visitor>(
types: &'info PortableRegistry,
visitor: V,
) -> Result<V::Value<'scale, 'info>, V::Error> {
let mut items = Sequence::new(data, ty.type_param().id(), types)?;
let mut items = Sequence::new(data, ty.type_param.id, types)?;
let res = visitor.visit_sequence(&mut items, ty_id);

// Skip over any bytes that the visitor chose not to decode:
Expand All @@ -121,8 +121,8 @@ fn decode_array_value<'scale, 'info, V: Visitor>(
types: &'info PortableRegistry,
visitor: V,
) -> Result<V::Value<'scale, 'info>, V::Error> {
let len = ty.len() as usize;
let mut arr = Array::new(data, ty.type_param().id(), len, types);
let len = ty.len as usize;
let mut arr = Array::new(data, ty.type_param.id, len, types);
let res = visitor.visit_array(&mut arr, ty_id);

// Skip over any bytes that the visitor chose not to decode:
Expand All @@ -139,7 +139,7 @@ fn decode_tuple_value<'scale, 'info, V: Visitor>(
types: &'info PortableRegistry,
visitor: V,
) -> Result<V::Value<'scale, 'info>, V::Error> {
let mut items = Tuple::new(data, ty.fields(), types);
let mut items = Tuple::new(data, ty.fields.as_slice(), types);
let res = visitor.visit_tuple(&mut items, ty_id);

// Skip over any bytes that the visitor chose not to decode:
Expand Down Expand Up @@ -254,7 +254,7 @@ fn decode_compact_value<'scale, 'info, V: Visitor>(
visitor: V,
) -> Result<V::Value<'scale, 'info>, V::Error> {
use TypeDefPrimitive::*;
match inner.type_def() {
match &inner.type_def {
// It's obvious how to decode basic primitive unsigned types, since we have impls for them.
TypeDef::Primitive(U8) => {
locations.push(CompactLocation::Primitive(current_type_id));
Expand Down Expand Up @@ -288,22 +288,22 @@ fn decode_compact_value<'scale, 'info, V: Visitor>(
}
// A struct with exactly 1 field containing one of the above types can be sensibly compact encoded/decoded.
TypeDef::Composite(composite) => {
if composite.fields().len() != 1 {
if composite.fields.len() != 1 {
return Err(DecodeError::CannotDecodeCompactIntoType(inner.clone()).into());
}

// What type is the 1 field that we are able to decode?
let field = &composite.fields()[0];
let field = &composite.fields[0];

// Record this composite location.
match field.name() {
match &field.name {
Some(name) => {
locations.push(CompactLocation::NamedComposite(current_type_id, name))
}
None => locations.push(CompactLocation::UnnamedComposite(current_type_id)),
}

let field_type_id = field.ty().id();
let field_type_id = field.ty.id;
let inner_ty = types
.resolve(field_type_id)
.ok_or(DecodeError::TypeIdNotFound(field_type_id))?;
Expand All @@ -328,7 +328,7 @@ fn decode_compact_value<'scale, 'info, V: Visitor>(
}

// The type ID of the thing encoded into a Compact type.
let inner_ty_id = ty.type_param().id();
let inner_ty_id = ty.type_param.id;

// Attempt to compact-decode this inner type.
let inner = types.resolve(inner_ty_id).ok_or(DecodeError::TypeIdNotFound(inner_ty_id))?;
Expand Down
2 changes: 1 addition & 1 deletion scale-decode/src/visitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ mod test {
let id = types.register_type(&m);
let portable_registry: PortableRegistry = types.into();

(id.id(), portable_registry)
(id.id, portable_registry)
}

/// This just tests that if we try to decode some values we've encoded using a visitor
Expand Down
12 changes: 6 additions & 6 deletions scale-decode/src/visitor/types/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl<'scale, 'info> Composite<'scale, 'info> {
}
/// Return whether any of the fields are unnamed.
pub fn has_unnamed_fields(&self) -> bool {
self.fields.iter().any(|f| f.name().is_none())
self.fields.iter().any(|f| f.name.is_none())
}
/// Convert the remaining fields in this Composite type into a [`super::Tuple`]. This allows them to
/// be parsed in the same way as a tuple type, discarding name information.
Expand All @@ -80,7 +80,7 @@ impl<'scale, 'info> Composite<'scale, 'info> {
/// Return the name of the next field to be decoded; `None` if either the field has no name,
/// or there are no fields remaining.
pub fn peek_name(&self) -> Option<&'info str> {
self.fields.get(0).and_then(|f| f.name().map(|n| &**n))
self.fields.get(0).and_then(|f| f.name.as_deref())
}
/// Decode the next field in the composite type by providing a visitor to handle it. This is more
/// efficient than iterating over the key/value pairs if you already know how you want to decode the
Expand All @@ -97,7 +97,7 @@ impl<'scale, 'info> Composite<'scale, 'info> {
let b = &mut &*self.item_bytes;

// Decode the bytes:
let res = crate::visitor::decode_with_visitor(b, field.ty().id(), self.types, visitor);
let res = crate::visitor::decode_with_visitor(b, field.ty.id, self.types, visitor);

// Update self to point to the next item, now:
self.item_bytes = *b;
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<'scale, 'info> CompositeField<'scale, 'info> {
}
/// The type ID associated with this field.
pub fn type_id(&self) -> u32 {
self.field.ty().id()
self.field.ty.id
}
/// Decode this field using a visitor.
pub fn decode_with_visitor<V: Visitor>(
Expand All @@ -158,14 +158,14 @@ impl<'scale, 'info> CompositeField<'scale, 'info> {
) -> Result<V::Value<'scale, 'info>, V::Error> {
crate::visitor::decode_with_visitor(
&mut &*self.bytes,
self.field.ty().id(),
self.field.ty.id,
self.types,
visitor,
)
}
/// Decode this field into a specific type via [`DecodeAsType`].
pub fn decode_as_type<T: DecodeAsType>(&self) -> Result<T, crate::Error> {
T::decode_as_type(&mut &*self.bytes, self.field.ty().id(), self.types)
T::decode_as_type(&mut &*self.bytes, self.field.ty.id, self.types)
}
}

Expand Down
4 changes: 2 additions & 2 deletions scale-decode/src/visitor/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ impl<'info> TupleFieldIds<'info> {
}
fn first(&self) -> Option<u32> {
match self {
TupleFieldIds::Ids(fs) => fs.get(0).map(|f| f.id()),
TupleFieldIds::Fields(fs) => fs.get(0).map(|f| f.ty().id()),
TupleFieldIds::Ids(fs) => fs.get(0).map(|f| f.id),
TupleFieldIds::Fields(fs) => fs.get(0).map(|f| f.ty.id),
}
}
fn pop_front_unwrap(&mut self) {
Expand Down
10 changes: 5 additions & 5 deletions scale-decode/src/visitor/types/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ impl<'scale, 'info> Variant<'scale, 'info> {

// Does a variant exist with the index we're looking for?
let variant = ty
.variants()
.variants
.iter()
.find(|v| v.index() == index)
.find(|v| v.index == index)
.ok_or_else(|| DecodeError::VariantNotFound(index, ty.clone()))?;

// Allow decoding of the fields:
let fields = Composite::new(item_bytes, path, variant.fields(), types);
let fields = Composite::new(item_bytes, path, variant.fields.as_slice(), types);

Ok(Variant { bytes, variant, fields })
}
Expand All @@ -66,11 +66,11 @@ impl<'scale, 'info> Variant<'scale, 'info> {
}
/// The name of the variant.
pub fn name(&self) -> &'info str {
self.variant.name()
self.variant.name.as_str()
}
/// The index of the variant.
pub fn index(&self) -> u8 {
self.variant.index()
self.variant.index
}
/// Access the variant fields.
pub fn fields(&mut self) -> &mut Composite<'scale, 'info> {
Expand Down

0 comments on commit 81f5343

Please sign in to comment.