Implement basic data directory operations

This commit is contained in:
Joscha 2025-02-18 23:21:12 +01:00
parent 58cd4d7517
commit 909399b276
8 changed files with 284 additions and 0 deletions

42
gdn/src/data.rs Normal file
View file

@ -0,0 +1,42 @@
mod datadir;
mod lockfile;
mod v0;
mod v1;
use std::path::PathBuf;
use anyhow::Context;
use directories::ProjectDirs;
pub use self::{
datadir::{LockedDataDir, UnlockedDataDir},
v1::{load_state, save_state, VERSION},
};
fn migrate(dir: &LockedDataDir) -> anyhow::Result<()> {
loop {
match dir.read_version()? {
0 => v0::migrate(dir)?,
_ => break Ok(()),
}
}
}
pub fn open(path: PathBuf) -> anyhow::Result<UnlockedDataDir> {
let dir = UnlockedDataDir::new(path);
dir.require_version(VERSION)?;
Ok(dir)
}
pub fn open_and_migrate(path: PathBuf) -> anyhow::Result<LockedDataDir> {
let dir = UnlockedDataDir::new(path).lock()?;
migrate(&dir)?;
dir.require_version(VERSION)?;
Ok(dir)
}
pub fn path() -> anyhow::Result<PathBuf> {
let dirs = ProjectDirs::from("de", "plugh", crate::TECHNICAL_NAME)
.context("failed to locate data dir")?;
Ok(dirs.data_dir().to_path_buf())
}