1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use std::{env, fmt, mem, sync::Arc};

use anyhow::Context as _;

use super::{ConfigSource, Flat};
use crate::{
    testing::MOCK_ENV_VARS,
    utils::JsonObject,
    value::{FileFormat, Map, Value, ValueOrigin, WithOrigin},
    Json,
};

/// Configuration sourced from environment variables.
#[derive(Debug, Clone)]
pub struct Environment {
    origin: Arc<ValueOrigin>,
    map: Map,
}

impl Default for Environment {
    fn default() -> Self {
        Self {
            origin: Arc::new(ValueOrigin::EnvVars),
            map: Map::new(),
        }
    }
}

impl Environment {
    /// Loads environment variables with the specified prefix.
    pub fn prefixed(prefix: &str) -> Self {
        MOCK_ENV_VARS.with_borrow(|mock_vars| {
            let mock_vars = mock_vars
                .iter()
                .map(|(key, value)| (key.clone(), value.clone()));
            Self::from_iter(prefix, env::vars().chain(mock_vars))
        })
    }

    /// Creates a custom environment.
    pub fn from_iter<K, V>(prefix: &str, env: impl IntoIterator<Item = (K, V)>) -> Self
    where
        K: AsRef<str> + Into<String>,
        V: Into<String>,
    {
        let origin = Arc::new(ValueOrigin::EnvVars);
        let map = env.into_iter().filter_map(|(name, value)| {
            let retained_name = name.as_ref().strip_prefix(prefix)?.to_lowercase();
            Some((
                retained_name,
                WithOrigin {
                    inner: Value::from(value.into()),
                    origin: Arc::new(ValueOrigin::Path {
                        source: origin.clone(),
                        path: name.into(),
                    }),
                },
            ))
        });
        let map = map.collect();
        Self { origin, map }
    }

    /// Adds additional variables to this environment. This is useful if the added vars don't have the necessary prefix.
    #[must_use]
    pub fn with_vars(mut self, var_names: &[&str]) -> Self {
        let origin = Arc::new(ValueOrigin::EnvVars);
        let defined_vars = var_names.iter().filter_map(|&name| {
            let value = env::var_os(name)?.into_string().ok()?;
            Some((
                name.to_owned(),
                WithOrigin {
                    inner: Value::from(value),
                    origin: Arc::new(ValueOrigin::Path {
                        source: origin.clone(),
                        path: name.to_owned(),
                    }),
                },
            ))
        });
        self.map.extend(defined_vars);
        self
    }

    #[doc(hidden)] // FIXME: functionally incomplete ('' strings, interpolation, comments after vars)
    pub fn from_dotenv(filename: &str, contents: &str) -> anyhow::Result<Self> {
        let origin = Arc::new(ValueOrigin::File {
            name: filename.to_owned(),
            format: FileFormat::Dotenv,
        });
        let mut map = Map::default();
        for line in contents.lines().map(str::trim) {
            if line.is_empty() || line.starts_with('#') {
                continue;
            }
            let (name, variable_value) = line.split_once('=').with_context(|| {
                format!("Incorrect line for setting environment variable: {line}")
            })?;
            let variable_value = variable_value.trim_matches('"');
            map.insert(
                name.to_lowercase(),
                WithOrigin {
                    inner: Value::from(variable_value.to_owned()),
                    origin: Arc::new(ValueOrigin::Path {
                        source: origin.clone(),
                        path: name.into(),
                    }),
                },
            );
        }
        Ok(Self { origin, map })
    }

    /// Iterates over variables in this container.
    pub fn iter(&self) -> impl ExactSizeIterator<Item = (&str, &WithOrigin)> + '_ {
        self.map.iter().map(|(name, value)| (name.as_str(), value))
    }

    /// Strips a prefix from all contained vars and returns the filtered vars.
    #[must_use]
    pub fn strip_prefix(self, prefix: &str) -> Self {
        let prefix = prefix.to_lowercase();
        let filtered = self
            .map
            .into_iter()
            .filter_map(|(name, value)| Some((name.strip_prefix(&prefix)?.to_owned(), value)));
        Self {
            origin: self.origin,
            map: filtered.collect(),
        }
    }

    /// Coerces JSON values in env variables which names end with the `__json` / `:json` suffixes and strips this suffix.
    ///
    /// # Errors
    ///
    /// Returns an error if any coercion fails; provides a list of all failed coercions. Successful coercions are still applied in this case.
    pub fn coerce_json(&mut self) -> anyhow::Result<()> {
        let mut coerced_values = vec![];
        let mut errors = vec![];
        for (key, value) in &self.map {
            let stripped_key = key
                .strip_suffix("__json")
                .or_else(|| key.strip_suffix(":json"));
            let Some(stripped_key) = stripped_key else {
                continue;
            };
            let Some(value_str) = value.inner.as_plain_str() else {
                // The value was already transformed, probably.
                continue;
            };

            let val = match serde_json::from_str::<serde_json::Value>(value_str) {
                Ok(val) => val,
                Err(err) => {
                    mem::take(&mut coerced_values);
                    errors.push((value.origin.clone(), err));
                    continue;
                }
            };
            if !errors.is_empty() {
                continue; // No need to record coerced values if there are coercion errors.
            }

            let root_origin = Arc::new(ValueOrigin::Synthetic {
                source: value.origin.clone(),
                transform: "parsed JSON string".into(),
            });
            let coerced_value = Json::map_value(val, &root_origin, String::new());
            coerced_values.push((key.to_owned(), stripped_key.to_owned(), coerced_value));
        }

        for (key, stripped_key, coerced_value) in coerced_values {
            self.map.remove(&key);
            self.map.insert(stripped_key, coerced_value);
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(JsonCoercionErrors(errors).into())
        }
    }

    /// Converts a [flat configuration object](crate::SerializerOptions::flat()) into a flat object
    /// usable as the env var specification for Docker Compose etc. It uppercases and prefixes param names,
    /// replacing `.`s with `_`s, and replaces object / JSON params with strings so that they can be parsed
    /// via [JSON coercion](Self::coerce_json()).
    ///
    /// # Important
    ///
    /// Beware that additional transforms may be required depending on the use case. E.g., Docker Compose
    /// requires to escape Boolean values and nulls to strings.
    pub fn convert_flat_params(flat_params: &JsonObject, prefix: &str) -> JsonObject {
        let vars = flat_params.iter().map(|(path, value)| {
            let mut var_name = path.replace('.', "_").to_uppercase();
            var_name.insert_str(0, prefix);
            let value: serde_json::Value = match value {
                serde_json::Value::Array(_) | serde_json::Value::Object(_) => {
                    var_name.push_str("__JSON");
                    value.to_string().into()
                }
                simple => simple.clone(),
            };
            (var_name, value)
        });
        vars.collect()
    }
}

#[derive(Debug)]
struct JsonCoercionErrors(Vec<(Arc<ValueOrigin>, serde_json::Error)>);

impl fmt::Display for JsonCoercionErrors {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(
            formatter,
            "failed coercing flat configuration params to JSON:"
        )?;
        for (i, (key, err)) in self.0.iter().enumerate() {
            writeln!(formatter, "{}. {key}: {err}", i + 1)?;
        }
        Ok(())
    }
}

impl std::error::Error for JsonCoercionErrors {}

impl ConfigSource for Environment {
    type Kind = Flat;

    fn into_contents(self) -> WithOrigin<Map> {
        WithOrigin::new(self.map, self.origin)
    }
}

#[cfg(test)]
mod tests {
    use assert_matches::assert_matches;

    use super::*;

    #[test]
    fn parsing_dotenv_contents() {
        let env = Environment::from_dotenv(
            "test.env",
            r#"
            APP_TEST=what
            APP_OTHER="test string"

            # Overwriting vars should be supported
            APP_TEST=42
            "#,
        )
        .unwrap();

        assert_eq!(env.map.len(), 2, "{:?}", env.map);
        assert_eq!(env.map["app_test"].inner.as_plain_str(), Some("42"));
        let origin = &env.map["app_test"].origin;
        let ValueOrigin::Path { path, source } = origin.as_ref() else {
            panic!("unexpected origin: {origin:?}");
        };
        assert_eq!(path, "APP_TEST");
        assert_matches!(
            source.as_ref(),
            ValueOrigin::File { name, format: FileFormat::Dotenv } if name == "test.env"
        );
        assert_eq!(
            env.map["app_other"].inner.as_plain_str(),
            Some("test string")
        );

        let env = env.strip_prefix("app_");
        assert_eq!(env.map.len(), 2, "{:?}", env.map);
        assert_eq!(env.map["test"].inner.as_plain_str(), Some("42"));
        assert_matches!(env.map["test"].origin.as_ref(), ValueOrigin::Path { path, .. } if path == "APP_TEST");
        assert_eq!(env.map["other"].inner.as_plain_str(), Some("test string"));
    }

    #[test]
    fn converting_flat_params() {
        let params = serde_json::json!({
            "value": 23,
            "flag": true,
            "nested.option": null,
            "nested.renamed": "first",
            "nested.set": ["first", "second"],
            "nested.map": { "call": 42 },
        });
        let params = params.as_object().unwrap();

        let converted = Environment::convert_flat_params(params, "APP_");
        assert_eq!(
            serde_json::Value::from(converted),
            serde_json::json!({
                "APP_VALUE": 23,
                "APP_FLAG": true,
                "APP_NESTED_OPTION": null,
                "APP_NESTED_RENAMED": "first",
                "APP_NESTED_SET__JSON": r#"["first","second"]"#,
                "APP_NESTED_MAP__JSON": r#"{"call":42}"#,
            })
        );
    }
}