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

@@ -7,9 +7,9 @@ Typical uses of this crate do not interact directly with this module; [`StorageC
However, users who wish to implement their own storage backends can implement the traits defined here and pass the result to [`Replica`](crate::Replica).
*/
use failure::Fallible;
use std::collections::HashMap;
use uuid::Uuid;
use anyhow::Result;
mod config;
mod inmemory;
@@ -55,66 +55,66 @@ pub(crate) const DEFAULT_BASE_VERSION: Uuid = crate::server::NO_VERSION_ID;
/// It is safe and performant to drop transactions that did not modify any data without committing.
pub trait StorageTxn {
/// Get an (immutable) task, if it is in the storage
fn get_task(&mut self, uuid: Uuid) -> Fallible<Option<TaskMap>>;
fn get_task(&mut self, uuid: Uuid) -> Result<Option<TaskMap>>;
/// Create an (empty) task, only if it does not already exist. Returns true if
/// the task was created (did not already exist).
fn create_task(&mut self, uuid: Uuid) -> Fallible<bool>;
fn create_task(&mut self, uuid: Uuid) -> Result<bool>;
/// Set a task, overwriting any existing task. If the task does not exist, this implicitly
/// creates it (use `get_task` to check first, if necessary).
fn set_task(&mut self, uuid: Uuid, task: TaskMap) -> Fallible<()>;
fn set_task(&mut self, uuid: Uuid, task: TaskMap) -> Result<()>;
/// Delete a task, if it exists. Returns true if the task was deleted (already existed)
fn delete_task(&mut self, uuid: Uuid) -> Fallible<bool>;
fn delete_task(&mut self, uuid: Uuid) -> Result<bool>;
/// Get the uuids and bodies of all tasks in the storage, in undefined order.
fn all_tasks(&mut self) -> Fallible<Vec<(Uuid, TaskMap)>>;
fn all_tasks(&mut self) -> Result<Vec<(Uuid, TaskMap)>>;
/// Get the uuids of all tasks in the storage, in undefined order.
fn all_task_uuids(&mut self) -> Fallible<Vec<Uuid>>;
fn all_task_uuids(&mut self) -> Result<Vec<Uuid>>;
/// Get the current base_version for this storage -- the last version synced from the server.
fn base_version(&mut self) -> Fallible<VersionId>;
fn base_version(&mut self) -> Result<VersionId>;
/// Set the current base_version for this storage.
fn set_base_version(&mut self, version: VersionId) -> Fallible<()>;
fn set_base_version(&mut self, version: VersionId) -> Result<()>;
/// Get the current set of outstanding operations (operations that have not been sync'd to the
/// server yet)
fn operations(&mut self) -> Fallible<Vec<Operation>>;
fn operations(&mut self) -> Result<Vec<Operation>>;
/// Add an operation to the end of the list of operations in the storage. Note that this
/// merely *stores* the operation; it is up to the TaskDB to apply it.
fn add_operation(&mut self, op: Operation) -> Fallible<()>;
fn add_operation(&mut self, op: Operation) -> Result<()>;
/// Replace the current list of operations with a new list.
fn set_operations(&mut self, ops: Vec<Operation>) -> Fallible<()>;
fn set_operations(&mut self, ops: Vec<Operation>) -> Result<()>;
/// Get the entire working set, with each task UUID at its appropriate (1-based) index.
/// Element 0 is always None.
fn get_working_set(&mut self) -> Fallible<Vec<Option<Uuid>>>;
fn get_working_set(&mut self) -> Result<Vec<Option<Uuid>>>;
/// Add a task to the working set and return its (one-based) index. This index will be one greater
/// than the highest used index.
fn add_to_working_set(&mut self, uuid: Uuid) -> Fallible<usize>;
fn add_to_working_set(&mut self, uuid: Uuid) -> Result<usize>;
/// Update the working set task at the given index. This cannot add a new item to the
/// working set.
fn set_working_set_item(&mut self, index: usize, uuid: Option<Uuid>) -> Fallible<()>;
fn set_working_set_item(&mut self, index: usize, uuid: Option<Uuid>) -> Result<()>;
/// Clear all tasks from the working set in preparation for a garbage-collection operation.
/// Note that this is the only way items are removed from the set.
fn clear_working_set(&mut self) -> Fallible<()>;
fn clear_working_set(&mut self) -> Result<()>;
/// Commit any changes made in the transaction. It is an error to call this more than
/// once.
fn commit(&mut self) -> Fallible<()>;
fn commit(&mut self) -> Result<()>;
}
/// A trait for objects able to act as task storage. Most of the interesting behavior is in the
/// [`crate::storage::StorageTxn`] trait.
pub trait Storage {
/// Begin a transaction
fn txn<'a>(&'a mut self) -> Fallible<Box<dyn StorageTxn + 'a>>;
fn txn<'a>(&'a mut self) -> Result<Box<dyn StorageTxn + 'a>>;
}