Anodized Core
This crate is the interoperability layer for tools connected to the Anodized specification system.
Who Is This For?
-
If you want to add specifications to your code...
You're looking for the
anodizedcrate, which provides the#[spec]macro. -
If you're building a tool and want to work with Anodized specifications...
You're in the right place! This crate provides the necessary components to parse and interact with Anodized specification annotations.
-
If you're looking for blockchain smart contracts...
"These are not the contracts you're looking for." 🤖
But don't leave yet! While Anodized is about Design by Contract (not blockchain), it can still help make your smart contracts more robust through formal specifications.
Specification Syntax
The #[spec] attribute's fields follow a specific grammar, which is formally defined using EBNF as follows.
fields = [ qualifiers ]
[ requires_fields ]
, [ maintains_fields ]
(* not a typo: at most one `captures:` *)
, [ captures_field ]
, [ ensures_fields ];
qualifiers = { qualifier };
qualifier = `functional` | `pure` | `total`
| `deterministic` | `effectfree` | `infallible` | `terminating`;
requires_fields = { requires_field };
maintains_fields = { maintains_field };
ensures_fields = { ensures_field };
requires_field = [ cfg_attr ] , `requires:` , conditions, `,`;
maintains_field = [ cfg_attr ] , `maintains:` , conditions, `,`;
captures_field = `captures:` , captures, `,`;
ensures_field = [ cfg_attr ] , `ensures:` , postconds, `,`;
conditions = expr | condition_list;
condition_list = `[` , expr , { `,` , expr } , [ `,` ] , `]`;
captures = capture_stmt | capture_list;
capture_list = `[` , capture_stmt , { `,` , capture_stmt } , [ `,` ] , `]`;
capture_stmt = pattern , `=` , expr;
postconds = postcond_expr | postcond_list | postcond_list_closure;
postcond_list = `[` , postcond_expr , { `,` , postcond_expr } , [ `,` ] , `]`;
postcond_expr = expr | postcond_closure;
postcond_closure = `|` , pattern , `|` , expr;
postcond_list_closure = `|` , pattern , `|` , condition_list;
cfg_attr = `#[cfg(` , settings , `)]`;
Notes:
- The last
,is optional. - The
fieldsrule defines a sequence of optional field groups that must appear in the specified order. expris a Rustexpression.patternis an irrefutable Rustpattern.settingsis the content of thecfgattribute (e.g.test,debug_assertions).- Every valid spec field can be parsed as a Rust
structexpression field.
Instrumentation
The #[spec] macro transforms the function body by injecting code, determined by compiler cfg settings (e.g. anodized_panic, anodized_print). This process, known as instrumentation, follows a clear pattern.
Given an original function like this:
The macro rewrites the body to be conceptually equivalent to the following:
When a condition has a #[cfg(...)] attribute, the corresponding check! is wrapped in an if cfg!(...) block. This follows standard Rust #[cfg] semantics: the check only runs when the configuration predicate is true. Details of the injected code are determined by cfg settings.