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,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