From 7c023f2ec282e56e047372d514d152166fddf6b4 Mon Sep 17 00:00:00 2001 From: Gabriela Moldovan Date: Tue, 20 Jun 2023 15:29:15 +0100 Subject: [PATCH] keymgr: Create a KeyStoreError trait for keystore errors. The new `BoxedError` type will replace `tor_keymgr::Error`. Part of #901 --- Cargo.lock | 1 + crates/tor-keymgr/Cargo.toml | 1 + crates/tor-keymgr/src/err.rs | 33 +++++++++++++++++++++++++++++++++ crates/tor-keymgr/src/lib.rs | 2 +- 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index ee0377f93..609b117b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4522,6 +4522,7 @@ name = "tor-keymgr" version = "0.1.0" dependencies = [ "derive_more", + "dyn-clone", "fs-mistrust", "ssh-key", "thiserror", diff --git a/crates/tor-keymgr/Cargo.toml b/crates/tor-keymgr/Cargo.toml index b1fcfdb58..4b9b82ce0 100644 --- a/crates/tor-keymgr/Cargo.toml +++ b/crates/tor-keymgr/Cargo.toml @@ -23,6 +23,7 @@ __is_experimental = [] [dependencies] derive_more = "0.99.3" +dyn-clone = "1.0.11" fs-mistrust = { path = "../fs-mistrust", version = "0.7.1", features = ["serde", "walkdir"] } ssh-key = { version = "0.5.1", features = ["std"] } thiserror = "1" diff --git a/crates/tor-keymgr/src/err.rs b/crates/tor-keymgr/src/err.rs index 653bd88d4..640ca54d8 100644 --- a/crates/tor-keymgr/src/err.rs +++ b/crates/tor-keymgr/src/err.rs @@ -4,11 +4,44 @@ use tor_error::{ErrorKind, HasKind}; #[cfg(feature = "keymgr")] use {crate::key_type::ssh::SshKeyAlgorithm, crate::KeyType}; +use dyn_clone::DynClone; use thiserror::Error; #[cfg(feature = "keymgr")] use {std::io, std::path::PathBuf, std::sync::Arc}; +use std::error::Error as StdError; +use std::fmt; + +/// An Error type for this crate. +// TODO hs: replace Error with BoxedError +#[allow(unreachable_pub)] +pub type BoxedError = Box; + +/// An error returned by a [`KeyStore`](crate::KeyStore). +// TODO hs: replace Error with KeyStoreError and create an `ArtiNativeKeyStoreError: KeyStoreError` +// type for ArtiNativeKeyStore. +pub trait KeystoreError: + HasKind + AsRef + DynClone + fmt::Debug + fmt::Display + Send + Sync + 'static +{ +} + +// Generate a Clone impl for Box +dyn_clone::clone_trait_object!(KeystoreError); + +impl From for BoxedError { + fn from(k: K) -> Self { + Box::new(k) + } +} + +impl StdError for BoxedError { + fn source(&self) -> Option<&(dyn StdError + 'static)> { + let e: &dyn StdError = self; + e.source() + } +} + /// A key store error. // // TODO hs: refactor this error type. diff --git a/crates/tor-keymgr/src/lib.rs b/crates/tor-keymgr/src/lib.rs index d4ee20121..2a7c8c39c 100644 --- a/crates/tor-keymgr/src/lib.rs +++ b/crates/tor-keymgr/src/lib.rs @@ -53,7 +53,7 @@ mod mgr; #[cfg(not(feature = "keymgr"))] mod dummy; -pub use err::Error; +pub use err::{Error, KeystoreError}; pub use key_specifier::{ArtiPath, CTorPath, KeySpecifier}; #[cfg(feature = "keymgr")]