mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
chore(codecs): contain [Struct]Flags in a mod and import what's necessary (#329)
* contain StructFlags in a mod with its imports * update code generation test
This commit is contained in:
committed by
Georgios Konstantopoulos
parent
f65969e90f
commit
13df80c1d1
@ -6,11 +6,13 @@ pub(crate) fn generate_flag_struct(ident: &Ident, fields: &FieldList) -> TokenSt
|
||||
let is_enum = fields.iter().any(|field| matches!(field, FieldTypes::EnumVariant(_)));
|
||||
|
||||
let flags_ident = format_ident!("{ident}Flags");
|
||||
let mod_flags_ident = format_ident!("{ident}_flags");
|
||||
|
||||
let mut field_flags = vec![];
|
||||
|
||||
let total_bits = if is_enum {
|
||||
field_flags.push(quote! {
|
||||
variant: B8,
|
||||
pub variant: B8,
|
||||
});
|
||||
8
|
||||
} else {
|
||||
@ -44,17 +46,26 @@ pub(crate) fn generate_flag_struct(ident: &Ident, fields: &FieldList) -> TokenSt
|
||||
|
||||
// Generate the flag struct.
|
||||
quote! {
|
||||
#[bitfield]
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
struct #flags_ident {
|
||||
#(#field_flags)*
|
||||
}
|
||||
|
||||
impl #flags_ident {
|
||||
fn from(mut buf: &[u8]) -> (Self, &[u8]) {
|
||||
(#flags_ident::from_bytes([
|
||||
#(#readable_bytes)*
|
||||
]), buf)
|
||||
pub use #mod_flags_ident::#flags_ident;
|
||||
mod #mod_flags_ident {
|
||||
use bytes::Buf;
|
||||
use modular_bitfield::prelude::*;
|
||||
|
||||
/// Fieldset that facilitates compacting the parent type.
|
||||
#[bitfield]
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct #flags_ident {
|
||||
#(#field_flags)*
|
||||
}
|
||||
|
||||
impl #flags_ident {
|
||||
/// Deserializes this fieldset and returns it, alongside the original slice in an advanced position.
|
||||
pub fn from(mut buf: &[u8]) -> (Self, &[u8]) {
|
||||
(#flags_ident::from_bytes([
|
||||
#(#readable_bytes)*
|
||||
]), buf)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -82,13 +93,13 @@ fn build_struct_field_flags(
|
||||
total_bits += bitsize;
|
||||
|
||||
field_flags.push(quote! {
|
||||
#name: #bsize ,
|
||||
pub #name: #bsize ,
|
||||
});
|
||||
} else {
|
||||
let name = format_ident!("{name}");
|
||||
|
||||
field_flags.push(quote! {
|
||||
#name: bool ,
|
||||
pub #name: bool ,
|
||||
});
|
||||
|
||||
total_bits += 1;
|
||||
@ -120,15 +131,18 @@ fn pad_flag_struct(total_bits: u8, field_flags: &mut Vec<TokenStream2>) -> u8 {
|
||||
/// Placeholder struct for when there are no bitfields to be added.
|
||||
fn placeholder_flag_struct(flags: &Ident) -> TokenStream2 {
|
||||
quote! {
|
||||
/// Placeholder struct for when there is no need for a fieldset. Doesn't actually write or read any data.
|
||||
#[derive(Debug, Default)]
|
||||
struct #flags {
|
||||
pub struct #flags {
|
||||
}
|
||||
|
||||
impl #flags {
|
||||
fn from(mut buf: &[u8]) -> (Self, &[u8]) {
|
||||
/// Placeholder: does not read any value.
|
||||
pub fn from(mut buf: &[u8]) -> (Self, &[u8]) {
|
||||
(#flags::default(), buf)
|
||||
}
|
||||
fn into_bytes(self) -> [u8; 0] {
|
||||
/// Placeholder: returns an empty array.
|
||||
pub fn into_bytes(self) -> [u8; 0] {
|
||||
[]
|
||||
}
|
||||
}
|
||||
|
||||
@ -205,25 +205,33 @@ mod tests {
|
||||
|
||||
// Expected output in a TokenStream format. Commas matter!
|
||||
let should_output = quote! {
|
||||
#[bitfield]
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
struct TestStructFlags {
|
||||
f_u64_len: B4,
|
||||
f_u256_len: B6,
|
||||
f_bool_t_len: B1,
|
||||
f_bool_f_len: B1,
|
||||
f_option_none_len: B1,
|
||||
f_option_some_len: B1,
|
||||
f_option_some_u64_len: B1,
|
||||
#[skip]
|
||||
unused: B1,
|
||||
}
|
||||
impl TestStructFlags {
|
||||
fn from(mut buf: &[u8]) -> (Self, &[u8]) {
|
||||
(
|
||||
TestStructFlags::from_bytes([buf.get_u8(), buf.get_u8(),]),
|
||||
buf
|
||||
)
|
||||
pub use TestStruct_flags::TestStructFlags;
|
||||
mod TestStruct_flags {
|
||||
use bytes::Buf;
|
||||
use modular_bitfield::prelude::*;
|
||||
|
||||
#[doc=r" Fieldset that facilitates compacting the parent type."]
|
||||
#[bitfield]
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct TestStructFlags {
|
||||
pub f_u64_len: B4,
|
||||
pub f_u256_len: B6,
|
||||
pub f_bool_t_len: B1,
|
||||
pub f_bool_f_len: B1,
|
||||
pub f_option_none_len: B1,
|
||||
pub f_option_some_len: B1,
|
||||
pub f_option_some_u64_len: B1,
|
||||
#[skip]
|
||||
unused: B1,
|
||||
}
|
||||
impl TestStructFlags {
|
||||
#[doc=r" Deserializes this fieldset and returns it, alongside the original slice in an advanced position."]
|
||||
pub fn from(mut buf: &[u8]) -> (Self, &[u8]) {
|
||||
(
|
||||
TestStructFlags::from_bytes([buf.get_u8(), buf.get_u8(),]),
|
||||
buf
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
|
||||
@ -282,7 +282,6 @@ impl Compact for bool {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use ethers_core::types::Address;
|
||||
use modular_bitfield::prelude::*;
|
||||
|
||||
#[test]
|
||||
fn compact_bytes() {
|
||||
|
||||
@ -2,8 +2,6 @@ use crate::db::{
|
||||
models::{accounts::AccountBeforeTx, StoredBlockBody},
|
||||
Compress, Decompress, Error,
|
||||
};
|
||||
use bytes::Buf;
|
||||
use modular_bitfield::prelude::*;
|
||||
use reth_codecs::{main_codec, Compact};
|
||||
use reth_primitives::*;
|
||||
|
||||
|
||||
@ -7,8 +7,7 @@ use crate::{
|
||||
},
|
||||
impl_fixed_arbitrary,
|
||||
};
|
||||
use bytes::{Buf, Bytes};
|
||||
use modular_bitfield::prelude::*;
|
||||
use bytes::Bytes;
|
||||
use reth_codecs::{main_codec, Compact};
|
||||
use reth_primitives::{BlockHash, BlockNumber, Header, TxNumber, H256};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
use crate::{H256, U256};
|
||||
use bytes::Buf;
|
||||
use modular_bitfield::prelude::*;
|
||||
use reth_codecs::{main_codec, Compact};
|
||||
|
||||
/// Account saved in database
|
||||
|
||||
@ -2,9 +2,8 @@ use crate::{
|
||||
proofs::{EMPTY_LIST_HASH, EMPTY_ROOT},
|
||||
BlockHash, BlockNumber, Bloom, H160, H256, U256,
|
||||
};
|
||||
use bytes::{Buf, BufMut, BytesMut};
|
||||
use bytes::{BufMut, BytesMut};
|
||||
use ethers_core::{types::H64, utils::keccak256};
|
||||
use modular_bitfield::prelude::*;
|
||||
use reth_codecs::{main_codec, Compact};
|
||||
use reth_rlp::{length_of_length, Decodable, Encodable};
|
||||
use std::ops::Deref;
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
use crate::{Bloom, Log, TxType};
|
||||
use bytes::{Buf, BufMut, BytesMut};
|
||||
use modular_bitfield::prelude::*;
|
||||
use reth_codecs::{main_codec, Compact};
|
||||
use reth_rlp::{length_of_length, Decodable, Encodable};
|
||||
use std::cmp::Ordering;
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
use super::{H256, U256};
|
||||
use bytes::Buf;
|
||||
use modular_bitfield::prelude::*;
|
||||
use reth_codecs::{main_codec, Compact};
|
||||
|
||||
/// Account storage entry.
|
||||
|
||||
@ -3,7 +3,6 @@ pub use access_list::{AccessList, AccessListItem};
|
||||
use bytes::{Buf, BytesMut};
|
||||
use derive_more::{AsRef, Deref};
|
||||
use ethers_core::utils::keccak256;
|
||||
use modular_bitfield::prelude::*;
|
||||
use reth_codecs::{main_codec, Compact};
|
||||
use reth_rlp::{length_of_length, Decodable, DecodeError, Encodable, Header, EMPTY_STRING_CODE};
|
||||
pub use signature::Signature;
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
use crate::{transaction::util::secp256k1, Address, H256, U256};
|
||||
use bytes::Buf;
|
||||
use modular_bitfield::prelude::*;
|
||||
use reth_codecs::{main_codec, Compact};
|
||||
use reth_rlp::{Decodable, DecodeError, Encodable};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user