1个不稳定版本
0.1.1 | 2024年4月6日 |
---|
#482 在 过程宏
9KB
80 行
vodca
vodca在DDD中生成样板代码
值对象
Fromln
use vodca::Fromln;
#[derive(Fromln)]
pub struct TupleStruct(String);
/// generated by vodca
impl From<TupleStruct> 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
use vodca::AsRefln;
#[derive(AsRefln)]
pub struct TupleStructString(String);
/// generated by vodca
impl AsRef<String> 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!");
}
实体
引用
use vodca::References;
#[derive(References)]
pub struct Struct {
field_i32: i32,
field_string: String,
field_vec: Vec<i32>
}
/// 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<i32> {
&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]);
}
依赖项
~255–700KB
~17K SLoC