Skip to main content

agile_config_client/
source.rs

1//! `config` crate [`AsyncSource`] adapter.
2
3use std::fmt::{Debug, Formatter};
4use std::sync::Arc;
5
6use async_trait::async_trait;
7use config::{AsyncSource, ConfigError, Map, Value};
8
9use crate::client::Inner;
10
11/// Read-only configuration source backed by a [`crate::Client`].
12///
13/// `collect` returns the current snapshot. If the client has not loaded yet,
14/// it performs an HTTP pull (with cache fallback). It never starts the
15/// WebSocket session.
16#[derive(Clone)]
17pub struct Source {
18    inner: Arc<Inner>,
19}
20
21impl Source {
22    pub(crate) fn new(inner: Arc<Inner>) -> Self {
23        Self { inner }
24    }
25}
26
27impl Debug for Source {
28    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
29        formatter
30            .debug_struct("Source")
31            .field("app_id", &self.inner.options.app_id)
32            .finish()
33    }
34}
35
36#[async_trait]
37impl AsyncSource for Source {
38    async fn collect(&self) -> Result<Map<String, Value>, ConfigError> {
39        self.inner
40            .ensure_loaded()
41            .await
42            .map_err(|error| ConfigError::Foreign(Box::new(error)))?;
43        Ok(self.inner.snapshot().to_config_map())
44    }
45}