wasmcloud_host/
config.rs

1use std::collections::HashMap;
2
3use anyhow::Context as _;
4use tokio::sync::watch::{self, Receiver};
5
6use crate::store::{DefaultStore, StoreManager};
7
8#[async_trait::async_trait]
9/// A trait for managing a config store which can be watched to receive updates to the config
10pub trait ConfigManager: StoreManager {
11    /// Watches a config by name and returns a receiver that will be notified when the config changes
12    ///
13    /// The default implementation returns a receiver that will never receive any updates.
14    async fn watch(&self, name: &str) -> anyhow::Result<Receiver<HashMap<String, String>>> {
15        let config = match self.get(name).await {
16            Ok(Some(data)) => serde_json::from_slice(&data)
17                .context("Data corruption error, unable to decode data from store")?,
18            Ok(None) => return Err(anyhow::anyhow!("Config {} does not exist", name)),
19            Err(e) => return Err(anyhow::anyhow!("Error fetching config {}: {}", name, e)),
20        };
21        Ok(watch::channel(config).1)
22    }
23}
24
25/// A default implementation of the config manager that does not watch for updates
26impl ConfigManager for DefaultStore {}