use crate::config::Config;
use crate::error::AppError;
use sled::Db;
use std::path::PathBuf;
use std::{env, fs, str};
pub fn open(config: Config) -> Result<Db, AppError> {
let db_path = match env::var("SHOTEXT_DB_PATH") {
Ok(path_str) => PathBuf::from(path_str),
Err(_) => config.paths.database.clone(),
};
if let Some(parent) = db_path.parent() {
fs::create_dir_all(parent)?;
}
sled::open(db_path).map_err(AppError::from)
}
pub fn key_exists(db: &Db, key: &str) -> Result<bool, AppError> {
db.contains_key(key).map_err(AppError::from)
}