use std::path::{Path, PathBuf};
use crate::error::SillokError;
use crate::storage::sql::store::SqlStore;
use crate::storage::store::ArchiveStore;
#[derive(Debug, Clone)]
pub struct StoreHandle {
path: PathBuf,
}
impl StoreHandle {
pub fn new(path: PathBuf) -> Self {
Self { path }
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn is_legacy_path(&self) -> bool {
self.path
.file_name()
.and_then(|value| value.to_str())
.is_some_and(|value| value.ends_with(".slk.zst"))
}
pub fn legacy(&self) -> ArchiveStore {
ArchiveStore::new(self.path.clone())
}
pub fn sql(&self) -> SqlStore {
SqlStore::new(self.path.clone())
}
pub fn require_sql_mutation(&self) -> Result<SqlStore, SillokError> {
if self.is_legacy_path() {
Err(SillokError::new(
"migration_required",
format!(
"`{}` is a legacy archive; run `sillok migrate --store {}` first",
self.path.display(),
self.path.display()
),
))
} else {
Ok(self.sql())
}
}
}