Open and close sqlite db properly
This commit is contained in:
parent
e65bf49a6e
commit
9d1810eceb
4 changed files with 165 additions and 0 deletions
|
|
@ -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" }
|
||||
|
|
|
|||
|
|
@ -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(())
|
||||
}
|
||||
|
|
|
|||
44
cove-tui/src/vault.rs
Normal file
44
cove-tui/src/vault.rs
Normal file
|
|
@ -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<Request>,
|
||||
}
|
||||
|
||||
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<Request>) -> 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<Vault> {
|
||||
// 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 })
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue