From 59c96603e54fd1316170427c5acc461ceeaf7646 Mon Sep 17 00:00:00 2001 From: Nora Widdecke Date: Thu, 24 Nov 2022 17:02:26 +0100 Subject: simplify anyhow handling --- public-store/src/error.rs | 19 +++---------------- public-store/src/store.rs | 36 ++++++++++++------------------------ 2 files changed, 15 insertions(+), 40 deletions(-) diff --git a/public-store/src/error.rs b/public-store/src/error.rs index e295888f..b1e2267f 100644 --- a/public-store/src/error.rs +++ b/public-store/src/error.rs @@ -22,9 +22,9 @@ pub enum Error { #[error("IO error")] IoError(#[from] std::io::Error), - /// A trust-root error + /// Other kind of Error #[error(transparent)] - OpenPgpError(#[from] OpenPgpError), + Other(#[from] Box), } #[derive(thiserror::Error, Debug)] @@ -42,17 +42,4 @@ pub enum TrustRootError { InvalidTrustRoot, } -#[derive(thiserror::Error, Debug)] -pub enum OpenPgpError { - /// Failed to parse a cert in the store - #[error("Failed to parse a cert in the store: {keyhandle}")] - FailedToParseCert { keyhandle: String }, - - /// Failed to export a cert - #[error("Failed to export a cert: {keyhandle}")] - FailedToExportCert { keyhandle: String }, - - /// Failed to serialize a cert - #[error("Failed to serialize a cert: {keyhandle}")] - FailedToSerializeCert { keyhandle: String }, -} +pub(crate) type SequoiaOpenpgpError = Box; diff --git a/public-store/src/store.rs b/public-store/src/store.rs index f23cedf5..c3d41af2 100644 --- a/public-store/src/store.rs +++ b/public-store/src/store.rs @@ -8,7 +8,7 @@ use openpgp::serialize::{Serialize, SerializeInto}; use openpgp::{Cert, KeyHandle}; use sequoia_openpgp as openpgp; -use crate::error::OpenPgpError; +use crate::error::SequoiaOpenpgpError; use crate::error::TrustRootError; use crate::{Error, Result}; @@ -60,7 +60,7 @@ trait BasicStore { keyhandle: kh.clone(), })?; - cert_from_bytes(&cert, &kh.to_hex()) + cert_from_bytes(&cert) } /// Query the store for a certificate's path by primary fingerprint. @@ -71,13 +71,10 @@ trait BasicStore { /// Export all certs in the store. fn export(&self, out: &mut dyn std::io::Write) -> Result<()> { for item in self.certd().iter() { - let (fp, _tag, cert) = item?; + let (_fp, _tag, cert) = item?; // Use export to remove non-exportable parts from certificates - let cert = cert_from_bytes(&cert, &fp)?; - cert.export(out) - .map_err(|_| OpenPgpError::FailedToExportCert { - keyhandle: fp.to_owned(), - })? + let cert = cert_from_bytes(&cert)?; + cert.export(out).map_err(::from)? } Ok(()) @@ -99,9 +96,7 @@ trait BasicStore { self.certd().insert( cert.to_vec() - .map_err(|_| OpenPgpError::FailedToSerializeCert { - keyhandle: cert.fingerprint().to_hex(), - })? + .map_err(::from)? .into_boxed_slice(), f, )?; @@ -111,8 +106,8 @@ trait BasicStore { /// Iterate over all certificates in the store fn certs(&self) -> Box> + '_> { let certs = self.certd().iter().map(|item| { - let (fp, _tag, data) = item?; - cert_from_bytes(&data, &fp) + let (_fp, _tag, data) = item?; + cert_from_bytes(&data) }); Box::new(certs) } @@ -217,9 +212,7 @@ impl Store { trust_root .as_tsk() .to_vec() - .map_err(|_| OpenPgpError::FailedToSerializeCert { - keyhandle: TRUST_ROOT.to_owned(), - })? + .map_err(::from)? .into_boxed_slice(), |ours, _theirs| Ok(ours), )?; @@ -248,19 +241,14 @@ impl StoreWithTrustRoot { .get(TRUST_ROOT) .transpose() .unwrap() - .map(|(_tag, data)| cert_from_bytes(&data, TRUST_ROOT))? + .map(|(_tag, data)| cert_from_bytes(&data))? } // add label, remove label, find by label } -fn cert_from_bytes(bytes: &Box<[u8]>, kh: &str) -> Result { - Cert::from_bytes(&bytes).map_err(|_| { - OpenPgpError::FailedToParseCert { - keyhandle: kh.to_owned(), - } - .into() - }) +fn cert_from_bytes(bytes: &Box<[u8]>) -> Result { + Cert::from_bytes(&bytes).map_err(|e| ::from(e).into()) } #[cfg(test)] -- cgit v1.2.3