index_store: use custom error type for write errors

Public APIs should use custom error types (not `io::Error` as
here). The caller isn't affected by this commit because it just
unwraps the error.
This commit is contained in:
Martin von Zweigbergk 2023-02-26 12:15:27 -08:00 committed by Martin von Zweigbergk
parent 2cc15f40ef
commit da1c259211

View file

@ -21,6 +21,7 @@ use std::sync::Arc;
use itertools::Itertools;
use tempfile::NamedTempFile;
use thiserror::Error;
use crate::backend::CommitId;
use crate::commit::Commit;
@ -31,6 +32,12 @@ use crate::op_store::OperationId;
use crate::operation::Operation;
use crate::store::Store;
#[derive(Debug, Error)]
pub enum IndexWriteError {
#[error("{0}")]
Other(String),
}
pub struct IndexStore {
dir: PathBuf,
}
@ -79,9 +86,16 @@ impl IndexStore {
&self,
index: MutableIndex,
op_id: &OperationId,
) -> io::Result<Arc<ReadonlyIndex>> {
let index = index.save_in(self.dir.clone())?;
self.associate_file_with_operation(&index, op_id)?;
) -> Result<Arc<ReadonlyIndex>, IndexWriteError> {
let index = index.save_in(self.dir.clone()).map_err(|err| {
IndexWriteError::Other(format!("Failed to write commit index file: {err:?}"))
})?;
self.associate_file_with_operation(&index, op_id)
.map_err(|err| {
IndexWriteError::Other(format!(
"Failed to associate commit index file with a operation {op_id:?}: {err:?}"
))
})?;
Ok(index)
}