keymgr: Create a KeyStoreError trait for keystore errors.

The new `BoxedError` type will replace `tor_keymgr::Error`.

Part of #901
This commit is contained in:
Gabriela Moldovan 2023-06-20 15:29:15 +01:00
parent 08d1155cb9
commit 7c023f2ec2
4 changed files with 36 additions and 1 deletions

1
Cargo.lock generated
View File

@ -4522,6 +4522,7 @@ name = "tor-keymgr"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"derive_more", "derive_more",
"dyn-clone",
"fs-mistrust", "fs-mistrust",
"ssh-key", "ssh-key",
"thiserror", "thiserror",

View File

@ -23,6 +23,7 @@ __is_experimental = []
[dependencies] [dependencies]
derive_more = "0.99.3" derive_more = "0.99.3"
dyn-clone = "1.0.11"
fs-mistrust = { path = "../fs-mistrust", version = "0.7.1", features = ["serde", "walkdir"] } fs-mistrust = { path = "../fs-mistrust", version = "0.7.1", features = ["serde", "walkdir"] }
ssh-key = { version = "0.5.1", features = ["std"] } ssh-key = { version = "0.5.1", features = ["std"] }
thiserror = "1" thiserror = "1"

View File

@ -4,11 +4,44 @@ use tor_error::{ErrorKind, HasKind};
#[cfg(feature = "keymgr")] #[cfg(feature = "keymgr")]
use {crate::key_type::ssh::SshKeyAlgorithm, crate::KeyType}; use {crate::key_type::ssh::SshKeyAlgorithm, crate::KeyType};
use dyn_clone::DynClone;
use thiserror::Error; use thiserror::Error;
#[cfg(feature = "keymgr")] #[cfg(feature = "keymgr")]
use {std::io, std::path::PathBuf, std::sync::Arc}; 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<dyn KeystoreError>;
/// 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<dyn StdError> + DynClone + fmt::Debug + fmt::Display + Send + Sync + 'static
{
}
// Generate a Clone impl for Box<dyn KeystoreError>
dyn_clone::clone_trait_object!(KeystoreError);
impl<K: KeystoreError + Send + Sync> From<K> 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. /// A key store error.
// //
// TODO hs: refactor this error type. // TODO hs: refactor this error type.

View File

@ -53,7 +53,7 @@ mod mgr;
#[cfg(not(feature = "keymgr"))] #[cfg(not(feature = "keymgr"))]
mod dummy; mod dummy;
pub use err::Error; pub use err::{Error, KeystoreError};
pub use key_specifier::{ArtiPath, CTorPath, KeySpecifier}; pub use key_specifier::{ArtiPath, CTorPath, KeySpecifier};
#[cfg(feature = "keymgr")] #[cfg(feature = "keymgr")]