⛏️ fieldwork – field accessor generation
fieldwork is a procedural macro crate designed to automate the generation of field accessor
methods for structs and enums. By leveraging Rust's powerful macro system, fieldwork reduces
boilerplate code, enhances code readability, and ensures consistency. Just as importantly,
fieldwork intends to be fully customizable and expressive for common access patterns.
Manually writing getters and setters is repetitive and adds to maintenance burden. fieldwork
addresses this by providing a procedural macro that automatically generates these methods based on
your type definitions. The intent of this crate, and distinguishing feature, is to be as
customizable and expressive as writing your own getters and setters. The crate succeeds if you are
able to emit exactly the code that you would have manually written, but far more concisely.
Example to get a sense of the library
Structs
use PathBuf;
let mut config = default
.with_host // accepts &str via Into<String>
.with_port
.with_log_dir // accepts &str, wraps in Some automatically
.with_tls_required; // sets bool to true
config.host_mut.make_ascii_lowercase;
assert_eq!; // String → &str
assert_eq!;
assert_eq!;
assert!; // rename_predicates: bool getters use is_ prefix
assert!;
// Chainable setters return &mut Self
config.set_port.set_verbose;
config = config.without_log_dir; // clears Option to None
assert!;
Enums
Enums are supported with smart handling of field coverage across variants — whether a field appears in all variants or only some:
let event = Request ;
// host and port appear in every variant → full coverage, same smart defaults as structs
assert_eq!; // String → &str
assert_eq!; // Copy types returned by value
// path appears in only one variant → partial coverage, return type wrapped in Option
assert_eq!;
assert_eq!;
// into_field is generated for full-coverage fields
assert_eq!;
Performance
The compile time cost of using a proc macro crate is always worth considering. All efforts have been made to keep this crate as lightweight as possible and featureful enough to be worth the tradeoff.
Testing
This crate has a full suite of macro-expansion tests in tests/expand. These tests are also used for test coverage.
Documentation
View the docs for main on github at docs.md, or on github pages in rustdoc format. The most recent release can always be viewed at docs.rs.
Safety
This crate uses #![deny(unsafe_code)].