spin_variables_static/
lib.rs

1use std::{collections::HashMap, hash::Hash, sync::Arc};
2
3use serde::Deserialize;
4use spin_expressions::{async_trait::async_trait, Key, Provider};
5use spin_factors::anyhow;
6
7pub use source::*;
8mod source;
9
10/// A [`Provider`] that reads variables from an static map.
11#[derive(Debug, Deserialize, Clone)]
12pub struct StaticVariablesProvider {
13    values: Arc<HashMap<String, String>>,
14}
15
16#[async_trait]
17impl Provider for StaticVariablesProvider {
18    async fn get(&self, key: &Key) -> anyhow::Result<Option<String>> {
19        Ok(self.values.get(key.as_str()).cloned())
20    }
21
22    fn may_resolve(&self, key: &Key) -> bool {
23        self.values.contains_key(key.as_str())
24    }
25}
26
27impl StaticVariablesProvider {
28    /// Creates a new `StaticVariablesProvider` with the given key-value pairs.
29    pub fn new<K, V>(values: impl IntoIterator<Item = (K, V)>) -> Self
30    where
31        K: Into<String> + Eq + Hash,
32        V: Into<String>,
33    {
34        let values = values
35            .into_iter()
36            .map(|(k, v)| (k.into(), v.into()))
37            .collect();
38        Self {
39            values: Arc::new(values),
40        }
41    }
42}