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
23impl StaticVariablesProvider {
24    /// Creates a new `StaticVariablesProvider` with the given key-value pairs.
25    pub fn new<K, V>(values: impl IntoIterator<Item = (K, V)>) -> Self
26    where
27        K: Into<String> + Eq + Hash,
28        V: Into<String>,
29    {
30        let values = values
31            .into_iter()
32            .map(|(k, v)| (k.into(), v.into()))
33            .collect();
34        Self {
35            values: Arc::new(values),
36        }
37    }
38}