basicmethod 0.1.0

Add constructor, get and set method using derive macro
Documentation

basicmethod

**Add constructor, get and set method using derive macro

  • This crates contains derive macro that add: *Constructor - new method *Getter - get method for each struct field *Setter - set method for each struct field *Info - info method to print struct documentation *Fields - fields method that return Vec<(&str, &str)> of fields **Support for struct, unit-struct and tuple-struct **enum is not supported

Examples - quick start

// src/main.rs
use basicmethod::BasicMethod;

pub enum Division {
    Marketing, IT, Finance, Other
}

#[derive(BasicMethod)]
/// Sample documentation
struct Sample {
    id: i32,
    name: String,
    age: u8,
    division: Division
}

fn demo1() {
    let mut s = Sample::new(23, "Natalia".to_string(), 35, Division::IT);
    println!("{}", s.get_name());
    s.set_name("Mundo".to_string());
    println!("{}", s.get_name());
    let i = Sample::info();
    assert_eq!("Sample documentation", i.as_str());

    for (field, ftype) in Sample::fields() {
        println!("{}-> {}", field, ftype);
    }
}

#[derive(BasicMethod)]
/// A tuple struct
struct Player(String, u8);

fn demo2() {
    let mut kowi = Player("Joko".to_string(), 55);
    for field in Player::fields() {
        println!("{}", field)
    }
    kowi.set_String("Jokowido".to_string());
    kowi.set_u8(64);
    println!("{} {}", kowi.get_String(), kowi.get_u8());
}

fn main() {
    demo1();
    demo2();
}

**Helper attributes

  • #[only="get"] -> will generate get method only
  • #[only="set"] -> will generate set method only
  • #[exclude] -> will not generate get nor set method

Examples - using helper attributes

// src/main.rs
use basicmethod::BasicMethod;

pub enum Division {
    Marketing, IT, Finance, Other
}

#[derive(BasicMethod)]
#[allow(unused)]
/// Sample documentation
struct Sample {
    #[only="get"] id: i32,          // support only get method
    #[exclude] name: String,        // exclude - will not have get nor set method
    #[only="set"] age: u8,          // support only set method 
    division: Division              // support get and set method
}

fn main() {
    let mut s = Sample::new(12, "Fika".to_string(), 27, Division::Finance);

    // ===valid
    let _id = s.get_id();
    s.set_age(30);
    s.set_division(Division::IT);
    let _div = s.get_division();

    // ===invalid
    // s.set_id(58);
    // s.set_name("Tino".to_string());
    // let name = s.get_name();
    // let age = s.get_age();
}