spin_factor_key_value/
util.rs

1use crate::{Error, Store, StoreManager};
2use spin_core::async_trait;
3use std::{collections::HashMap, sync::Arc};
4
5/// A [`StoreManager`] which delegates to other `StoreManager`s based on the store label.
6pub struct DelegatingStoreManager {
7    delegates: HashMap<String, Arc<dyn StoreManager>>,
8}
9
10impl DelegatingStoreManager {
11    pub fn new(delegates: impl IntoIterator<Item = (String, Arc<dyn StoreManager>)>) -> Self {
12        let delegates = delegates.into_iter().collect();
13        Self { delegates }
14    }
15}
16
17#[async_trait]
18impl StoreManager for DelegatingStoreManager {
19    async fn get(&self, name: &str) -> Result<Arc<dyn Store>, Error> {
20        match self.delegates.get(name) {
21            Some(store) => store.get(name).await,
22            None => Err(Error::NoSuchStore),
23        }
24    }
25
26    fn is_defined(&self, store_name: &str) -> bool {
27        self.delegates.contains_key(store_name)
28    }
29
30    fn summary(&self, store_name: &str) -> Option<String> {
31        if let Some(store) = self.delegates.get(store_name) {
32            return store.summary(store_name);
33        }
34        None
35    }
36}