use rusqlite::Connection;
use std::path::{Path, PathBuf};
pub fn connect_db(path: Option<&Path>, create_db: bool) -> Result<Connection, DBError> {
let path = path
.unwrap_or(&PathBuf::from("~/.local/share/keyboxen.db"))
.canonicalize()?;
if !create_db && !path.is_file() {
Err(DBError::NotFound(PathBuf::from(&path)))?;
}
Ok(Connection::open(&path)?)
}
#[derive(Debug, thiserror::Error)]
pub enum DBError {
#[error(transparent)]
PathError(#[from] std::io::Error),
#[error("DB file '{0}' not found")]
NotFound(PathBuf),
#[error(transparent)]
ConnectError(#[from] rusqlite::Error),
#[error(transparent)]
CloseError(rusqlite::Error),
}