Skip to content

Commit 87d1b09

Browse files
committed
refactor(u): parse Solidity layout type directly via syn::parse_str
1 parent e95fa0f commit 87d1b09

3 files changed

Lines changed: 6 additions & 18 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/u/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ jsonrpsee = { workspace = true, features = ["client", "full", "tracing"
3636
keccak-asm = "0.1.4"
3737
num_cpus = "1.16"
3838
parlia-verifier = { workspace = true }
39-
proc-macro2 = { workspace = true }
4039
protos = { workspace = true, features = ["proto_full", "serde"] }
40+
syn = { workspace = true, features = ["parsing"] }
4141
rand = { workspace = true }
4242
serde = { workspace = true, features = ["derive"] }
4343
serde_json = { workspace = true }

tools/u/src/slot.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,13 @@ use std::collections::VecDeque;
33

44
use anyhow::{Context, bail};
55
use solidity_slot::{H256, MappingKey, Slot, U256};
6-
use syn_solidity::{Item, Type};
6+
use syn_solidity::Type;
77
use typed_arena::Arena;
88

99
fn parse_layout(layout: &str) -> anyhow::Result<Type> {
10-
// syn-solidity parses items, not bare types. Wrapping in a dummy state variable lets it parse any layout string the user provides.
11-
let src = format!("{layout} public dummy;");
12-
13-
let tokens = src
14-
.parse::<proc_macro2::TokenStream>()
15-
.ok()
16-
.context("failed to tokenize layout")?;
17-
let file = syn_solidity::parse2(tokens).context("failed to parse layout as Solidity")?;
18-
19-
match file.items.into_iter().next() {
20-
Some(Item::Variable(var)) => match var.ty {
21-
ty @ (Type::Mapping(_) | Type::Array(_)) => Ok(ty),
22-
other => bail!("unsupported top-level layout type: {other:?}"),
23-
},
24-
_ => bail!("layout did not parse to a state variable declaration"),
10+
match syn::parse_str::<Type>(layout).context("failed to parse layout as Solidity type")? {
11+
ty @ (Type::Mapping(_) | Type::Array(_)) => Ok(ty),
12+
other => bail!("unsupported top-level layout type: {other:?}"),
2513
}
2614
}
2715

0 commit comments

Comments
 (0)