noema 0.1.5

Noema IOC and DI framework for Rust
Documentation
/// A trait representing a Value Object in Domain-Driven Design (DDD).
/// A value object is an immutable type that is defined by its attributes rather than a unique identity.
/// Can be used to encapsulate simple data types with validation logic like Emails or Ids.
/// # Examples
/// ```ignore
/// struct Email {
///     value: String,
/// }
/// impl ValueObject for Email {
///     type Value = String;
///
///     fn validate(value: Self::Value) -> Result<(), Box<dyn std::error::Error>> {
///         if value.contains('@') {
///             Ok(())
///         } else {
///             Err("Invalid email format".into())
///         }
///     }
///
///     fn value(&self) -> Self::Value {
///         self.value.clone()
///     }
/// }
/// impl Email {
///     fn new(value: String) -> Result<Self, Box<dyn std::error::Error>> {
///         let email = Email { value };
///         email.validate()?;
///         Ok(email)
///     }
/// }
/// ````
pub trait ValueObject {
    type Value: Clone + PartialEq + Eq;
    fn validate(value: Self::Value) -> Result<(), Box<dyn std::error::Error>>;
    fn value(&self) -> Self::Value;
}