#![allow(dead_code)]
use std::{rc::Rc, sync::Arc};
use matrix_sdk_store_encryption::StoreCipher;
use crate::{
event_cache_store::{
IndexeddbEventCacheStore, error::IndexeddbEventCacheStoreError,
migrations::open_and_upgrade_db,
},
serializer::{indexed_type::IndexedTypeSerializer, safe_encode::types::SafeEncodeSerializer},
};
pub struct IndexeddbEventCacheStoreBuilder {
database_name: String,
store_cipher: Option<Arc<StoreCipher>>,
}
impl Default for IndexeddbEventCacheStoreBuilder {
fn default() -> Self {
Self { database_name: Self::DEFAULT_DATABASE_NAME.to_owned(), store_cipher: None }
}
}
impl IndexeddbEventCacheStoreBuilder {
pub const DEFAULT_DATABASE_NAME: &'static str = "event_cache";
pub fn database_name(mut self, name: String) -> Self {
self.database_name = name;
self
}
pub fn with_prefix(prefix: &str) -> Self {
Self {
database_name: format!("{}::{}", prefix, Self::DEFAULT_DATABASE_NAME),
store_cipher: None,
}
}
pub fn store_cipher(mut self, store_cipher: Arc<StoreCipher>) -> Self {
self.store_cipher = Some(store_cipher);
self
}
pub async fn build(self) -> Result<IndexeddbEventCacheStore, IndexeddbEventCacheStoreError> {
Ok(IndexeddbEventCacheStore {
inner: Rc::new(open_and_upgrade_db(&self.database_name).await?),
serializer: IndexedTypeSerializer::new(SafeEncodeSerializer::new(self.store_cipher)),
})
}
}