use std::path::{Path, PathBuf};
use crate::provenance::Provenance;
use crate::storage::{CommitId, MemWriterError};
use crate::vcs::CommitContext;
pub trait MemBackend: Send + Sync {
fn list_entities(&self) -> Result<Vec<PathBuf>, BackendError>;
fn read_entity(&self, rel_path: &Path) -> Result<Option<Vec<u8>>, BackendError>;
fn write_entity(&self, rel_path: &Path, content: &[u8]) -> Result<(), BackendError>;
fn delete_entity(&self, rel_path: &Path) -> Result<(), BackendError>;
fn move_entity(&self, from: &Path, to: &Path) -> Result<(), BackendError>;
fn discard_pending(&self) -> Result<(), BackendError> {
Ok(())
}
fn commit(&self, message: &str, ctx: &CommitContext<'_>) -> Result<CommitId, BackendError>;
fn commit_with_expected_parent(
&self,
message: &str,
ctx: &CommitContext<'_>,
_expected_parent: Option<&str>,
) -> Result<CommitId, BackendError> {
self.commit(message, ctx)
}
fn append_provenance(&self, record: &Provenance) -> Result<(), BackendError>;
fn read_provenance(&self, cursor: Option<&str>) -> Result<Vec<Provenance>, BackendError>;
fn current_head(&self) -> Result<Option<String>, BackendError> {
Ok(None)
}
fn read_mem_config(&self) -> Result<Option<Vec<u8>>, BackendError> {
Ok(None)
}
fn read_archive_provenance(&self) -> Result<Option<Vec<u8>>, BackendError> {
Ok(None)
}
fn write_mem_config(&self, _bytes: &[u8]) -> Result<(), BackendError> {
Err(BackendError::Sealed)
}
fn write_mem_config_with_note(
&self,
bytes: &[u8],
_note: Option<&str>,
) -> Result<(), BackendError> {
self.write_mem_config(bytes)
}
fn delete_artifacts(&self) -> Result<(), BackendError> {
Ok(())
}
}
#[derive(Debug, thiserror::Error)]
pub enum BackendError {
#[error(transparent)]
MemWriter(#[from] MemWriterError),
#[error("backend is sealed (writes rejected)")]
Sealed,
#[error("backend io error: {0}")]
Io(#[from] std::io::Error),
#[error("backend error: {0}")]
Other(String),
#[error(
"parent-ref mismatch: expected {expected}, found {actual} — sibling writer advanced the mem"
)]
ParentMismatch { expected: String, actual: String },
}