1
//! An error type for [`ArtiEphemeralKeystore`](crate::ArtiEphemeralKeystore).
2

            
3
use std::sync::Arc;
4

            
5
use tor_error::{ErrorKind, HasKind};
6

            
7
use crate::KeystoreError;
8

            
9
/// An error returned by [`ArtiEphemeralKeystore`](crate::ArtiEphemeralKeystore)'s
10
/// [`Keystore`](crate::Keystore) implementation.
11
#[derive(thiserror::Error, Debug, Clone)]
12
pub(crate) enum ArtiEphemeralKeystoreError {
13
    /// An error that occurred  building an ArtiPath from a KeySpecifier
14
    #[error("unable to build ArtiPath from KeySpecifier")]
15
    ArtiPathUnavailableError(#[from] crate::key_specifier::ArtiPathUnavailableError),
16
    /// An error that occurred serializing a key to OpenSSH text format
17
    #[error("{0}")]
18
    SshKeySerialize(#[from] ssh_key::Error),
19

            
20
    /// An unsupported operation.
21
    #[error("Operation not supported: {action}")]
22
    NotSupported {
23
        /// The action we were trying to perform.
24
        action: &'static str,
25
    },
26
}
27

            
28
impl KeystoreError for ArtiEphemeralKeystoreError {}
29

            
30
impl HasKind for ArtiEphemeralKeystoreError {
31
    fn kind(&self) -> ErrorKind {
32
        match self {
33
            // TODO: These could probably use more specific ErrorKinds. They
34
            // are explicitly matched instead of using a default match to
35
            // encourage future additions to use the appropriate ErrorKind
36
            // rather than letting the default match handle it.
37
            Self::ArtiPathUnavailableError(_) => ErrorKind::Other,
38
            Self::SshKeySerialize(_) => ErrorKind::Other,
39
            Self::NotSupported { .. } => ErrorKind::BadApiUsage,
40
        }
41
    }
42
}
43

            
44
impl From<ArtiEphemeralKeystoreError> for crate::Error {
45
    fn from(e: ArtiEphemeralKeystoreError) -> Self {
46
        crate::Error::Keystore(Arc::new(e))
47
    }
48
}