Add bitvec-like generic support to the scale-bits type for use in codegen (#718)

* Add bitvec-like generic support to the scale-bits type for use in codegen

* Use nightly 1.66 formatting

* Fix reading input while decoding bit sequences

* Add tests for our DecodedBits wrapper

* Add convenience DecodedBits::(in)to_bits functions

* Don't expose DecodedBits::bit_format

* Re-export scale_bits as peer dependency

* Move subxt::utils into a separate file

* Hide DecodedBits internals

* Don't re-export types from the `bits` module

* Update subxt/src/utils/bits.rs

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Update subxt/src/utils/bits.rs

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Address review feedback

* Clarify the byte needed calculation in DecodedBits encoding

* Remove remaining dbg! invocations

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
This commit is contained in:
Igor Matuszewski
2022-11-24 15:09:20 +01:00
committed by GitHub
parent a80d6cfd30
commit f0ce26db7b
9 changed files with 273 additions and 28 deletions
+3 -2
View File
@@ -12,7 +12,7 @@ homepage = "https://www.parity.io/"
description = "Generate an API for interacting with a substrate node from FRAME metadata"
[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "full", "bit-vec"] }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "full"] }
darling = "0.14.0"
frame-metadata = "15.0.0"
heck = "0.4.0"
@@ -20,7 +20,7 @@ proc-macro2 = "1.0.24"
proc-macro-error = "1.0.4"
quote = "1.0.8"
syn = "1.0.58"
scale-info = { version = "2.0.0", features = ["bit-vec"] }
scale-info = "2.0.0"
subxt-metadata = { version = "0.25.0", path = "../metadata" }
jsonrpsee = { version = "0.16.0", features = ["async-client", "client-ws-transport", "http-client"] }
hex = "0.4.3"
@@ -28,4 +28,5 @@ tokio = { version = "1.8", features = ["macros", "rt-multi-thread"] }
[dev-dependencies]
bitvec = { version = "1.0.0", default-features = false, features = ["alloc"] }
scale-info = { version = "2.0.0", features = ["bit-vec"] }
pretty_assertions = "1.0.0"
+2 -2
View File
@@ -161,11 +161,11 @@ impl RuntimeGenerator {
let mut type_substitutes = [
(
"bitvec::order::Lsb0",
parse_quote!(#crate_path::ext::bitvec::order::Lsb0),
parse_quote!(#crate_path::utils::bits::Lsb0),
),
(
"bitvec::order::Msb0",
parse_quote!(#crate_path::ext::bitvec::order::Msb0),
parse_quote!(#crate_path::utils::bits::Msb0),
),
(
"sp_core::crypto::AccountId32",
+15 -3
View File
@@ -745,10 +745,22 @@ fn generate_bitvec() {
registry.register_type(&meta_type::<S>());
let portable_types: PortableRegistry = registry.into();
let substitutes = [
(
String::from("bitvec::order::Lsb0"),
parse_quote!(::subxt_path::utils::bits::Lsb0),
),
(
String::from("bitvec::order::Msb0"),
parse_quote!(::subxt_path::utils::bits::Msb0),
),
]
.into();
let type_gen = TypeGenerator::new(
&portable_types,
"root",
Default::default(),
substitutes,
DerivesRegistry::new(&"::subxt_path".into()),
"::subxt_path".into(),
);
@@ -762,8 +774,8 @@ fn generate_bitvec() {
use super::root;
#[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, Debug)]
pub struct S {
pub lsb: ::subxt_path::ext::bitvec::vec::BitVec<::core::primitive::u8, root::bitvec::order::Lsb0>,
pub msb: ::subxt_path::ext::bitvec::vec::BitVec<::core::primitive::u16, root::bitvec::order::Msb0>,
pub lsb: ::subxt_path::utils::bits::DecodedBits<::core::primitive::u8, ::subxt_path::utils::bits::Lsb0>,
pub msb: ::subxt_path::utils::bits::DecodedBits<::core::primitive::u16, ::subxt_path::utils::bits::Msb0>,
}
}
}
+1 -1
View File
@@ -246,7 +246,7 @@ impl TypePathType {
bit_store_type,
crate_path,
} => {
let type_path = parse_quote! { #crate_path::ext::bitvec::vec::BitVec<#bit_store_type, #bit_order_type> };
let type_path = parse_quote! { #crate_path::utils::bits::DecodedBits<#bit_store_type, #bit_order_type> };
syn::Type::Path(type_path)
}
}