mithril_persistence/sqlite/
entity.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use sqlite::Row;
use thiserror::Error;

use super::Projection;

/// SqLite hydration error
#[derive(Error, Debug)]
pub enum HydrationError {
    /// data do not conform to expectations
    #[error("data do not conform to expectations: {0}")]
    InvalidData(String),
}

/// How to hydrate an entity from a SQLite result row
pub trait SqLiteEntity {
    /// This method is intended to be used when creating new instances from SQL
    /// result rows. This is the place to grab data, check consistency and types
    /// and return the entity if possible.
    fn hydrate(row: Row) -> Result<Self, HydrationError>
    where
        Self: Sized;

    /// Construct a [Projection] that will allow to hydrate this `SqLiteEntity`.
    fn get_projection() -> Projection;
}