8 releases

0.1.8 Oct 10, 2024
0.1.7 Oct 10, 2024
0.1.6 Sep 25, 2024
0.1.3 Aug 29, 2024
0.1.1 Apr 6, 2024

#312 in Procedural macros

23 downloads per month

MIT license

15KB
239 lines

vodca

crate.io GitHub Actions Workflow Status

vodca generates boiler-template codes in DDD

ValueObject

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!");
}

Entity

References


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]);
}

Nameln


use vodca::Nameln;

#[derive(Nameln)]
pub enum Events<T> {
    Created { id: i64 },
    Updated(T),
    Deleted,
}

/// generated by vodca
impl<T> Events<T> {
    #[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<i64> = 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


use vodca::Newln;

#[derive(Newln)]
pub struct TupleStruct(String);

/// generated by vodca
impl TupleStruct {
    pub fn new(value_0: impl Into<String>) -> Self {
        Self(value_0.into())
    }
}
///

#[derive(Newln)]
pub struct Struct {
    field_i32: i32,
    field_string: String,
    field_vec: Vec<i32>
}

/// generated by vodca
impl Struct {
    pub fn new(field_i32: i32, field_string: String, field_vec: Vec<i32>) -> 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]);
}

Dependencies

~215–660KB
~16K SLoC