alloy-sol-type-parser
Solidity type string parsing.
We do not recommend most users use this crate directly. This library provides a
simple parser for working with Solidity type encoded as strings. It is
primarily a dependency for the user-facing APIs in alloy-json-abi and
alloy-dyn-abi. Please see the documentation for those crates for
more information.
This parser generally follows the solidity spec, however, it supports only a subset of possible types, chosen to support ABI coding.
Usage
The TypeSpecifier is the top-level type in this crate. It is a wrapper around
a section of a string (called a span). It progressively breaks the strings
down into subspans, and adds metadata about the type. E.g. it tracks the stem
type as well as the sizes of array dimensions. A TypeSpecifier is expected to
handle any valid Solidity type string.
use TypeSpecifier;
use NonZeroUsize;
// Parse a type specifier from a string
let my_type = try_from.unwrap;
// Read the total span
assert_eq!;
// A type specifier has a stem type. This is the type string, stripped of its
// array dimensions.
assert_eq!;
// Arrays are represented as a vector of sizes. This allows for deep nesting.
assert_eq!;
// Type specifiers also work for complex tuples!
let my_tuple = try_from.unwrap;
assert_eq!;
// Types are NOT resolved, so you can parse custom structs just by name.
let my_struct = try_from.unwrap;
Why not support parse()?
The core::str::FromStr trait is not implemented for TypeSpecifier because
of lifetime constraints. Unfortunately, it is impossible to implement this for
a type with a lifetime dependent on the input str. Instead, we recommend using
TryFrom::try_from.
Why not use syn?
This is NOT a full syntax library, and is not intended to be used as a
replacement for alloy-syn-solidity. This crate is intended to be used for
parsing type strings present in existing ecosystem tooling, and nothing else.
It is not intended to be used for parsing Solidity source code.
This crate is useful for:
- syntax-checking abi json files
- providing known-good input to
alloy-dyn-abi - porting ethers.js code to rust
It is NOT useful for:
- parsing Solidity source code
- generating rust code from Solidity source code
- generating Solidity source code from rust code