use crate::error::Result;
#[cfg(feature = "file-backend")]
mod file;
#[cfg(feature = "testing")]
mod memory;
#[cfg(feature = "file-backend")]
pub use file::FileBackend;
#[cfg(feature = "testing")]
pub use memory::MemoryBackend;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct BackendKey(pub String);
impl BackendKey {
pub fn new(name: impl Into<String>) -> Self {
Self(name.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl<T: Into<String>> From<T> for BackendKey {
fn from(v: T) -> Self {
Self(v.into())
}
}
impl std::fmt::Display for BackendKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
pub trait KeychainBackend: Send + Sync + 'static {
fn read(&self, key: &BackendKey) -> Result<Vec<u8>>;
fn write(&self, key: &BackendKey, data: &[u8]) -> Result<()>;
fn delete(&self, key: &BackendKey) -> Result<()>;
fn list(&self, prefix: &str) -> Result<Vec<BackendKey>>;
fn exists(&self, key: &BackendKey) -> Result<bool> {
match self.read(key) {
Ok(_) => Ok(true),
Err(crate::error::KeystoreError::Backend(e))
if e.kind() == std::io::ErrorKind::NotFound =>
{
Ok(false)
}
Err(e) => Err(e),
}
}
}