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

@@ -29,7 +29,7 @@ pub(crate) fn api_scope() -> Scope {
}
/// Convert a failure::Error to an Actix ISE
fn failure_to_ise(err: failure::Error) -> impl actix_web::ResponseError {
fn failure_to_ise(err: anyhow::Error) -> impl actix_web::ResponseError {
error::InternalError::new(err, StatusCode::INTERNAL_SERVER_ERROR)
}

View File

@@ -4,7 +4,6 @@ use crate::storage::{KVStorage, Storage};
use actix_web::{get, middleware::Logger, web, App, HttpServer, Responder, Scope};
use api::{api_scope, ServerState};
use clap::Arg;
use failure::Fallible;
mod api;
mod server;
@@ -27,7 +26,7 @@ pub(crate) fn app_scope(server_state: ServerState) -> Scope {
}
#[actix_web::main]
async fn main() -> Fallible<()> {
async fn main() -> anyhow::Result<()> {
env_logger::init();
let matches = clap::App::new("taskchampion-sync-server")
.version(env!("CARGO_PKG_VERSION"))

View File

@@ -1,7 +1,6 @@
//! This module implements the core logic of the server: handling transactions, upholding
//! invariants, and so on.
use crate::storage::{Client, StorageTxn};
use failure::Fallible;
use uuid::Uuid;
/// The distinguished value for "no version"
@@ -23,7 +22,7 @@ pub(crate) fn get_child_version<'a>(
mut txn: Box<dyn StorageTxn + 'a>,
client_key: ClientKey,
parent_version_id: VersionId,
) -> Fallible<Option<GetVersionResult>> {
) -> anyhow::Result<Option<GetVersionResult>> {
Ok(txn
.get_version_by_parent(client_key, parent_version_id)?
.map(|version| GetVersionResult {
@@ -48,7 +47,7 @@ pub(crate) fn add_version<'a>(
client: Client,
parent_version_id: VersionId,
history_segment: HistorySegment,
) -> Fallible<AddVersionResult> {
) -> anyhow::Result<AddVersionResult> {
log::debug!(
"add_version(client_key: {}, parent_version_id: {})",
client_key,
@@ -84,7 +83,7 @@ mod test {
use crate::storage::{InMemoryStorage, Storage};
#[test]
fn gcv_not_found() -> Fallible<()> {
fn gcv_not_found() -> anyhow::Result<()> {
let storage = InMemoryStorage::new();
let txn = storage.txn()?;
let client_key = Uuid::new_v4();
@@ -94,7 +93,7 @@ mod test {
}
#[test]
fn gcv_found() -> Fallible<()> {
fn gcv_found() -> anyhow::Result<()> {
let storage = InMemoryStorage::new();
let mut txn = storage.txn()?;
let client_key = Uuid::new_v4();
@@ -121,7 +120,7 @@ mod test {
}
#[test]
fn av_conflict() -> Fallible<()> {
fn av_conflict() -> anyhow::Result<()> {
let storage = InMemoryStorage::new();
let mut txn = storage.txn()?;
let client_key = Uuid::new_v4();
@@ -154,7 +153,7 @@ mod test {
Ok(())
}
fn test_av_success(latest_version_id_nil: bool) -> Fallible<()> {
fn test_av_success(latest_version_id_nil: bool) -> anyhow::Result<()> {
let storage = InMemoryStorage::new();
let mut txn = storage.txn()?;
let client_key = Uuid::new_v4();
@@ -198,12 +197,12 @@ mod test {
}
#[test]
fn av_success_with_existing_history() -> Fallible<()> {
fn av_success_with_existing_history() -> anyhow::Result<()> {
test_av_success(true)
}
#[test]
fn av_success_nil_latest_version_id() -> Fallible<()> {
fn av_success_nil_latest_version_id() -> anyhow::Result<()> {
test_av_success(false)
}
}

View File

@@ -1,5 +1,4 @@
use super::{Client, Storage, StorageTxn, Uuid, Version};
use failure::{format_err, Fallible};
use std::collections::HashMap;
use std::sync::{Mutex, MutexGuard};
@@ -28,19 +27,19 @@ struct InnerTxn<'a>(MutexGuard<'a, Inner>);
///
/// NOTE: this does not implement transaction rollback.
impl Storage for InMemoryStorage {
fn txn<'a>(&'a self) -> Fallible<Box<dyn StorageTxn + 'a>> {
fn txn<'a>(&'a self) -> anyhow::Result<Box<dyn StorageTxn + 'a>> {
Ok(Box::new(InnerTxn(self.0.lock().expect("poisoned lock"))))
}
}
impl<'a> StorageTxn for InnerTxn<'a> {
fn get_client(&mut self, client_key: Uuid) -> Fallible<Option<Client>> {
fn get_client(&mut self, client_key: Uuid) -> anyhow::Result<Option<Client>> {
Ok(self.0.clients.get(&client_key).cloned())
}
fn new_client(&mut self, client_key: Uuid, latest_version_id: Uuid) -> Fallible<()> {
fn new_client(&mut self, client_key: Uuid, latest_version_id: Uuid) -> anyhow::Result<()> {
if self.0.clients.get(&client_key).is_some() {
return Err(format_err!("Client {} already exists", client_key));
return Err(anyhow::anyhow!("Client {} already exists", client_key));
}
self.0
.clients
@@ -52,12 +51,12 @@ impl<'a> StorageTxn for InnerTxn<'a> {
&mut self,
client_key: Uuid,
latest_version_id: Uuid,
) -> Fallible<()> {
) -> anyhow::Result<()> {
if let Some(client) = self.0.clients.get_mut(&client_key) {
client.latest_version_id = latest_version_id;
Ok(())
} else {
Err(format_err!("Client {} does not exist", client_key))
Err(anyhow::anyhow!("Client {} does not exist", client_key))
}
}
@@ -65,7 +64,7 @@ impl<'a> StorageTxn for InnerTxn<'a> {
&mut self,
client_key: Uuid,
parent_version_id: Uuid,
) -> Fallible<Option<Version>> {
) -> anyhow::Result<Option<Version>> {
Ok(self
.0
.versions
@@ -79,7 +78,7 @@ impl<'a> StorageTxn for InnerTxn<'a> {
version_id: Uuid,
parent_version_id: Uuid,
history_segment: Vec<u8>,
) -> Fallible<()> {
) -> anyhow::Result<()> {
// TODO: verify it doesn't exist (`.entry`?)
let version = Version {
version_id,
@@ -92,7 +91,7 @@ impl<'a> StorageTxn for InnerTxn<'a> {
Ok(())
}
fn commit(&mut self) -> Fallible<()> {
fn commit(&mut self) -> anyhow::Result<()> {
Ok(())
}
}

View File

@@ -1,5 +1,4 @@
use super::{Client, Storage, StorageTxn, Uuid, Version};
use failure::Fallible;
use kv::msgpack::Msgpack;
use kv::{Bucket, Config, Error, Serde, Store, ValueBuf};
use std::path::Path;
@@ -29,7 +28,7 @@ pub(crate) struct KVStorage<'t> {
}
impl<'t> KVStorage<'t> {
pub fn new<P: AsRef<Path>>(directory: P) -> Fallible<KVStorage<'t>> {
pub fn new<P: AsRef<Path>>(directory: P) -> anyhow::Result<KVStorage<'t>> {
let mut config = Config::default(directory);
config.bucket("clients", None);
config.bucket("versions", None);
@@ -50,7 +49,7 @@ impl<'t> KVStorage<'t> {
}
impl<'t> Storage for KVStorage<'t> {
fn txn<'a>(&'a self) -> Fallible<Box<dyn StorageTxn + 'a>> {
fn txn<'a>(&'a self) -> anyhow::Result<Box<dyn StorageTxn + 'a>> {
Ok(Box::new(Txn {
storage: self,
txn: Some(self.store.write_txn()?),
@@ -82,7 +81,7 @@ impl<'t> Txn<'t> {
}
impl<'t> StorageTxn for Txn<'t> {
fn get_client(&mut self, client_key: Uuid) -> Fallible<Option<Client>> {
fn get_client(&mut self, client_key: Uuid) -> anyhow::Result<Option<Client>> {
let key = client_db_key(client_key);
let bucket = self.clients_bucket();
let kvtxn = self.kvtxn();
@@ -97,7 +96,7 @@ impl<'t> StorageTxn for Txn<'t> {
Ok(Some(client))
}
fn new_client(&mut self, client_key: Uuid, latest_version_id: Uuid) -> Fallible<()> {
fn new_client(&mut self, client_key: Uuid, latest_version_id: Uuid) -> anyhow::Result<()> {
let key = client_db_key(client_key);
let bucket = self.clients_bucket();
let kvtxn = self.kvtxn();
@@ -110,7 +109,7 @@ impl<'t> StorageTxn for Txn<'t> {
&mut self,
client_key: Uuid,
latest_version_id: Uuid,
) -> Fallible<()> {
) -> anyhow::Result<()> {
// implementation is the same as new_client..
self.new_client(client_key, latest_version_id)
}
@@ -119,7 +118,7 @@ impl<'t> StorageTxn for Txn<'t> {
&mut self,
client_key: Uuid,
parent_version_id: Uuid,
) -> Fallible<Option<Version>> {
) -> anyhow::Result<Option<Version>> {
let key = version_db_key(client_key, parent_version_id);
let bucket = self.versions_bucket();
let kvtxn = self.kvtxn();
@@ -139,7 +138,7 @@ impl<'t> StorageTxn for Txn<'t> {
version_id: Uuid,
parent_version_id: Uuid,
history_segment: Vec<u8>,
) -> Fallible<()> {
) -> anyhow::Result<()> {
let key = version_db_key(client_key, parent_version_id);
let bucket = self.versions_bucket();
let kvtxn = self.kvtxn();
@@ -152,7 +151,7 @@ impl<'t> StorageTxn for Txn<'t> {
Ok(())
}
fn commit(&mut self) -> Fallible<()> {
fn commit(&mut self) -> anyhow::Result<()> {
if let Some(kvtxn) = self.txn.take() {
kvtxn.commit()?;
} else {
@@ -165,11 +164,10 @@ impl<'t> StorageTxn for Txn<'t> {
#[cfg(test)]
mod test {
use super::*;
use failure::Fallible;
use tempdir::TempDir;
#[test]
fn test_get_client_empty() -> Fallible<()> {
fn test_get_client_empty() -> anyhow::Result<()> {
let tmp_dir = TempDir::new("test")?;
let storage = KVStorage::new(&tmp_dir.path())?;
let mut txn = storage.txn()?;
@@ -179,7 +177,7 @@ mod test {
}
#[test]
fn test_client_storage() -> Fallible<()> {
fn test_client_storage() -> anyhow::Result<()> {
let tmp_dir = TempDir::new("test")?;
let storage = KVStorage::new(&tmp_dir.path())?;
let mut txn = storage.txn()?;
@@ -201,7 +199,7 @@ mod test {
}
#[test]
fn test_gvbp_empty() -> Fallible<()> {
fn test_gvbp_empty() -> anyhow::Result<()> {
let tmp_dir = TempDir::new("test")?;
let storage = KVStorage::new(&tmp_dir.path())?;
let mut txn = storage.txn()?;
@@ -211,7 +209,7 @@ mod test {
}
#[test]
fn test_add_version_and_gvbp() -> Fallible<()> {
fn test_add_version_and_gvbp() -> anyhow::Result<()> {
let tmp_dir = TempDir::new("test")?;
let storage = KVStorage::new(&tmp_dir.path())?;
let mut txn = storage.txn()?;

View File

@@ -1,4 +1,3 @@
use failure::Fallible;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
@@ -24,24 +23,24 @@ pub(crate) struct Version {
pub(crate) trait StorageTxn {
/// Get information about the given client
fn get_client(&mut self, client_key: Uuid) -> Fallible<Option<Client>>;
fn get_client(&mut self, client_key: Uuid) -> anyhow::Result<Option<Client>>;
/// Create a new client with the given latest_version_id
fn new_client(&mut self, client_key: Uuid, latest_version_id: Uuid) -> Fallible<()>;
fn new_client(&mut self, client_key: Uuid, latest_version_id: Uuid) -> anyhow::Result<()>;
/// Set the client's latest_version_id
fn set_client_latest_version_id(
&mut self,
client_key: Uuid,
latest_version_id: Uuid,
) -> Fallible<()>;
) -> anyhow::Result<()>;
/// Get a version, indexed by parent version id
fn get_version_by_parent(
&mut self,
client_key: Uuid,
parent_version_id: Uuid,
) -> Fallible<Option<Version>>;
) -> anyhow::Result<Option<Version>>;
/// Add a version (that must not already exist)
fn add_version(
@@ -50,16 +49,16 @@ pub(crate) trait StorageTxn {
version_id: Uuid,
parent_version_id: Uuid,
history_segment: Vec<u8>,
) -> Fallible<()>;
) -> anyhow::Result<()>;
/// Commit any changes made in the transaction. It is an error to call this more than
/// once. It is safe to skip this call for read-only operations.
fn commit(&mut self) -> Fallible<()>;
fn commit(&mut self) -> anyhow::Result<()>;
}
/// A trait for objects able to act as storage. Most of the interesting behavior is in the
/// [`crate::storage::StorageTxn`] trait.
pub(crate) trait Storage: Send + Sync {
/// Begin a transaction
fn txn<'a>(&'a self) -> Fallible<Box<dyn StorageTxn + 'a>>;
fn txn<'a>(&'a self) -> anyhow::Result<Box<dyn StorageTxn + 'a>>;
}