aro-core 1.0.0

Core domain layer for the Aro web framework
Documentation
//! Entity marker trait for domain types.

use std::fmt::Debug;
use std::hash::Hash;

/// Marker trait for domain entities managed by a repository.
///
/// Implementors must specify an `Id` type used to uniquely identify each entity
/// and provide an accessor to retrieve it.
pub trait Entity {
    /// The type used to uniquely identify this entity.
    type Id: Debug + Clone + PartialEq + Eq + Hash + Send + Sync;

    /// Returns the unique identifier of this entity.
    fn id(&self) -> Self::Id;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug, Clone)]
    struct User {
        id: u64,
        name: String,
    }

    impl Entity for User {
        type Id = u64;

        fn id(&self) -> Self::Id {
            self.id
        }
    }

    #[test]
    fn entity_trait_compiles_with_simple_struct() {
        let user = User {
            id: 1,
            name: "Alice".to_string(),
        };
        // Verify the entity can be used with its Id type
        let id: <User as Entity>::Id = user.id();
        assert_eq!(id, 1);
        assert_eq!(user.name, "Alice");
    }
}