mod parquet;
mod partitions;
mod sync_db;
pub use parquet::ParquetStore;
pub use partitions::EntityType;
pub use sync_db::SyncDb;
use std::path::PathBuf;
use crate::error::Result;
pub fn default_storage_path() -> PathBuf {
dirs::data_local_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join("garmin")
}
pub fn default_sync_db_path() -> PathBuf {
default_storage_path().join("sync.db")
}
pub struct Storage {
pub parquet: ParquetStore,
pub sync_db: SyncDb,
}
impl Storage {
pub fn open_default() -> Result<Self> {
let base_path = default_storage_path();
Self::open(base_path)
}
pub fn open(base_path: PathBuf) -> Result<Self> {
std::fs::create_dir_all(&base_path).map_err(|e| {
crate::error::GarminError::Database(format!(
"Failed to create storage directory: {}",
e
))
})?;
let parquet = ParquetStore::new(&base_path);
let sync_db = SyncDb::open(base_path.join("sync.db"))?;
Ok(Self { parquet, sync_db })
}
pub fn open_in_memory(temp_path: PathBuf) -> Result<Self> {
std::fs::create_dir_all(&temp_path).map_err(|e| {
crate::error::GarminError::Database(format!("Failed to create temp directory: {}", e))
})?;
let parquet = ParquetStore::new(&temp_path);
let sync_db = SyncDb::open_in_memory()?;
Ok(Self { parquet, sync_db })
}
pub fn clone_with_new_db(&self) -> Result<Self> {
let parquet = self.parquet.clone();
let sync_db = SyncDb::open(self.base_path().join("sync.db"))?;
Ok(Self { parquet, sync_db })
}
pub fn base_path(&self) -> &std::path::Path {
self.parquet.base_path()
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn test_storage_open() {
let temp = TempDir::new().unwrap();
let storage = Storage::open(temp.path().to_path_buf()).unwrap();
assert!(storage.base_path().exists());
}
}