From 9d1810eceb613fd435d3b91d3424b8de3177bbce Mon Sep 17 00:00:00 2001 From: Joscha Date: Wed, 15 Jun 2022 21:52:05 +0200 Subject: [PATCH] Open and close sqlite db properly --- Cargo.lock | 111 ++++++++++++++++++++++++++++++++++++++++++ cove-tui/Cargo.toml | 2 + cove-tui/src/main.rs | 8 +++ cove-tui/src/vault.rs | 44 +++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 cove-tui/src/vault.rs diff --git a/Cargo.lock b/Cargo.lock index 810c36e..b93a0a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,17 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ahash" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + [[package]] name = "aho-corasick" version = "0.7.18" @@ -176,7 +187,9 @@ dependencies = [ "async-trait", "chrono", "crossterm", + "directories", "parking_lot", + "rusqlite", "tokio", "toss", ] @@ -244,6 +257,26 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "directories" +version = "4.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f51c5d4ddabd36886dd3e1438cb358cdcb0d7c499cb99cb4ac2e38e18b5cb210" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "env_logger" version = "0.9.0" @@ -257,6 +290,18 @@ dependencies = [ "termcolor", ] +[[package]] +name = "fallible-iterator" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" + [[package]] name = "fnv" version = "1.0.7" @@ -383,6 +428,24 @@ dependencies = [ "wasi 0.10.0+wasi-snapshot-preview1", ] +[[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +dependencies = [ + "ahash", +] + +[[package]] +name = "hashlink" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7249a3129cbc1ffccd74857f81464a323a152173cdb134e0fd81bc803b29facf" +dependencies = [ + "hashbrown", +] + [[package]] name = "hermit-abi" version = "0.1.19" @@ -462,6 +525,16 @@ version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" +[[package]] +name = "libsqlite3-sys" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "898745e570c7d0453cc1fbc4a701eb6c662ed54e8fec8b7d14be137ebeeb9d14" +dependencies = [ + "pkg-config", + "vcpkg", +] + [[package]] name = "lock_api" version = "0.4.7" @@ -593,6 +666,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" + [[package]] name = "ppv-lite86" version = "0.2.16" @@ -656,6 +735,17 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom", + "redox_syscall", + "thiserror", +] + [[package]] name = "regex" version = "1.5.6" @@ -688,6 +778,21 @@ dependencies = [ "winapi", ] +[[package]] +name = "rusqlite" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85127183a999f7db96d1a976a309eebbfb6ea3b0b400ddd8340190129de6eb7a" +dependencies = [ + "bitflags", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink", + "libsqlite3-sys", + "memchr", + "smallvec", +] + [[package]] name = "rustls" version = "0.20.6" @@ -1128,6 +1233,12 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.4" diff --git a/cove-tui/Cargo.toml b/cove-tui/Cargo.toml index 4acf8df..e890e66 100644 --- a/cove-tui/Cargo.toml +++ b/cove-tui/Cargo.toml @@ -8,6 +8,8 @@ anyhow = "1.0.57" async-trait = "0.1.56" chrono = "0.4.19" crossterm = "0.23.2" +directories = "4.0.1" parking_lot = "0.12.1" +rusqlite = "0.27.0" tokio = { version = "1.19.2", features = ["full"] } toss = { git = "https://github.com/Garmelon/toss.git", rev = "333cf74fba56080043a13b9f55c0b62695e2fa4a" } diff --git a/cove-tui/src/main.rs b/cove-tui/src/main.rs index 86b2ba0..8452781 100644 --- a/cove-tui/src/main.rs +++ b/cove-tui/src/main.rs @@ -3,14 +3,22 @@ mod chat; mod store; mod ui; +mod vault; +use directories::ProjectDirs; use toss::terminal::Terminal; use ui::Ui; #[tokio::main] async fn main() -> anyhow::Result<()> { + let dirs = ProjectDirs::from("de", "plugh", "cove").expect("unable to determine directories"); + println!("Data dir: {}", dirs.data_dir().to_string_lossy()); + + let vault = vault::launch(&dirs.data_dir().join("vault.db"))?; let mut terminal = Terminal::new()?; // terminal.set_measuring(true); + Ui::run(&mut terminal).await?; + vault.close().await; Ok(()) } diff --git a/cove-tui/src/vault.rs b/cove-tui/src/vault.rs new file mode 100644 index 0000000..e51eb0c --- /dev/null +++ b/cove-tui/src/vault.rs @@ -0,0 +1,44 @@ +use std::path::Path; +use std::{fs, thread}; + +use rusqlite::Connection; +use tokio::sync::{mpsc, oneshot}; + +enum Request { + Close(oneshot::Sender<()>), + Nop, +} + +pub struct Vault { + tx: mpsc::Sender, +} + +impl Vault { + pub async fn close(&self) { + let (tx, rx) = oneshot::channel(); + let _ = self.tx.send(Request::Close(tx)).await; + let _ = rx.await; + } +} + +fn run(conn: Connection, mut rx: mpsc::Receiver) -> anyhow::Result<()> { + while let Some(request) = rx.blocking_recv() { + match request { + // Drops the Sender resulting in `Vault::close` exiting + Request::Close(_) => break, + Request::Nop => {} + } + } + Ok(()) +} + +pub fn launch(path: &Path) -> rusqlite::Result { + // If this fails, rusqlite will complain about not being able to open the db + // file, which saves me from adding a separate vault error type. + let _ = fs::create_dir_all(path.parent().expect("path to file")); + + let conn = Connection::open(path)?; + let (tx, rx) = mpsc::channel(8); + thread::spawn(move || run(conn, rx)); + Ok(Vault { tx }) +}