summaryrefslogtreecommitdiff
path: root/src/store.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-02-03 09:11:49 +0200
committerLars Wirzenius <liw@liw.fi>2021-02-04 09:14:01 +0200
commita2adcb5a90c15b473a2fcf114555443fba8a20ce (patch)
tree7ec36f244daa105b0da774d6705ef736f9135f64 /src/store.rs
parentbf08ea67ca035fc0e78364450599cefff7cd9bc6 (diff)
downloadobnam2-a2adcb5a90c15b473a2fcf114555443fba8a20ce.tar.gz
refactor: have per-module error enums
This means that a function that parses step bindings can't return an error that the document is missing a title. Such an error return would be nonsensical, and we use the Rust type system to prevent it, at a small cost of being a bit verbose. Additional benefit is that the library portion of Obnam doesn't return anyhow::Result values anymore.
Diffstat (limited to 'src/store.rs')
-rw-r--r--src/store.rs18
1 files changed, 10 insertions, 8 deletions
diff --git a/src/store.rs b/src/store.rs
index e6cc71f..fca2c13 100644
--- a/src/store.rs
+++ b/src/store.rs
@@ -1,7 +1,6 @@
use crate::chunk::DataChunk;
use crate::chunkid::ChunkId;
use crate::chunkmeta::ChunkMeta;
-use anyhow::Context;
use std::path::{Path, PathBuf};
/// Store chunks, with metadata, persistently.
@@ -13,6 +12,12 @@ pub struct Store {
dir: PathBuf,
}
+/// An error from a `Store` operation.
+pub type StoreError = std::io::Error;
+
+/// A result from an `Store` operation.
+pub type StoreResult<T> = Result<T, StoreError>;
+
impl Store {
/// Create a new Store to represent on-disk storage of chunks.x
pub fn new(dir: &Path) -> Self {
@@ -38,14 +43,11 @@ impl Store {
}
/// Save a chunk into a store.
- pub fn save(&self, id: &ChunkId, meta: &ChunkMeta, chunk: &DataChunk) -> anyhow::Result<()> {
+ pub fn save(&self, id: &ChunkId, meta: &ChunkMeta, chunk: &DataChunk) -> StoreResult<()> {
let (dir, metaname, dataname) = &self.filenames(id);
if !dir.exists() {
- let res = std::fs::create_dir_all(dir).into();
- if let Err(_) = res {
- return res.with_context(|| format!("creating directory {}", dir.display()));
- }
+ std::fs::create_dir_all(dir)?;
}
std::fs::write(&metaname, meta.to_json())?;
@@ -54,7 +56,7 @@ impl Store {
}
/// Load a chunk from a store.
- pub fn load(&self, id: &ChunkId) -> anyhow::Result<DataChunk> {
+ pub fn load(&self, id: &ChunkId) -> StoreResult<DataChunk> {
let (_, _, dataname) = &self.filenames(id);
let data = std::fs::read(&dataname)?;
let data = DataChunk::new(data);
@@ -62,7 +64,7 @@ impl Store {
}
/// Delete a chunk from a store.
- pub fn delete(&self, id: &ChunkId) -> anyhow::Result<()> {
+ pub fn delete(&self, id: &ChunkId) -> StoreResult<()> {
let (_, metaname, dataname) = &self.filenames(id);
std::fs::remove_file(&metaname)?;
std::fs::remove_file(&dataname)?;