core_crypto_keystore/traits/entity.rs
1use std::borrow::Borrow;
2
3use async_trait::async_trait;
4use rusqlite::Connection;
5
6use crate::{
7 CryptoKeystoreResult,
8 traits::{
9 EntityBase, KeyType, OwnedKeyType,
10 primary_key::{BorrowPrimaryKey, PrimaryKey},
11 },
12};
13
14/// Something which can be stored in our database.
15///
16/// It has a primary key, which uniquely identifies it.
17#[cfg_attr(target_os = "unknown", async_trait(?Send))]
18#[cfg_attr(not(target_os = "unknown"), async_trait)]
19pub trait Entity: EntityBase + PrimaryKey {
20 /// Get an entity by its primary key.
21 ///
22 /// For entites whose primary key has a distinct borrowed type, it is best to implement this as a direct
23 /// passthrough:
24 ///
25 /// ```rust,ignore
26 /// async fn get(conn: &mut Self::ConnectionType, key: &Self::PrimaryKey) -> CoreCryptoKeystoreResult<Option<Self>> {
27 /// Self::get_borrowed(conn, key).await
28 /// }
29 /// ```
30 async fn get(conn: &mut Self::ConnectionType, key: &Self::PrimaryKey) -> CryptoKeystoreResult<Option<Self>>;
31
32 /// Count the number of entities of this type in the database.
33 async fn count(conn: &mut Self::ConnectionType) -> CryptoKeystoreResult<u32>;
34
35 /// Retrieve all entities of this type from the database.
36 async fn load_all(conn: &mut Self::ConnectionType) -> CryptoKeystoreResult<Vec<Self>>;
37}
38
39#[cfg_attr(target_os = "unknown", async_trait(?Send))]
40#[cfg_attr(not(target_os = "unknown"), async_trait)]
41pub trait EntityGetBorrowed: Entity + BorrowPrimaryKey {
42 /// Get an entity by a borrowed form of its primary key.
43 async fn get_borrowed(
44 conn: &mut Self::ConnectionType,
45 key: &Self::BorrowedPrimaryKey,
46 ) -> CryptoKeystoreResult<Option<Self>>
47 where
48 for<'pk> &'pk Self::BorrowedPrimaryKey: KeyType;
49}
50
51/// Something which can be stored in our database.
52///
53/// It has a primary key, which uniquely identifies it.
54//
55// During WPB-23775 we need to rename this and all other `Unified*` traits to just the bare form
56pub trait UnifiedEntity: PrimaryKey + Sized {
57 /// The table name for this entity
58 const COLLECTION_NAME: &'static str;
59
60 /// Get an entity by its primary key.
61 ///
62 /// For entites whose primary key has a distinct borrowed type, it is best to implement this as a direct
63 /// passthrough:
64 ///
65 /// ```rust,ignore
66 /// fn get(conn: &Connection, key: &Self::PrimaryKey) -> CoreCryptoKeystoreResult<Option<Self>> {
67 /// Self::get_borrowed(conn, key).await
68 /// }
69 /// ```
70 fn get(conn: &Connection, key: &Self::PrimaryKey) -> CryptoKeystoreResult<Option<Self>>;
71
72 /// Count the number of entities of this type in the database.
73 fn count(conn: &Connection) -> CryptoKeystoreResult<u32>;
74
75 /// Retrieve all entities of this type from the database.
76 fn load_all(conn: &Connection) -> CryptoKeystoreResult<Vec<Self>>;
77}
78
79pub trait UnifiedEntityGetBorrowed: UnifiedEntity + BorrowPrimaryKey {
80 /// Get an entity by a borrowed form of its primary key.
81 fn get_borrowed(conn: &Connection, key: &Self::BorrowedPrimaryKey) -> CryptoKeystoreResult<Option<Self>>
82 where
83 for<'pk> &'pk Self::BorrowedPrimaryKey: KeyType;
84}