launchdarkly_server_sdk/stores/
store_builders.rs

1use super::store::{DataStore, InMemoryDataStore};
2use parking_lot::RwLock;
3use std::sync::Arc;
4use thiserror::Error;
5
6#[non_exhaustive]
7#[derive(Debug, Error)]
8pub enum BuildError {
9    #[error("data store factory failed to build: {0}")]
10    InvalidConfig(String),
11}
12
13/// Trait which allows creation of data stores. Should be implemented by data store builder types.
14pub trait DataStoreFactory {
15    fn build(&self) -> Result<Arc<RwLock<dyn DataStore>>, BuildError>;
16    fn to_owned(&self) -> Box<dyn DataStoreFactory>;
17}
18
19/// Contains methods for configuring the in memory data store.
20///
21/// By default, the SDK uses an in memory store to manage flag and segment data.
22#[derive(Clone)]
23pub struct InMemoryDataStoreBuilder {}
24
25impl InMemoryDataStoreBuilder {
26    pub fn new() -> Self {
27        Self {}
28    }
29}
30
31impl DataStoreFactory for InMemoryDataStoreBuilder {
32    fn build(&self) -> Result<Arc<RwLock<dyn DataStore>>, BuildError> {
33        Ok(Arc::new(RwLock::new(InMemoryDataStore::new())))
34    }
35
36    fn to_owned(&self) -> Box<dyn DataStoreFactory> {
37        Box::new(self.clone())
38    }
39}