#macro #lombok #java #builder

lombokrs

lombokrs is a lightweight Rust macro library. It is the simple implementation of lombok Java in Rust.

2 unstable releases

0.2.0 Jul 28, 2024
0.1.0 Jul 20, 2024

#110 in Procedural macros

Download history 106/week @ 2024-07-16 141/week @ 2024-07-23 35/week @ 2024-07-30

282 downloads per month

Apache-2.0

10KB

lombokrs

lombokrs(Lombok Rust) is a lightweight Rust macro library. It is the simple implementation of Lombok Java in Rust.

1. Acknowledgment

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.

2. Implementation

  • @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)]

2.1. Explanation

Why the annotations below are not implemented:

  • EqualsAndHashCode
  • ToString
  • Value
  • NoArgsConstructor
  • AllArgsConstructor
  1. In the actual development process, Equals, ToString, HashCode, Value etc. are not used very often;
  2. NoArgsConstructor can be replaced by Default Trait;
  3. AllArgsConstructor can be replaced by builder mode.

Based on the above reasons, it is not implemented. If necessary, please use lombok-rs crates instead.

3. Usage

Add this to your Cargo.toml:

[dependencies]
lombokrs = "0.2"

4.APIs

4.0. Prepare

#[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 {}
}

4.1. Setter

let mut user = User::new(
    10086,
    18,
    "photowey".to_string(),
    "[email protected]".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!("[email protected]", 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("[email protected]".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!("[email protected]", user.get_email());
assert_eq!(&vec!["football".to_string()], user.get_hobby());

4.2. Getter

let user = User::new(
    10086,
    18,
    "photowey".to_string(),
    "[email protected]".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!("[email protected]", 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!("[email protected]", user.email());
assert_eq!(&vec!["badminton".to_string()], user.hobby());

4.3. Builder

// ---------------------------------------------------------------- Builder
// UserBuilder = User::builder()

let user = User::builder()
    .id(10086)
    .age(18)
    .name("photowey".to_string())
    .email("[email protected]".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!("[email protected]", user.get_email());
assert_eq!(&vec!["badminton".to_string()], user.get_hobby());

4.4. Data

// ---------------------------------------------------------------- Builder

let mut user = DataUser::builder()
    .id(10086)
    .age(18)
    .name("photowey".to_string())
    .email("[email protected]".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("[email protected]".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!("[email protected]", 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!("[email protected]", user.email());
assert_eq!(&vec!["football".to_string()], user.hobby());

Dependencies

~1.5MB
~36K SLoC