use std::ffi::c_void;
use crate::backend::{Compartment, Options, StoreInstance, find};
use crate::error::Result;
pub struct Keystore {
instance: Box<dyn StoreInstance>,
}
impl Keystore {
pub fn new(backend_name: &str, opts: &Options) -> Result<Self> {
let backend = find(backend_name)?;
let instance = backend.open(opts)?;
Ok(Keystore { instance })
}
pub fn from_instance(instance: Box<dyn StoreInstance>) -> Self {
Keystore { instance }
}
pub fn instance_mut(&mut self) -> &mut dyn StoreInstance {
self.instance.as_mut()
}
pub fn instance(&self) -> &dyn StoreInstance {
self.instance.as_ref()
}
pub fn put_secret(
&mut self,
module: &str,
app: &str,
key_id: &str,
key: *mut c_void,
) -> Result<()> {
self.instance.put_secret(module, app, key_id, key)
}
pub fn get_secret(&self, module: &str, app: &str, key_id: &str) -> Result<*mut c_void> {
self.instance.get_secret(module, app, key_id)
}
pub fn put_public(
&mut self,
module: &str,
app: &str,
identity: &str,
key: *mut c_void,
sig: &[u8],
) -> Result<()> {
self.instance.put_public(module, app, identity, key, sig)
}
pub fn get_public(
&self,
module: &str,
app: &str,
identity: &str,
) -> Result<(*mut c_void, Vec<u8>)> {
self.instance.get_public(module, app, identity)
}
pub fn enumerate(
&self,
module: &str,
app: &str,
compartment: Compartment,
) -> Result<Vec<(*mut c_void, String)>> {
self.instance.enumerate(module, app, compartment)
}
}