2 releases
| 0.1.1 | Jun 15, 2025 |
|---|---|
| 0.1.0 | Jun 14, 2025 |
#1649 in Data structures
31 downloads per month
7KB
PatternUtils
This crate provides handy procedural macros for implementing common design patterns in Rust.
Features
-
Builderderive macro
Implements the Builder pattern by generating an associatedBuilderstruct with fluent setter methods and flexible configuration options. -
observermacro (for traits)
Implements the Observer pattern by generating aPublisherstruct for the specified trait, allowing multiple observers to subscribe to instances of types implementing that trait.
Example
use patternutils::Builder;
#[derive(Builder)]
#[builder_attr(name = "CommandCreator", opt_in)]
struct Command {
#[builder_field(name = "value", include = true)]
definition: String,
#[builder_field(include = true)]
arg_count: usize,
accepted_flag_fallback: fn(val: usize) -> usize,
childs: Vec<Command>,
}
let mut builder = Command::builder();
let command = builder
.value(String::from("my-command"))
.arg_count(3)
.build(|val| val + 5, vec![]);
assert_eq!(command.definition, String::from("my-command"));
assert_eq!(command.arg_count, 3);
assert_eq!((command.accepted_flag_fallback)(67), 72);
assert_eq!(command.childs.len(), 0);
Dependencies
~125–510KB
~12K SLoC