use std::fmt;
use std::sync::Arc;
use tessarikv_format::{Cipher, RootKey};
use super::db::{DbOptions, DbOptionsBuilder};
pub trait KeyProvider: Send + Sync {
fn root_key(&self, key_id: u32) -> Option<RootKey>;
}
#[derive(Clone)]
pub struct Encryption {
provider: Arc<dyn KeyProvider>,
key_id: u32,
}
impl Encryption {
#[must_use]
pub const fn key_id(&self) -> u32 {
self.key_id
}
pub(crate) fn provider(&self) -> &dyn KeyProvider {
self.provider.as_ref()
}
}
impl fmt::Debug for Encryption {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Encryption")
.field("key_id", &self.key_id)
.finish_non_exhaustive()
}
}
impl PartialEq for Encryption {
fn eq(&self, other: &Self) -> bool {
self.key_id == other.key_id && Arc::ptr_eq(&self.provider, &other.provider)
}
}
impl Eq for Encryption {}
impl DbOptions {
#[must_use]
pub const fn encryption(&self) -> Option<&Encryption> {
self.encryption.as_ref()
}
#[must_use]
pub const fn encryption_cipher(&self) -> Option<Cipher> {
self.encryption_cipher
}
}
impl DbOptionsBuilder {
pub fn encryption(mut self, provider: Arc<dyn KeyProvider>, key_id: u32) -> Self {
self.options.encryption = Some(Encryption { provider, key_id });
self
}
pub const fn encryption_cipher(mut self, cipher: Cipher) -> Self {
self.options.encryption_cipher = Some(cipher);
self
}
}