| Crates.io | lombokrs |
| lib.rs | lombokrs |
| version | 0.2.0 |
| created_at | 2024-07-20 15:11:15.531766+00 |
| updated_at | 2024-07-28 09:39:43.990544+00 |
| description | lombokrs is a lightweight Rust macro library. It is the simple implementation of lombok Java in Rust. |
| homepage | https://github.com/photowey/lombokrs |
| repository | https://github.com/photowey/lombokrs |
| max_upload_size | |
| id | 1309529 |
| size | 9,153 |
lombokrslombokrs(Lombok Rust) is a lightweight Rust macro library. It is the simple implementation of Lombok Java in Rust.
This project, lombokrs, was developed with significant inspiration from the open-source project lombok-rs. Special
thanks to the contributors of lombok-rs for their excellent work,
particularly in handling Lifetimes. While
building upon their foundation, I have made several modifications based on my understanding, especially in the areas of
Builder and Getter implementations.
v0.2.0 version, redesigned the functionality of macro Builder, mainly inspired by
the proc-macro-workshop project.
@Getter - #[derive(Getter)]@Setter - #[derive(Setter)]@Builder - #[derive(Builder)]@Data - #[derive(Data)]@EqualsAndHashCode - #[derive(EqualsAndHashCode)]@ToString - #[derive(ToString)]@Value - #[derive(Value)]@NoArgsConstructor - #[derive(NoArgsConstructor)]@AllArgsConstructor - #[derive(AllArgsConstructor)]Why the annotations below are not implemented:
EqualsAndHashCodeToStringValueNoArgsConstructorAllArgsConstructorEquals, ToString, HashCode, Value etc. are not used very often;NoArgsConstructor can be replaced by Default Trait;AllArgsConstructor can be replaced by builder mode.Based on the above reasons, it is not implemented. If necessary, please use lombok-rs crates instead.
Add this to your Cargo.toml:
[dependencies]
lombokrs = "0.2"
APIsPrepare#[derive(Setter, Getter, Builder, Debug)]
pub struct User {
id: u32,
age: u8,
name: String,
email: String,
hobby: Vec<String>,
// @since 0.2.0
#[builder(method = "activity")]
activities: Vec<String>,
}
#[derive(Setter, Getter, Builder, Debug)]
pub struct LifetimeUser<'a> {
id: u32,
age: u8,
name: &'a str,
email: &'a str,
hobby: Box<&'a str>,
}
#[derive(Data, Debug)]
pub struct DataUser {
id: u32,
age: u8,
name: String,
email: String,
hobby: Vec<String>,
}
// ----------------------------------------------------------------
impl User {
pub fn new(...) -> Self {}
}
// ----------------------------------------------------------------
impl<'a> LifetimeUser<'a> {
pub fn new(...) -> Self {}
}
Setterlet mut user = User::new(
10086,
18,
"photowey".to_string(),
"photowey@gmail.com".to_string(),
vec!["badminton".to_string()],
);
// ----------------------------------------------------------------
assert_eq!(&10086u32, user.get_id());
assert_eq!(&18u8, user.get_age());
assert_eq!("photowey", user.get_name());
assert_eq!("photowey@gmail.com", user.get_email());
assert_eq!(&vec!["badminton".to_string()], user.get_hobby());
// ---------------------------------------------------------------- Setter
user.set_id(9527);
user.set_age(25);
user.set_name("lombokrs".to_string());
user.set_email("lombokrs@gmail.com".to_string());
user.set_hobby(vec!["football".to_string()]);
// ----------------------------------------------------------------
assert_eq!(&9527u32, user.get_id());
assert_eq!(&25u8, user.get_age());
assert_eq!("lombokrs", user.get_name());
assert_eq!("lombokrs@gmail.com", user.get_email());
assert_eq!(&vec!["football".to_string()], user.get_hobby());
Getterlet user = User::new(
10086,
18,
"photowey".to_string(),
"photowey@gmail.com".to_string(),
vec!["badminton".to_string()],
);
// ---------------------------------------------------------------- Getter | get_x()
assert_eq!(&10086u32, user.get_id());
assert_eq!(&18u8, user.get_age());
assert_eq!("photowey", user.get_name());
assert_eq!("photowey@gmail.com", user.get_email());
assert_eq!(&vec!["badminton".to_string()], user.get_hobby());
// ---------------------------------------------------------------- Getter/fluent | x()
assert_eq!(&10086u32, user.id());
assert_eq!(&18u8, user.age());
assert_eq!("photowey", user.name());
assert_eq!("photowey@gmail.com", user.email());
assert_eq!(&vec!["badminton".to_string()], user.hobby());
Builder// ---------------------------------------------------------------- Builder
// UserBuilder = User::builder()
let user = User::builder()
.id(10086)
.age(18)
.name("photowey".to_string())
.email("photowey@gmail.com".to_string())
.hobby(vec!["badminton".to_string()])
// @since 0.2.0
.activities(vec!["badminton".to_string()])
// #[builder(method = "activity")]
.activity("badminton".to_string())
.build() // Result<T,E>
.unwrap(); // @since 0.2.0
// ----------------------------------------------------------------
assert_eq!(&10086u32, user.get_id());
assert_eq!(&18u8, user.get_age());
assert_eq!("photowey", user.get_name());
assert_eq!("photowey@gmail.com", user.get_email());
assert_eq!(&vec!["badminton".to_string()], user.get_hobby());
Data// ---------------------------------------------------------------- Builder
let mut user = DataUser::builder()
.id(10086)
.age(18)
.name("photowey".to_string())
.email("photowey@gmail.com".to_string())
.hobby(vec!["badminton".to_string()])
.build() // Result<T,E>
.unwrap(); // @since 0.2.0
// ---------------------------------------------------------------- Setter
user.set_id(9527);
user.set_age(25);
user.set_name("lombokrs".to_string());
user.set_email("lombokrs@gmail.com".to_string());
user.set_hobby(vec!["football".to_string()]);
// ---------------------------------------------------------------- Getter | get_x()
assert_eq!(&9527u32, user.get_id());
assert_eq!(&25u8, user.get_age());
assert_eq!("lombokrs", user.get_name());
assert_eq!("lombokrs@gmail.com", user.get_email());
assert_eq!(&vec!["football".to_string()], user.get_hobby());
// ---------------------------------------------------------------- Getter/fluent | x()
assert_eq!(&9527u32, user.id());
assert_eq!(&25u8, user.age());
assert_eq!("lombokrs", user.name());
assert_eq!("lombokrs@gmail.com", user.email());
assert_eq!(&vec!["football".to_string()], user.hobby());