Crates.io | rombok_macro |
lib.rs | rombok_macro |
version | 0.3.0 |
source | src |
created_at | 2024-05-25 18:18:28.901444 |
updated_at | 2024-05-29 16:42:39.268112 |
description | macro for rombok libary |
homepage | https://github.com/myyrakle/rombok/blob/master/README.md |
repository | https://github.com/myyrakle/rombok |
max_upload_size | |
id | 1252117 |
size | 17,092 |
boilerplate generation macros like lombok.
It automatically creates pattern implementations such as Getter, Setter, and Builder through attribute macros.
The Getter macro generates get_{fieldname}
and get_{fieldname}_mut
methods for each field of the structure.
use rombok::Getter;
#[Getter]
struct Person {
name: String,
age: u8,
}
fn main() {
let person = Person {
name: "John".to_string(),
age: 30,
};
let name = person.get_name();
let age = person.get_age();
println!("Hello, world!: {name}, {age}");
}
The Setter macro generates a set_{fieldname}
method for each field in the structure.
use rombok::Setter;
#[Setter]
struct Person {
name: String,
age: u8,
}
fn main() {
let mut person = Person {
name: "John".to_string(),
age: 30,
};
person.set_name("Jane".to_string());
person.set_age(31);
println!("Hello, world!: {}, {}", person.name, person.age);
}
With macro generates a with_{fieldname}
method for each field in the structure.
Unlike Setter, it modifies the value through move and returns the changed object as the return value.
use rombok::With;
#[With]
struct Person {
name: String,
age: u8,
}
fn main() {
let person = Person {
name: "".to_string(),
age: 0,
}.with_name("Jane".to_string()).with_age(31);
let person = person.with_name("tom".to_string()).with_age(44);
println!("Hello, world!: {}, {}", person.name, person.age);
}
The Builder macro generates a Builder pattern object for the structure, and adds a builder static method to the structure. Each method in builder is implemented through the With pattern.
use rombok::Builder;
#[Builder]
struct Person {
name: String,
age: u8,
}
fn main() {
let person = Person::builder()
.name("Jane".to_string())
.age(31)
.build();
println!("Hello, world!: {}, {}", person.name, person.age);
}
This is an attribute macro that creates a constructor that initializes all fields of the structure.
use rombok::AllArgsConstructor;
#[AllArgsConstructor]
struct Person {
name: String,
age: u8,
money: Option<f64>,
point: (u8, u8),
}
fn main() {
let person = Person::with_all_args(
"John".to_string(),
30,
Some(100.0),
(10, 20),
);
let name = person.name;
println!("Hello, world!: {name}");
}
This is an attribute macro that creates a constructor method that does not receive arguments and initializes all fields to default. The type of each field must implement the Default trait.
use rombok::NoArgsConstructor;
#[NoArgsConstructor]
struct Person {
name: String,
age: u8,
money: Option<f64>,
}
fn main() {
let person = Person::with_no_args();
let money = person.money;
println!("Hello, world!: {money:?}");
}
This is an attribute macro that generates the equals
and hashcode
methods for the structure. (Eq, Hash trait)
use rombok::EqualsAndHashcode;
use std::hash::{DefaultHasher, Hash, Hasher};
#[derive(Debug)]
#[EqualsAndHashcode]
struct Person {
name: String,
age: u8,
}
fn main() {
let person = Person {
name: "John".to_string(),
age: 30,
};
let person2 = Person {
name: "Jane".to_string(),
age: 30,
};
let mut hasher = DefaultHasher::new();
person.hash(&mut hasher);
let hashcode = hasher.finish();
println!("hashcode: {:?}", hashcode);
assert_ne!(person, person2);
}
This is an attribute macro that generates the to_string
method for the structure. (Display, ToString, Debug trait)
use rombok::ToString;
#[ToString]
struct Person {
name: String,
age: u8,
}
fn main() {
let person = Person {
name: "John".to_string(),
age: 30,
};
println!("to_string: {}", person.to_string());
}
This macro is a boilerplate combination of the following macros:: ToString + EqualsAndHashcode + Getter + AllArgsConstructor
use rombok::Value;
#[Value]
struct Person {
name: String,
age: u8,
}
fn main() {
let person = Person::with_all_args("John".to_string(), 30);
println!("age: {}", person.get_age());
let person2 = Person::with_all_args("Jane".to_string(), 30);
assert_ne!(person, person2);
println!("person: {}", person.to_string());
}