use std::path::PathBuf;
pub struct StorePaths {
root: PathBuf,
}
impl StorePaths {
pub fn new(data_dir: impl Into<PathBuf>) -> Self {
Self {
root: data_dir.into(),
}
}
pub fn docs_root(&self) -> PathBuf {
self.root.join("docs")
}
pub fn doc_dir(&self, doc_id: &str) -> PathBuf {
self.docs_root().join(doc_id)
}
pub fn objects_dir(&self, doc_id: &str) -> PathBuf {
self.doc_dir(doc_id).join("objects")
}
pub fn versions_file(&self, doc_id: &str) -> PathBuf {
self.doc_dir(doc_id).join("versions.jsonl")
}
pub fn session_dir(&self, doc_id: &str) -> PathBuf {
self.doc_dir(doc_id).join("session")
}
pub fn meta_file(&self, doc_id: &str) -> PathBuf {
self.doc_dir(doc_id).join("meta.json")
}
pub fn runs_file(&self, doc_id: &str) -> PathBuf {
self.doc_dir(doc_id).join("runs.jsonl")
}
pub fn previews_file(&self, doc_id: &str) -> PathBuf {
self.doc_dir(doc_id).join("previews.jsonl")
}
pub fn scratch_dir(&self, doc_id: &str) -> PathBuf {
self.doc_dir(doc_id).join("scratch")
}
pub fn scratch_index(&self, doc_id: &str) -> PathBuf {
self.scratch_dir(doc_id).join("index.jsonl")
}
pub fn workspace_dir(&self, doc_id: &str) -> PathBuf {
self.doc_dir(doc_id).join("workspace")
}
pub fn workspace_renders_dir(&self, doc_id: &str) -> PathBuf {
self.workspace_dir(doc_id).join("renders")
}
}
#[cfg(test)]
mod tests {
use super::*;
fn paths() -> StorePaths {
StorePaths::new("/data")
}
#[test]
fn docs_root() {
assert_eq!(paths().docs_root(), PathBuf::from("/data/docs"));
}
#[test]
fn doc_dir() {
assert_eq!(paths().doc_dir("doc1"), PathBuf::from("/data/docs/doc1"));
}
#[test]
fn objects_dir() {
assert_eq!(
paths().objects_dir("doc1"),
PathBuf::from("/data/docs/doc1/objects")
);
}
#[test]
fn versions_file() {
assert_eq!(
paths().versions_file("doc1"),
PathBuf::from("/data/docs/doc1/versions.jsonl")
);
}
#[test]
fn session_dir() {
assert_eq!(
paths().session_dir("doc1"),
PathBuf::from("/data/docs/doc1/session")
);
}
#[test]
fn different_doc_ids_produce_different_paths() {
let p = paths();
assert_ne!(p.doc_dir("alpha"), p.doc_dir("beta"));
}
#[test]
fn meta_file() {
assert_eq!(
paths().meta_file("doc1"),
PathBuf::from("/data/docs/doc1/meta.json")
);
}
#[test]
fn runs_file() {
assert_eq!(
paths().runs_file("doc1"),
PathBuf::from("/data/docs/doc1/runs.jsonl")
);
}
#[test]
fn previews_file() {
assert_eq!(
paths().previews_file("doc1"),
PathBuf::from("/data/docs/doc1/previews.jsonl")
);
}
#[test]
fn scratch_dir() {
assert_eq!(
paths().scratch_dir("doc1"),
PathBuf::from("/data/docs/doc1/scratch")
);
}
#[test]
fn scratch_index() {
assert_eq!(
paths().scratch_index("doc1"),
PathBuf::from("/data/docs/doc1/scratch/index.jsonl")
);
}
#[test]
fn workspace_dir() {
assert_eq!(
paths().workspace_dir("doc1"),
PathBuf::from("/data/docs/doc1/workspace")
);
}
#[test]
fn workspace_renders_dir() {
assert_eq!(
paths().workspace_renders_dir("doc1"),
PathBuf::from("/data/docs/doc1/workspace/renders")
);
}
}