Skip to content

Commit

Permalink
Add optional error converts for popular crates
Browse files Browse the repository at this point in the history
  • Loading branch information
nlfiedler committed Feb 27, 2022
1 parent 3b1cd44 commit 3f58f03
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ This file follows the convention described at

## [3.0.0] - 2022-02-12
### BREAKING CHANGES
- Remove `failure` dependency and introduce `Error` type instead.
- Remove `failure` dependency and introduce new `Error` type.
* Implementations of `Document` must now return `mokuroku::Error`.
* Functions in `Database` now return `mokuroku::Error`.

## [2.7.0] - 2022-02-12
### Changed
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ exclude = [
]

[dependencies]
anyhow = { version = "1.0.55", optional = true }
rocksdb = "0.18.0"
thiserror = "1.0.30"
serde_cbor = { version = "0.11.2", optional = true }

[dev-dependencies]
chrono = { version = "0.4", features = ["serde"] }
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ for result in results {
}
```

## Features

### Optional features

Mokuroku supports several optional features to reduce the burden of using this
crate with other popular crates.

* `anyhow`: Enable auto-conversion of `anyhow::Error` to `mokuroku::Error`
* `serde_cbor`: Enable auto-conversion of `serde_cbor::Error` to `mokuroku::Error`

## Design

### Terminology
Expand Down
38 changes: 24 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,32 @@ use std::convert::TryInto;
use std::fmt;
use std::path::{Path, PathBuf};

/// This type represents all possible errors that can occur within this crate.
#[derive(thiserror::Error, Debug)]
pub enum Error {
/// The 'changes' column family is missing from the database.
#[error("missing changes column family")]
MissingChanges,
#[error("missing column family {0}")]
MissingColumn(String),
/// The column family for the named view is missing from the database.
#[error("missing view column family")]
MissingView,
MissingView(String),
/// An error occurred within the rocksdb crate.
#[error("RocksDB database error")]
RocksDB(#[from] rocksdb::Error),
/// Error related to serialization or deserialization.
#[error("serialization error: {0}")]
Serde(String),
/// Error occurred within the serde_cbor crate.
#[cfg(feature = "serde_cbor")]
#[error("serde_cbor error: {0}")]
CBOR(#[from] serde_cbor::Error),
/// Error in conversion of bytes to a UTF-8 encoded string.
#[error("UTF-8 conversion error")]
Utf8(#[from] std::str::Utf8Error),
#[error("unknown data store error")]
Unknown,
/// A catch-all error type from a commonly used crate.
#[cfg(feature = "anyhow")]
#[error("anyhow error: {0}")]
Anyhow(#[from] anyhow::Error),
}

pub mod base32;
Expand Down Expand Up @@ -495,7 +505,7 @@ impl Database {
let cf = self
.db
.cf_handle(&mrview)
.ok_or_else(|| Error::MissingView)?;
.ok_or_else(|| Error::MissingView(view.to_owned()))?;
let iter = self.db.iterator_cf(cf, IteratorMode::Start);
Ok(QueryIterator::new(&self.db, iter, &self.key_sep, &cf))
}
Expand All @@ -517,7 +527,7 @@ impl Database {
let cf = self
.db
.cf_handle(&mrview)
.ok_or_else(|| Error::MissingView)?;
.ok_or_else(|| Error::MissingView(view.to_owned()))?;
let iter = self.db.prefix_iterator_cf(cf, key.as_ref());
Ok(QueryIterator::new_prefix(
&self.db,
Expand Down Expand Up @@ -594,7 +604,7 @@ impl Database {
let cf = self
.db
.cf_handle(&mrview)
.ok_or_else(|| Error::MissingView)?;
.ok_or_else(|| Error::MissingView(view.to_owned()))?;
let iter = self.db.prefix_iterator_cf(cf, &prefix);
Ok(QueryIterator::new_prefix(
&self.db,
Expand Down Expand Up @@ -624,7 +634,7 @@ impl Database {
let cf = self
.db
.cf_handle(&mrview)
.ok_or_else(|| Error::MissingView)?;
.ok_or_else(|| Error::MissingView(view.to_owned()))?;
let iter = self
.db
.iterator_cf(cf, IteratorMode::From(key_a.as_ref(), Direction::Forward));
Expand Down Expand Up @@ -654,7 +664,7 @@ impl Database {
let cf = self
.db
.cf_handle(&mrview)
.ok_or_else(|| Error::MissingView)?;
.ok_or_else(|| Error::MissingView(view.to_owned()))?;
let iter = self
.db
.iterator_cf(cf, IteratorMode::From(key.as_ref(), Direction::Forward));
Expand All @@ -678,7 +688,7 @@ impl Database {
let cf = self
.db
.cf_handle(&mrview)
.ok_or_else(|| Error::MissingView)?;
.ok_or_else(|| Error::MissingView(view.to_owned()))?;
let iter = self.db.iterator_cf(cf, IteratorMode::Start);
Ok(QueryIterator::new_range(
&self.db,
Expand All @@ -704,7 +714,7 @@ impl Database {
let cf = self
.db
.cf_handle(&mrview)
.ok_or_else(|| Error::MissingView)?;
.ok_or_else(|| Error::MissingView(view.to_owned()))?;
let iter = self
.db
.iterator_cf(cf, IteratorMode::From(key.as_ref(), Direction::Reverse));
Expand Down Expand Up @@ -789,7 +799,7 @@ impl Database {
let cf = self
.db
.cf_handle(&mrview)
.ok_or_else(|| Error::MissingView)?;
.ok_or_else(|| Error::MissingView(view.to_owned()))?;
let iter = self.db.iterator(IteratorMode::Start);
for (key, value) in iter {
let emitter = Emitter::new(&self.db, &key, cf, &self.key_sep);
Expand Down Expand Up @@ -1585,7 +1595,7 @@ mod tests {
mrview.push_str(view);
let result = db
.cf_handle(&mrview)
.ok_or_else(|| Error::MissingColumn(view.to_owned()));
.ok_or_else(|| Error::MissingView(view.to_owned()));
assert!(result.is_ok());
let cf = result.unwrap();
let iter = db.iterator_cf(cf, IteratorMode::Start);
Expand Down

0 comments on commit 3f58f03

Please sign in to comment.