1
//! Raw keystore entry identifiers used in plumbing CLI functionalities.
2

            
3
use std::path::PathBuf;
4

            
5
use amplify::Getters;
6
use tor_basic_utils::PathExt;
7
use tor_key_forge::KeystoreItemType;
8

            
9
use crate::{ArtiPath, KeystoreId, UnrecognizedEntry};
10

            
11
/// A raw keystore entry descriptor.
12
#[cfg_attr(feature = "onion-service-cli-extra", visibility::make(pub))]
13
#[derive(Debug, Clone, PartialEq, derive_more::From, Getters)]
14
pub(crate) struct RawKeystoreEntry {
15
    /// The underlying keystore-specific raw identifier of the entry.
16
    #[getter(skip)]
17
    raw_id: RawEntryId,
18
    /// The keystore this entry was found in.
19
    #[getter(skip)]
20
    keystore_id: KeystoreId,
21
}
22

            
23
impl RawKeystoreEntry {
24
    /// Return the underlying keystore-specific raw identifier of the entry.
25
    #[cfg(feature = "onion-service-cli-extra")]
26
916
    pub fn raw_id(&self) -> &RawEntryId {
27
916
        &self.raw_id
28
916
    }
29

            
30
    /// Return the ID of the keystore this entry was found in.
31
    #[cfg(feature = "onion-service-cli-extra")]
32
364
    pub fn keystore_id(&self) -> &KeystoreId {
33
364
        &self.keystore_id
34
364
    }
35
}
36

            
37
impl From<&UnrecognizedEntry> for RawKeystoreEntry {
38
2
    fn from(value: &UnrecognizedEntry) -> Self {
39
2
        value.clone().into()
40
2
    }
41
}
42

            
43
impl RawKeystoreEntry {
44
    /// Returns a new instance of [`RawKeystoreEntry`]
45
    /// that identifies an entry with the specified `raw_id`
46
    /// raw identifier in the keystore with the specified
47
    /// `keystore_id`.
48
1470
    pub(crate) fn new(raw_id: RawEntryId, keystore_id: KeystoreId) -> Self {
49
1470
        Self {
50
1470
            raw_id,
51
1470
            keystore_id,
52
1470
        }
53
1470
    }
54
}
55

            
56
/// The raw identifier of a key inside a [`Keystore`](crate::Keystore).
57
///
58
/// The exact type of the identifier depends on the backing storage of the keystore
59
/// (for example, an on-disk keystore will identify its entries by [`Path`](RawEntryId::Path)).
60
#[cfg_attr(feature = "onion-service-cli-extra", visibility::make(pub))]
61
#[non_exhaustive]
62
#[derive(Debug, Clone, PartialEq, derive_more::Display)]
63
pub(crate) enum RawEntryId {
64
    /// An entry identified by path inside an on-disk keystore.
65
    // NOTE: this will only be used by on-disk keystores like
66
    // [`ArtiNativeKeystore`](crate::ArtiNativeKeystore)
67
    #[display("{}", _0.display_lossy())]
68
    Path(PathBuf),
69

            
70
    /// An entry of an in-memory ephemeral key storage
71
    /// [`ArtiEphemeralKeystore`](crate::ArtiEphemeralKeystore)
72
    #[display("{} {:?}", _0.0, _0.1)]
73
    Ephemeral((ArtiPath, KeystoreItemType)),
74
    // TODO: when/if we add support for non on-disk keystores,
75
    // new variants will be added
76
}