Skip to content
/ kenzu Public

kenzu is a procedural macro crate for generating builder patterns in Rust — both immutable and mutable, with optional compile-time validation using field-level attributes. Whether you're building configuration structs, domain models, or input data, kenzu eliminates boilerplate while increasing safety and clarity.

License

Notifications You must be signed in to change notification settings

pas2rust/kenzu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kenzu

Crates.io Docs.rs License

kenzu is a procedural macro crate for generating builder patterns in Rust — both immutable and mutable, with optional compile-time validation using field-level attributes.

Whether you're building configuration structs, domain models, or input data, kenzu eliminates boilerplate while increasing safety and clarity.


✨ Features

  • 🧱 Builder: Generates immutable, fluent .build()-based builders.
  • 🔁 M_Builder: Generates mutable builders using &mut self methods.
  • 🧰 Field-level defaults with #[set(value = "...")].
  • 🔍 Regex-based validation with #[build(pattern = ..., err = ...)].
  • 🔢 Range validation with #[range(min = ..., max = ..., err = ...)].
  • 🧼 Skip trait generation with #[skip_trait], or skip setters with #[set(skip)].
  • ⛔ Compile-time validation of regex patterns — invalid patterns produce macro errors.

⚙️ Installation

Add it to your Cargo.toml:

cargo add kenzu

🚀 Usage

Builder

use kenzu::Builder;

#[derive(Builder)]
pub struct User {
    pub id: String,
    
    #[set(value = "default-name")]
    pub name: String,
    
    pub password: String,
    
    #[build(pattern = r"^[^@]+@[^@]+\.[^@]+$", err = "invalid email")]
    #[set(value = "user@example.com")]
    pub email: String,
    
    #[range(min = 18, max = 99, err = "invalid age")]
    #[set(value = 18)]
    pub age: u8,
}

let user = User::new()
    .id("001")
    .name("Alice")
    .password("hunter2")
    .email("alice@example.com")
    .age(30)
    .build()
    .unwrap();

Mutable Builder

use kenzu::M_Builder;

#[derive(M_Builder)]
pub struct User<'a> {
    #[set(value = "uuid")]
    pub id: String,
    
    #[set(value = "default")]
    pub name: String,
    
    pub password: String,
    
    #[build(pattern = r"^[^@]+@[^@]+\.[^@]+$", err = "invalid email")]
    #[set(value = "email@example.com")]
    pub email: String,
    
    pub fix: &'a str,
    
    #[set(value = 18)]
    pub age: u8,
    
    #[set(skip)]
    pub code: u8,
    
    pub def: String,
}

let mut user = User::new();

user.id("123")
    .name("Bob")
    .password("pass123")
    .email("bob@example.com")
    .fix("ref")
    .def("value")
    .age(42)
    .build()
    .unwrap();

❤️ Donate

Monero Bitcoin

About

kenzu is a procedural macro crate for generating builder patterns in Rust — both immutable and mutable, with optional compile-time validation using field-level attributes. Whether you're building configuration structs, domain models, or input data, kenzu eliminates boilerplate while increasing safety and clarity.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages