Crates.io | domain_derive |
lib.rs | domain_derive |
version | 0.2.137 |
source | src |
created_at | 2019-09-02 23:16:18.3664 |
updated_at | 2019-11-12 04:06:04.917191 |
description | domain_derive holds derive macros for the domain_patterns crate. |
homepage | |
repository | https://github.com/PrismaPhonic/domain_patterns |
max_upload_size | |
id | 161690 |
size | 36,362 |
This crate provides domain_patterns
derive macros.
The Entity
derive macro can be used to automatically implement all methods of the Entity
trait
from the domain_patterns
crate. This only works if certain preconditions are met:
id
field of type Uuid
.version
field which is some integer type.#[macro_use]
extern crate domain_derive;
use uuid::Uuid;
#[derive(Entity)]
struct User {
id: Uuid,
version: u64
};
The ValueSetup
derive macro can be used to setup as much boilerplate as possible
for your choosen value object. It checks some preconditions:
value
of any type that is clonable.Once you've used this macro, you will still need to implement the ValueObject
trait,
but you will not have to implement TryFrom
(or create the validation error for TryFrom
, this
is handled by the macro), or implement PartialEq
or Clone
.
In case you need to use the validation error elsewhere, the created validation error will be the
name of your struct with ValidationError appended. For example, if you have an Email
struct,
then the generated validation error will be called EmailValidationError
.
#[macro_use]
extern crate domain_derive;
use domain_patterns::ValueObject;
use regex::Regex;
#[derive(ValueSetup)]
pub struct Email {
pub value: String,
}
impl ValueObject<String> for Email {
fn validate(value: &String) -> bool {
let email_rx = Regex::new(
r"^(?i)[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$"
).unwrap();
email_rx.is_match(value)
}
fn value(&self) -> &String {
return &self.value;
}
}
The DomainEvent
macro should be applied to a struct that represents a DomainEvent. It completely
implements all methods of the DomainEvent
trait, as long as some preconditions are met:
id
field of type Uuid
.aggregate_id
field of type Uuid
.occurred
field of type i64
.#[macro_use]
extern crate domain_derive;
use uuid::Uuid;
use domain_patterns::event::DomainEvent;
#[derive(Serialize, Clone, DomainEvent)]
pub struct FirstNameUpdatedEvent {
pub aggregate_id: Uuid,
pub first_name: String,
pub version: u64,
pub id: Uuid,
pub occurred: i64,
}
The DomainEvents
macro should be applied to an enum that holds variants which are all Domain Events.
This is a very thin wrapper, and all the macro does is check that the structure is an enum, and then applies
the trait, which has no methods.
#[macro_use]
extern crate domain_derive;
use uuid::Uuid;
use domain_patterns::event::{DomainEvent, DomainEvents};
#[derive(Serialize, Clone, DomainEvent)]
pub struct FirstNameUpdatedEvent {
pub aggregate_id: Uuid,
pub first_name: String,
pub version: u64,
pub id: Uuid,
pub occurred: i64,
}
#[derive(Clone, DomainEvents)]
pub enum UserEvents {
FirstNameUpdated(FirstNameUpdatedEvent),
}
License: MIT