xmtp_db/encrypted_store/
database.rs

1#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
2pub mod native;
3
4use diesel::SqliteConnection;
5#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
6pub use native::*;
7
8#[cfg(all(target_family = "wasm", target_os = "unknown"))]
9pub mod wasm;
10#[cfg(all(target_family = "wasm", target_os = "unknown"))]
11pub use wasm::*;
12
13#[cfg(all(target_family = "wasm", target_os = "unknown"))]
14pub use wasm_exports::*;
15
16#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
17pub use native_exports::*;
18
19use super::ConnectionExt;
20
21#[cfg(all(target_family = "wasm", target_os = "unknown"))]
22pub mod wasm_exports {
23    pub type RawDbConnection = diesel::prelude::SqliteConnection;
24    pub type DefaultDatabase = super::wasm::WasmDb;
25}
26
27#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
28pub mod native_exports {
29    pub type DefaultDatabase = super::native::NativeDb;
30    pub use super::native::EncryptedConnection;
31}
32
33mod instrumentation;
34
35#[derive(Debug)]
36pub enum PersistentOrMem<P, M> {
37    Persistent(P),
38    Mem(M),
39}
40
41// P and M must share connection & error types
42impl<P, M> ConnectionExt for PersistentOrMem<P, M>
43where
44    P: ConnectionExt,
45    M: ConnectionExt,
46{
47    fn raw_query_read<T, F>(&self, fun: F) -> Result<T, crate::ConnectionError>
48    where
49        F: FnOnce(&mut SqliteConnection) -> Result<T, diesel::result::Error>,
50        Self: Sized,
51    {
52        match self {
53            Self::Persistent(p) => p.raw_query_read(fun),
54            Self::Mem(m) => m.raw_query_read(fun),
55        }
56    }
57
58    fn raw_query_write<T, F>(&self, fun: F) -> Result<T, crate::ConnectionError>
59    where
60        F: FnOnce(&mut SqliteConnection) -> Result<T, diesel::result::Error>,
61        Self: Sized,
62    {
63        match self {
64            Self::Persistent(p) => p.raw_query_write(fun),
65            Self::Mem(m) => m.raw_query_write(fun),
66        }
67    }
68
69    fn disconnect(&self) -> Result<(), crate::ConnectionError> {
70        match self {
71            Self::Persistent(p) => p.disconnect(),
72            Self::Mem(m) => m.disconnect(),
73        }
74    }
75
76    fn reconnect(&self) -> Result<(), crate::ConnectionError> {
77        match self {
78            Self::Persistent(p) => p.reconnect(),
79            Self::Mem(m) => m.reconnect(),
80        }
81    }
82}