use std::path::{Path, PathBuf};
use mentra::runtime::FileRuntimeStore;
use crate::{error::RunError, store};
use super::RuntimeBuilder;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum History {
Directory(PathBuf),
Ephemeral,
}
impl History {
pub(super) fn open(posture: Option<&Self>) -> Result<Option<FileRuntimeStore>, RunError> {
match posture {
Some(Self::Directory(dir)) => Ok(Some(store::store_in(dir)?)),
Some(Self::Ephemeral) => Ok(None),
None => {
store::store_in(&store::default_directory())?;
Ok(None)
}
}
}
pub(super) fn transcripts(posture: Option<&Self>) -> PathBuf {
match posture {
Some(Self::Directory(dir)) => store::transcripts_in(dir),
Some(Self::Ephemeral) => store::volatile_transcripts(),
None => store::default_transcripts(),
}
}
}
impl RuntimeBuilder {
pub fn with_store_dir(self, dir: impl Into<PathBuf>) -> Self {
Self {
history: Some(History::Directory(dir.into())),
..self
}
}
pub fn with_ephemeral_history(self) -> Self {
Self {
history: Some(History::Ephemeral),
..self
}
}
pub(crate) fn named_store_dir(&self) -> Option<&Path> {
match &self.history {
Some(History::Directory(dir)) => Some(dir),
_ => None,
}
}
}