lref 0.3.2

Rust Entity Framework - An EFCore-inspired ORM for Rust
Documentation
//! Entity type trait and state definitions.

use crate::error::LrefResult;
use crate::metadata::EntityTypeMeta;
use crate::provider::DbValue;
use std::collections::HashMap;

// ---------------------------------------------------------------------------
// Entity materialization — converts raw rows into entity instances
// ---------------------------------------------------------------------------

/// Materializes entities from raw database row data using the `IFromRow` trait.
pub fn materialize_entities<T: IEntityType + IFromRow>(rows: &[Vec<String>]) -> LrefResult<Vec<T>> {
    let mut entities = Vec::with_capacity(rows.len());
    for row in rows {
        let entity = T::from_row(row)?;
        entities.push(entity);
    }
    Ok(entities)
}

/// Represents the state of an entity in the change tracker.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EntityState {
    Detached,
    Added,
    Unchanged,
    Modified,
    Deleted,
}

/// The core trait that every entity type must implement.
pub trait IEntityType: Send + Sync + 'static {
    fn entity_meta() -> EntityTypeMeta
    where
        Self: Sized;

    fn entity_meta_instance(&self) -> EntityTypeMeta
    where
        Self: Sized,
    {
        Self::entity_meta()
    }
}

/// Trait for materializing an entity from a database row.
///
/// Used by [`materialize_entities`] to deserialize query results.
/// Auto-generated by `#[derive(EntityType)]`.
pub trait IFromRow: IEntityType + Sized {
    fn from_row(values: &[String]) -> LrefResult<Self>;
}

/// Extracts primary key values from an entity for SaveChanges WHERE clauses.
///
/// Auto-generated by `#[derive(IEntityType)]`.
pub trait IGetKeyValues: IEntityType {
    fn key_values(&self) -> HashMap<String, DbValue>;
}

/// Extracts all scalar property values from an entity for INSERT/UPDATE.
///
/// Auto-generated by `#[derive(IEntityType)]`.
pub trait IEntitySnapshot: IEntityType {
    fn snapshot(&self) -> HashMap<String, DbValue>;
}