diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-02-03 09:11:49 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-02-04 09:14:01 +0200 |
commit | a2adcb5a90c15b473a2fcf114555443fba8a20ce (patch) | |
tree | 7ec36f244daa105b0da774d6705ef736f9135f64 /src/indexedstore.rs | |
parent | bf08ea67ca035fc0e78364450599cefff7cd9bc6 (diff) | |
download | obnam2-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/indexedstore.rs')
-rw-r--r-- | src/indexedstore.rs | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/src/indexedstore.rs b/src/indexedstore.rs index 0366013..3f347dd 100644 --- a/src/indexedstore.rs +++ b/src/indexedstore.rs @@ -1,8 +1,8 @@ use crate::chunk::DataChunk; use crate::chunkid::ChunkId; use crate::chunkmeta::ChunkMeta; -use crate::index::Index; -use crate::store::Store; +use crate::index::{Index, IndexError}; +use crate::store::{Store, StoreError}; use std::path::Path; /// A store for chunks and their metadata. @@ -14,43 +14,58 @@ pub struct IndexedStore { index: Index, } +/// All the errors that may be returned for `IndexStore`. +#[derive(Debug, thiserror::Error)] +pub enum IndexedError { + /// An error from Index. + #[error(transparent)] + IndexError(#[from] IndexError), + + /// An error from Store. + #[error(transparent)] + SqlError(#[from] StoreError), +} + +/// A result from an `Index` operation. +pub type IndexedResult<T> = Result<T, IndexedError>; + impl IndexedStore { - pub fn new(dirname: &Path) -> anyhow::Result<Self> { + pub fn new(dirname: &Path) -> IndexedResult<Self> { let store = Store::new(dirname); let index = Index::new(dirname)?; Ok(Self { store, index }) } - pub fn save(&mut self, meta: &ChunkMeta, chunk: &DataChunk) -> anyhow::Result<ChunkId> { + pub fn save(&mut self, meta: &ChunkMeta, chunk: &DataChunk) -> IndexedResult<ChunkId> { let id = ChunkId::new(); self.store.save(&id, meta, chunk)?; self.insert_meta(&id, meta)?; Ok(id) } - fn insert_meta(&mut self, id: &ChunkId, meta: &ChunkMeta) -> anyhow::Result<()> { + fn insert_meta(&mut self, id: &ChunkId, meta: &ChunkMeta) -> IndexedResult<()> { self.index.insert_meta(id.clone(), meta.clone())?; Ok(()) } - pub fn load(&self, id: &ChunkId) -> anyhow::Result<(DataChunk, ChunkMeta)> { + pub fn load(&self, id: &ChunkId) -> IndexedResult<(DataChunk, ChunkMeta)> { Ok((self.store.load(id)?, self.load_meta(id)?)) } - pub fn load_meta(&self, id: &ChunkId) -> anyhow::Result<ChunkMeta> { - self.index.get_meta(id) + pub fn load_meta(&self, id: &ChunkId) -> IndexedResult<ChunkMeta> { + Ok(self.index.get_meta(id)?) } - pub fn find_by_sha256(&self, sha256: &str) -> anyhow::Result<Vec<ChunkId>> { - self.index.find_by_sha256(sha256) + pub fn find_by_sha256(&self, sha256: &str) -> IndexedResult<Vec<ChunkId>> { + Ok(self.index.find_by_sha256(sha256)?) } - pub fn find_generations(&self) -> anyhow::Result<Vec<ChunkId>> { - self.index.find_generations() + pub fn find_generations(&self) -> IndexedResult<Vec<ChunkId>> { + Ok(self.index.find_generations()?) } - pub fn remove(&mut self, id: &ChunkId) -> anyhow::Result<()> { - self.index.remove_meta(id).unwrap(); + pub fn remove(&mut self, id: &ChunkId) -> IndexedResult<()> { + self.index.remove_meta(id)?; self.store.delete(id)?; Ok(()) } |