summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNora Widdecke <nora@sequoia-pgp.org>2022-11-24 17:02:26 +0100
committerNora Widdecke <nora@sequoia-pgp.org>2022-11-25 09:23:50 +0100
commit59c96603e54fd1316170427c5acc461ceeaf7646 (patch)
tree7d38835a3f67b87ff7ab843b59b58fe2b4063db2
parentd44df0617adc97716d2ca12ac6e4d46a08ceda80 (diff)
simplify anyhow handlingnora/public-store
-rw-r--r--public-store/src/error.rs19
-rw-r--r--public-store/src/store.rs36
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<dyn std::error::Error + Send + Sync>),
}
#[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<dyn std::error::Error + Send + Sync>;
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(<SequoiaOpenpgpError>::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(<SequoiaOpenpgpError>::from)?
.into_boxed_slice(),
f,
)?;
@@ -111,8 +106,8 @@ trait BasicStore {
/// Iterate over all certificates in the store
fn certs(&self) -> Box<dyn Iterator<Item = Result<Cert>> + '_> {
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(<SequoiaOpenpgpError>::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> {
- Cert::from_bytes(&bytes).map_err(|_| {
- OpenPgpError::FailedToParseCert {
- keyhandle: kh.to_owned(),
- }
- .into()
- })
+fn cert_from_bytes(bytes: &Box<[u8]>) -> Result<Cert> {
+ Cert::from_bytes(&bytes).map_err(|e| <SequoiaOpenpgpError>::from(e).into())
}
#[cfg(test)]