asimov_signal_module/
db.rs1use super::SecretKey;
4use alloc::format;
5use asimov_module::secrecy::ExposeSecret;
6use rusqlite::{Connection, Result};
7
8#[derive(Debug)]
9pub struct SignalDb {
10 pub conn: Connection,
11}
12
13impl SignalDb {
14 #[cfg(feature = "std")]
15 pub fn open(path: impl AsRef<std::path::Path>) -> Result<Self> {
16 use rusqlite::OpenFlags;
17 let conn = Connection::open_with_flags(
18 path,
19 OpenFlags::SQLITE_OPEN_READ_ONLY
20 | OpenFlags::SQLITE_OPEN_URI
21 | OpenFlags::SQLITE_OPEN_NO_MUTEX
22 | OpenFlags::SQLITE_OPEN_PRIVATE_CACHE,
23 )?;
24 Ok(Self { conn })
25 }
26
27 pub fn decrypt(&self, key: SecretKey) -> Result<()> {
28 let ascii_key = hex::encode(key.expose_secret());
29 self.conn
30 .pragma_update(None, "key", format!("x'{}'", ascii_key))
31 }
32
33 pub fn is_readable(&self) -> bool {
34 self.conn
35 .query_row("SELECT count(*) FROM sqlite_master LIMIT 1", [], |_row| {
36 Ok(())
37 })
38 .is_ok()
39 }
40}