use crate::errors::{DbError, DbResult};
use std::path::{Path, PathBuf};
#[derive(Clone, Debug)]
pub struct Config {
pub path: PathBuf,
pub create_path: bool,
pub create_db: bool,
pub read_only: bool,
pub no_io: bool,
pub fs_locks: bool,
pub fs_locks_block: bool,
#[doc(hidden)]
pub schema_name: Option<String>,
}
impl Config {
fn base() -> Self {
Self {
path: Default::default(),
schema_name: None,
create_path: true,
create_db: true,
read_only: false,
no_io: false,
fs_locks: true,
fs_locks_block: false,
}
}
pub fn no_io() -> Self {
Self {
path: Default::default(),
schema_name: None,
create_path: false,
create_db: false,
read_only: true,
no_io: true,
fs_locks: false,
fs_locks_block: false,
}
}
pub fn in_folder<P>(p: P) -> Self
where
P: AsRef<Path>,
{
Self { path: PathBuf::from(p.as_ref()), ..Self::base() }
}
pub fn db_location_v2(&self) -> DbResult<PathBuf> {
let name = self.schema_name.as_ref().ok_or(DbError::Unexpected(
"Schema name not populated! db-rs-derive should have done this",
))?;
let mut pathbuf = self.path.clone();
pathbuf.push(format!("{name}.db"));
Ok(pathbuf)
}
pub fn db_location_v1(&self) -> DbResult<PathBuf> {
let name = self.schema_name.as_ref().ok_or(DbError::Unexpected(
"Schema name not populated! db-rs-derive should have done this",
))?;
let mut pathbuf = self.path.clone();
pathbuf.push(name);
Ok(pathbuf)
}
pub fn compaction_location(&self) -> DbResult<PathBuf> {
let name = self.schema_name.as_ref().ok_or(DbError::Unexpected(
"Schema name not populated! db-rs-derive should have done this",
))?;
let mut pathbuf = self.path.clone();
pathbuf.push(format!("{name}.db.tmp"));
Ok(pathbuf)
}
}