# vodca [crate.io](https://crates.io/crates/vodca) [GitHub Actions Workflow Status](https://github.com/turtton/vodca/actions/workflows/check.yml) vodca generates boiler-template codes in DDD ## ValueObject ### Fromln ```rust use vodca::Fromln; #[derive(Fromln)] pub struct TupleStruct(String); /// generated by vodca impl From for String { fn from(value: TupleStruct) -> Self { value.0 } } /// fn main() { let ts = TupleStruct("Hello, world!".to_string()); let parsed: String = ts.into(); assert_eq!(parsed, "Hello, world!"); } ``` ### AsRefln ```rust use vodca::AsRefln; #[derive(AsRefln)] pub struct TupleStructString(String); /// generated by vodca impl AsRef for TupleStructString { fn as_ref(&self) -> &String { &self.0 } } /// fn main() { let ts_string = TupleStructString("Hello, world!".to_string()); let str: &str = ts_string.as_ref(); assert_eq!(str, "Hello, world!"); } ``` ## Entity ### References ```rust use vodca::References; #[derive(References)] pub struct Struct { field_i32: i32, field_string: String, field_vec: Vec } /// generated by vodca impl Struct { pub fn field_i32(&self) -> &i32 { &self.field_i32 } pub fn field_string(&self) -> &String { &self.field_string } pub fn field_vec(&self) -> &Vec { &self.field_vec } } /// fn main() { let s = Struct { field_i32: 42, field_string: "Hello, world!".to_string(), field_vec: vec![1, 2, 3] }; let i32 = s.field_i32(); assert_eq!(i32, &42); let str: &str = s.field_string(); assert_eq!(str, "Hello, world!"); let vec: &[i32] = s.field_vec(); assert_eq!(vec, &[1, 2, 3]); } ``` ### Nameln ```rust use vodca::Nameln; #[derive(Nameln)] pub enum Events { Created { id: i64 }, Updated(T), Deleted, } /// generated by vodca impl Events { #[allow(unused_variables, non_snake_case)] pub fn name(&self) -> String { let name = match self { Enum::Created { id: i64 } => "Created", Enum::Updated(T) => "Updated", Enum::Deleted => "Deleted", }; name.to_string() } } /// #[derive(Newln)] #[vodca(prefix = "action", snake_case)] pub enum InitAction { NewGame, Continue } /// generated by vodca impl InitAction { pub fn name(&self) -> String { let name = match self { InitAction::NewGame => "action_new_game", InitAction::Continue => "action_continue", }; name.to_string() } } /// fn main() { let i64: Enum = Enum::Created { id: 42 }; let name = i64.name(); assert_eq!(name, "Created"); let generics = Enum::Updated("Hello, world!".to_string()); let name = generics.name(); assert_eq!(name, "Updated"); let deleted: Enum<&str> = Enum::Deleted; let name = deleted.name(); assert_eq!(name, "Deleted"); let new_game = InitAction::NewGame; let name = new_game.name(); assert_eq!(name, "action_new_game"); let cont = InitAction::Continue; let name = cont.name(); assert_eq!(name, "action_continue"); } ``` ## Both ### Newln ```rust use vodca::Newln; #[derive(Newln)] pub struct TupleStruct(String); /// generated by vodca impl TupleStruct { pub fn new(value_0: impl Into) -> Self { Self(value_0.into()) } } /// #[derive(Newln)] pub struct Struct { field_i32: i32, field_string: String, field_vec: Vec } /// generated by vodca impl Struct { pub fn new(field_i32: i32, field_string: String, field_vec: Vec) -> Self { Self { field_i32, field_string, field_vec } } } /// fn main() { let t = TupleStruct::new("Hello, world!".to_string()); assert_eq!(t.0, "Hello, world!"); let s = Struct::new(42, "Hello, world!".to_string(), vec![1, 2, 3]); assert_eq!(s.field_i32, 42); assert_eq!(s.field_string, "Hello, world!"); assert_eq!(s.field_vec, vec![1, 2, 3]); } ```