Skip to main content

core_crypto_keystore/traits/
entity.rs

1use rusqlite::Connection;
2
3use crate::{
4    CryptoKeystoreResult,
5    traits::primary_key::{BorrowPrimaryKey, PrimaryKey},
6};
7
8/// Something which can be stored in our database.
9///
10/// It has a primary key, which uniquely identifies it.
11pub trait Entity: PrimaryKey + Sized {
12    /// The name of the SQL table for this entity
13    const TABLE_NAME: &'static str;
14
15    /// Get an entity by its primary key.
16    ///
17    /// For entites whose primary key has a distinct borrowed type, it is best to implement this as a direct
18    /// passthrough:
19    ///
20    /// ```rust,ignore
21    /// fn get(conn: &Connection, key: &Self::PrimaryKey) -> CryptoKeystoreResult<Option<Self>> {
22    ///     <Self as EntityGetBorrowed>::get_borrowed(conn, key)
23    /// }
24    /// ```
25    fn get(conn: &Connection, key: &Self::PrimaryKey) -> CryptoKeystoreResult<Option<Self>>;
26
27    /// Count the number of entities of this type in the database.
28    fn count(conn: &Connection) -> CryptoKeystoreResult<u32>;
29
30    /// Retrieve all entities of this type from the database.
31    fn load_all(conn: &Connection) -> CryptoKeystoreResult<Vec<Self>>;
32}
33
34pub trait EntityGetBorrowed: Entity + BorrowPrimaryKey {
35    /// Get an entity by a borrowed form of its primary key.
36    fn get_borrowed(conn: &Connection, key: &Self::BorrowedPrimaryKey) -> CryptoKeystoreResult<Option<Self>>;
37}