Expand description
Derive macros for ddd-toolkit-core’s value-object-shaped traits:
ValueObject, EntityId,
SecretVo, and EnumVo. Works
alongside ddd-toolkit-core directly, or through the ddd-toolkit
facade crate - the generated code resolves the right crate path either
way, so it doesn’t matter which one the caller’s Cargo.toml names.
Three of the four derives (ValueObject,
EntityId, SecretVo) apply to
a tuple struct with exactly one non-pub field, e.g. struct Email(String);. The fourth, EnumVo, applies to a
fieldless enum instead. All four accept #[vo(...)] attributes to
configure the generated code; each derive’s own docs list which
attributes it accepts and why the others are rejected.
§Example
use ddd_toolkit_core::domain::{ValidationError, Wrapped};
use ddd_toolkit_macro::ValueObject;
#[derive(ValueObject, Clone, PartialEq, Debug)]
#[vo(validate = "validate_email")]
struct Email(String);
fn validate_email(s: &str) -> Result<(), ValidationError> {
if s.contains('@') {
Ok(())
} else {
Err(ValidationError::new("Email", "missing @"))
}
}
let email = Email::try_from("a@b.com".to_string())?;
assert_eq!(email.as_inner(), "a@b.com");
assert!(Email::try_from("no-at-sign".to_string()).is_err());Derive Macros§
- Entity
Id - Derives
EntityId- and everything#[derive(ValueObject)]derives - for a single-field tuple struct. - EnumVo
- Derives
EnumVo,Display, andFromStrfor a fieldless enum. - Secret
Vo - Derives
SecretVofor a single-field tuple struct, with aDebugimpl that always redacts the wrapped value. - Value
Object - Derives
ValueObject,Wrapped,TryFrom<Inner>,From<Self> for Inner, andDisplayfor a single-field tuple struct.