smile-marco

Crates.iosmile-marco
lib.rssmile-marco
version1.0.0
sourcesrc
created_at2024-04-01 03:21:46.913716
updated_at2024-05-26 04:12:23.878672
descriptionOne contains many useful macros for generating common code
homepage
repositoryhttps://github.com/shunlingsmile/smile-marco
max_upload_size
id1192096
size39,236
shunling (shunlingsmile)

documentation

https://docs.rs/smile-marco

README

smile-marco provides macros such as Getter, Setter, Wither, Builder, etc

These macros generate methods such as get, set, with, and builder for properties within a struct, providing quick access and manipulation. Convenient for daily development.

[dependencies]
smile-marco = { version = "1.0.0", features = ["full"] }
#or
smile-marco = { version = "1.0.0"}

If needed, use partial macros.

[dependencies]
smile-marco = { version = "1.0.0", features = ["builder","getter","setter","wither"] }

Example

use smile_marco::{Builder, Getter, Setter, Wither};

#[derive(Builder, Getter, Setter, Wither)]
struct Book {
    title: String,
    #[name(cost)] // rename
    price: i32,
    #[exclude] // Do not generate set methods for author
    author: String,
}

fn main() {
    let mut book = Book::builder()
        .title("Rust Programming".to_string())
        .price(100)
        .author("rust".to_string())
        .build();
    assert_eq!(book.title, "Rust Programming".to_string());
    book.set_title("Rust Book".into());
    assert_eq!(book.title, "Rust Book".to_string());
    assert_eq!(book.get_cost(), &100);
    let mut book = book.with_cost(|c| c + 100);
    assert_eq!(book.price, 200);
}

If you need more usage examples, please refer to examples

Commit count: 3

cargo fmt