Replace 'failure' crate with anyhow+thiserror

Closes #148
This commit is contained in:
dbr
2021-03-25 16:33:35 +11:00
parent 3cccdc7e32
commit 4d9755c43b
41 changed files with 255 additions and 316 deletions

View File

@@ -1,6 +1,5 @@
use super::types::Server;
use super::{LocalServer, RemoteServer};
use failure::Fallible;
use std::path::PathBuf;
use uuid::Uuid;
@@ -27,7 +26,7 @@ pub enum ServerConfig {
impl ServerConfig {
/// Get a server based on this configuration
pub fn into_server(self) -> Fallible<Box<dyn Server>> {
pub fn into_server(self) -> anyhow::Result<Box<dyn Server>> {
Ok(match self {
ServerConfig::Local { server_dir } => Box::new(LocalServer::new(server_dir)?),
ServerConfig::Remote {

View File

@@ -2,7 +2,6 @@ use crate::server::{
AddVersionResult, GetVersionResult, HistorySegment, Server, VersionId, NO_VERSION_ID,
};
use crate::utils::Key;
use failure::Fallible;
use kv::msgpack::Msgpack;
use kv::{Bucket, Config, Error, Integer, Serde, Store, ValueBuf};
use serde::{Deserialize, Serialize};
@@ -25,7 +24,7 @@ pub struct LocalServer<'t> {
impl<'t> LocalServer<'t> {
/// A test server has no notion of clients, signatures, encryption, etc.
pub fn new<P: AsRef<Path>>(directory: P) -> Fallible<LocalServer<'t>> {
pub fn new<P: AsRef<Path>>(directory: P) -> anyhow::Result<LocalServer<'t>> {
let mut config = Config::default(directory);
config.bucket("versions", None);
config.bucket("numbers", None);
@@ -48,7 +47,7 @@ impl<'t> LocalServer<'t> {
})
}
fn get_latest_version_id(&mut self) -> Fallible<VersionId> {
fn get_latest_version_id(&mut self) -> anyhow::Result<VersionId> {
let txn = self.store.read_txn()?;
let base_version = match txn.get(&self.latest_version_bucket, 0.into()) {
Ok(buf) => buf,
@@ -60,7 +59,7 @@ impl<'t> LocalServer<'t> {
Ok(base_version as VersionId)
}
fn set_latest_version_id(&mut self, version_id: VersionId) -> Fallible<()> {
fn set_latest_version_id(&mut self, version_id: VersionId) -> anyhow::Result<()> {
let mut txn = self.store.write_txn()?;
txn.set(
&self.latest_version_bucket,
@@ -74,7 +73,7 @@ impl<'t> LocalServer<'t> {
fn get_version_by_parent_version_id(
&mut self,
parent_version_id: VersionId,
) -> Fallible<Option<Version>> {
) -> anyhow::Result<Option<Version>> {
let txn = self.store.read_txn()?;
let version = match txn.get(&self.versions_bucket, parent_version_id.into()) {
@@ -87,7 +86,7 @@ impl<'t> LocalServer<'t> {
Ok(Some(version))
}
fn add_version_by_parent_version_id(&mut self, version: Version) -> Fallible<()> {
fn add_version_by_parent_version_id(&mut self, version: Version) -> anyhow::Result<()> {
let mut txn = self.store.write_txn()?;
txn.set(
&self.versions_bucket,
@@ -109,7 +108,7 @@ impl<'t> Server for LocalServer<'t> {
&mut self,
parent_version_id: VersionId,
history_segment: HistorySegment,
) -> Fallible<AddVersionResult> {
) -> anyhow::Result<AddVersionResult> {
// no client lookup
// no signature validation
@@ -133,7 +132,7 @@ impl<'t> Server for LocalServer<'t> {
}
/// Get a vector of all versions after `since_version`
fn get_child_version(&mut self, parent_version_id: VersionId) -> Fallible<GetVersionResult> {
fn get_child_version(&mut self, parent_version_id: VersionId) -> anyhow::Result<GetVersionResult> {
if let Some(version) = self.get_version_by_parent_version_id(parent_version_id)? {
Ok(GetVersionResult::Version {
version_id: version.version_id,
@@ -149,11 +148,10 @@ impl<'t> Server for LocalServer<'t> {
#[cfg(test)]
mod test {
use super::*;
use failure::Fallible;
use tempdir::TempDir;
#[test]
fn test_empty() -> Fallible<()> {
fn test_empty() -> anyhow::Result<()> {
let tmp_dir = TempDir::new("test")?;
let mut server = LocalServer::new(&tmp_dir.path())?;
let child_version = server.get_child_version(NO_VERSION_ID)?;
@@ -162,7 +160,7 @@ mod test {
}
#[test]
fn test_add_zero_base() -> Fallible<()> {
fn test_add_zero_base() -> anyhow::Result<()> {
let tmp_dir = TempDir::new("test")?;
let mut server = LocalServer::new(&tmp_dir.path())?;
let history = b"1234".to_vec();
@@ -187,7 +185,7 @@ mod test {
}
#[test]
fn test_add_nonzero_base() -> Fallible<()> {
fn test_add_nonzero_base() -> anyhow::Result<()> {
let tmp_dir = TempDir::new("test")?;
let mut server = LocalServer::new(&tmp_dir.path())?;
let history = b"1234".to_vec();
@@ -215,7 +213,7 @@ mod test {
}
#[test]
fn test_add_nonzero_base_forbidden() -> Fallible<()> {
fn test_add_nonzero_base_forbidden() -> anyhow::Result<()> {
let tmp_dir = TempDir::new("test")?;
let mut server = LocalServer::new(&tmp_dir.path())?;
let history = b"1234".to_vec();

View File

@@ -1,5 +1,4 @@
use crate::server::HistorySegment;
use failure::{format_err, Fallible};
use std::convert::TryFrom;
use std::io::Read;
use tindercrypt::cryptors::RingCryptor;
@@ -27,7 +26,7 @@ pub(super) struct HistoryCleartext {
impl HistoryCleartext {
/// Seal the payload into its ciphertext
pub(super) fn seal(self, secret: &Secret) -> Fallible<HistoryCiphertext> {
pub(super) fn seal(self, secret: &Secret) -> anyhow::Result<HistoryCiphertext> {
let cryptor = RingCryptor::new().with_aad(self.parent_version_id.as_bytes());
let ciphertext = cryptor.seal_with_passphrase(secret.as_ref(), &self.history_segment)?;
Ok(HistoryCiphertext(ciphertext))
@@ -42,7 +41,7 @@ impl HistoryCiphertext {
self,
secret: &Secret,
parent_version_id: Uuid,
) -> Fallible<HistoryCleartext> {
) -> anyhow::Result<HistoryCleartext> {
let cryptor = RingCryptor::new().with_aad(parent_version_id.as_bytes());
let plaintext = cryptor.open(secret.as_ref(), &self.0)?;
@@ -54,16 +53,16 @@ impl HistoryCiphertext {
}
impl TryFrom<ureq::Response> for HistoryCiphertext {
type Error = failure::Error;
type Error = anyhow::Error;
fn try_from(resp: ureq::Response) -> Result<HistoryCiphertext, failure::Error> {
fn try_from(resp: ureq::Response) -> Result<HistoryCiphertext, anyhow::Error> {
if let Some("application/vnd.taskchampion.history-segment") = resp.header("Content-Type") {
let mut reader = resp.into_reader();
let mut bytes = vec![];
reader.read_to_end(&mut bytes)?;
Ok(Self(bytes))
} else {
Err(format_err!("Response did not have expected content-type"))
Err(anyhow::anyhow!("Response did not have expected content-type"))
}
}
}

View File

@@ -1,5 +1,4 @@
use crate::server::{AddVersionResult, GetVersionResult, HistorySegment, Server, VersionId};
use failure::{format_err, Fallible};
use std::convert::TryInto;
use uuid::Uuid;
@@ -31,8 +30,8 @@ impl RemoteServer {
}
/// Convert a ureq::Response to an Error
fn resp_to_error(resp: ureq::Response) -> failure::Error {
return format_err!(
fn resp_to_error(resp: ureq::Response) -> anyhow::Error {
return anyhow::anyhow!(
"error {}: {}",
resp.status(),
resp.into_string()
@@ -41,12 +40,12 @@ fn resp_to_error(resp: ureq::Response) -> failure::Error {
}
/// Read a UUID-bearing header or fail trying
fn get_uuid_header(resp: &ureq::Response, name: &str) -> Fallible<Uuid> {
fn get_uuid_header(resp: &ureq::Response, name: &str) -> anyhow::Result<Uuid> {
let value = resp
.header(name)
.ok_or_else(|| format_err!("Response does not have {} header", name))?;
.ok_or_else(|| anyhow::anyhow!("Response does not have {} header", name))?;
let value = Uuid::parse_str(value)
.map_err(|e| format_err!("{} header is not a valid UUID: {}", name, e))?;
.map_err(|e| anyhow::anyhow!("{} header is not a valid UUID: {}", name, e))?;
Ok(value)
}
@@ -55,7 +54,7 @@ impl Server for RemoteServer {
&mut self,
parent_version_id: VersionId,
history_segment: HistorySegment,
) -> Fallible<AddVersionResult> {
) -> anyhow::Result<AddVersionResult> {
let url = format!("{}/client/add-version/{}", self.origin, parent_version_id);
let history_cleartext = HistoryCleartext {
parent_version_id,
@@ -84,7 +83,7 @@ impl Server for RemoteServer {
}
}
fn get_child_version(&mut self, parent_version_id: VersionId) -> Fallible<GetVersionResult> {
fn get_child_version(&mut self, parent_version_id: VersionId) -> anyhow::Result<GetVersionResult> {
let url = format!(
"{}/client/get-child-version/{}",
self.origin, parent_version_id

View File

@@ -1,7 +1,6 @@
use crate::server::{
AddVersionResult, GetVersionResult, HistorySegment, Server, VersionId, NO_VERSION_ID,
};
use failure::Fallible;
use std::collections::HashMap;
use uuid::Uuid;
@@ -34,7 +33,7 @@ impl Server for TestServer {
&mut self,
parent_version_id: VersionId,
history_segment: HistorySegment,
) -> Fallible<AddVersionResult> {
) -> anyhow::Result<AddVersionResult> {
// no client lookup
// no signature validation
@@ -64,7 +63,7 @@ impl Server for TestServer {
}
/// Get a vector of all versions after `since_version`
fn get_child_version(&mut self, parent_version_id: VersionId) -> Fallible<GetVersionResult> {
fn get_child_version(&mut self, parent_version_id: VersionId) -> anyhow::Result<GetVersionResult> {
if let Some(version) = self.versions.get(&parent_version_id) {
Ok(GetVersionResult::Version {
version_id: version.version_id,

View File

@@ -1,4 +1,3 @@
use failure::Fallible;
use uuid::Uuid;
/// Versions are referred to with sha2 hashes.
@@ -41,8 +40,8 @@ pub trait Server {
&mut self,
parent_version_id: VersionId,
history_segment: HistorySegment,
) -> Fallible<AddVersionResult>;
) -> anyhow::Result<AddVersionResult>;
/// Get the version with the given parent VersionId
fn get_child_version(&mut self, parent_version_id: VersionId) -> Fallible<GetVersionResult>;
fn get_child_version(&mut self, parent_version_id: VersionId) -> anyhow::Result<GetVersionResult>;
}