Introduction
Moxy is a Rust derive macro crate that eliminates boilerplate. Get Display, Deref, Default, Build, Get, Set, Union, and Forward implementations with a single attribute — no hand-written impl blocks needed.
What You Get
- Display — Flexible
std::fmt::Displaywith multiple output formats, JSON serialization, and colored terminal output. - Deref — Automatic
std::ops::Derefdelegation to inner fields. - Build — Type-safe fluent builder with compile-time field tracking,
V: Into<T>setters, and inline defaults. - Default —
std::default::Defaultwith per-field custom expressions via#[moxy(default = expr)]. - Get — Field getters returning
Deref::Target(e.g.String→&str), with copy/clone/mutable modifiers and callbacks. - Set — Field setters with
Into<T>coercion,Option<T>auto-wrapping, transform callbacks, and chaining. - Union — Enum variant accessors (
is_*,as_*,to_*), common field detection, and theunionize!shorthand macro. - Forward — Forward method calls to inner fields (structs) or variant payloads (enums) by declaring method signatures.
Quick Example
use moxy::{Deref, Display};
#[derive(Deref, Display)]
#[moxy(display(debug, pretty))]
struct User {
#[moxy(deref)]
name: String,
email: String,
}
let user = User {
name: "John".into(),
email: "john@example.com".into(),
};
// Display output:
// User {
// name: "John",
// email: "john@example.com",
// }
// Deref delegates to name:
assert_eq!(user.len(), 4);
Inspiration
Moxy draws ideas from these excellent crates:
License
MIT