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
use basicmethod::BasicMethod;
pub enum Division {
Marketing, IT, Finance, Other
}
#[derive(BasicMethod)]
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)]
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
use basicmethod::BasicMethod;
pub enum Division {
Marketing, IT, Finance, Other
}
#[derive(BasicMethod)]
#[allow(unused)]
struct Sample {
#[only="get"] id: i32, #[exclude] name: String, #[only="set"] age: u8, division: Division }
fn main() {
let mut s = Sample::new(12, "Fika".to_string(), 27, Division::Finance);
let _id = s.get_id();
s.set_age(30);
s.set_division(Division::IT);
let _div = s.get_division();
}