Skip to main content

miden_client_sqlite_store/
builder.rs

1use std::path::PathBuf;
2use std::sync::Arc;
3
4use miden_client::builder::{BuilderAuthenticator, ClientBuilder, StoreBuilder, StoreFactory};
5use miden_client::store::{Store, StoreError};
6
7use crate::SqliteStore;
8
9/// Extends the [`ClientBuilder`] with a method to add a [`SqliteStore`].
10pub trait ClientBuilderSqliteExt<AUTH> {
11    fn sqlite_store(self, database_filepath: PathBuf) -> ClientBuilder<AUTH>;
12}
13
14impl<AUTH: BuilderAuthenticator> ClientBuilderSqliteExt<AUTH> for ClientBuilder<AUTH> {
15    /// Sets a [`SqliteStore`] to the [`ClientBuilder`]. The store will be instantiated when the
16    /// [`build`](ClientBuilder::build) method is called.
17    fn sqlite_store(mut self, database_filepath: PathBuf) -> ClientBuilder<AUTH> {
18        self.store =
19            Some(StoreBuilder::Factory(Box::new(SqliteStoreFactory { database_filepath })));
20        self
21    }
22}
23
24/// Factory for building a [`SqliteStore`].
25struct SqliteStoreFactory {
26    database_filepath: PathBuf,
27}
28
29#[async_trait::async_trait]
30impl StoreFactory for SqliteStoreFactory {
31    async fn build(&self) -> Result<Arc<dyn Store>, StoreError> {
32        let sqlite_store = SqliteStore::new(self.database_filepath.clone()).await?;
33        Ok(Arc::new(sqlite_store))
34    }
35}