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
//! Environment-Variables configuration loader (`env` feature which is enabled by default).
//!
//! This is only usable if you enabled `env` Cargo feature.
//!
//! ### Example
//! ```rust
//! use std::env::set_var;
//! use plugx_config::{
//!     loader::{ConfigurationLoader, env::ConfigurationLoaderEnv},
//!     ext::url::Url,
//! };
//!
//! set_var("MY_APP_NAME__FOO__B_A_R", "Baz");
//! set_var("MY_APP_NAME__QUX__ABC", "XYZ");
//!
//! let url = Url::try_from("env://?prefix=MY_APP_NAME").expect("A valid URL!");
//!
//! let mut loader = ConfigurationLoaderEnv::new();
//! // You could set `prefix`, `separator`, and `strip_prefix` programmatically like this:
//! // loader.[set|with]_prefix("MY_APP_NAME");
//! // loader.[set|with]_separator("__");
//! // loader.[set|with]_strip_prefix(true);
//!
//! // We do not set `whitelist` so we're going to load all plugins' configurations:
//! let mut maybe_whitelist = None;
//! let result = loader.load(&url, maybe_whitelist, false).unwrap();
//! let (_, foo) = result
//!     .iter()
//!     .find(|(plugin_name, _)| plugin_name == "foo")
//!     .expect("`foo` plugin config");
//! assert_eq!(foo.maybe_contents(), Some(&"B_A_R=\"Baz\"".to_string()));
//! let (_, qux) = result
//!     .iter()
//!     .find(|(plugin_name, _)| plugin_name == "qux")
//!     .expect("`qux` plugin config");
//! assert_eq!(qux.maybe_contents(), Some(&"ABC=\"XYZ\"".to_string()));
//!
//! // Only load `foo` plugin configuration:
//! let whitelist = ["foo".to_string()].to_vec();
//! maybe_whitelist = Some(&whitelist);
//! let result = loader.load(&url, maybe_whitelist, false).unwrap();
//! assert!(result.iter().find(|(plugin_name, _)| plugin_name == "foo").is_some());
//! assert!(result.iter().find(|(plugin_name, _)| plugin_name == "qux").is_none());
//! ```
//!
//! See [mod@loader] documentation to known how loaders work.

use crate::{
    entity::ConfigurationEntity,
    loader::{self, ConfigurationLoadError, ConfigurationLoader},
};
use cfg_if::cfg_if;
use serde::Deserialize;
use std::{env, fmt::Debug};
use url::Url;

pub const NAME: &str = "Environment-Variables";
pub const SCHEME_LIST: &[&str] = &["env"];

/// Loads configurations from Environment-Variables.
#[derive(Debug, Default, Clone)]
pub struct ConfigurationLoaderEnv {
    options: ConfigurationLoaderEnvOptions,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
struct ConfigurationLoaderEnvOptions {
    prefix: String,
    separator: String,
    strip_prefix: bool,
}

impl Default for ConfigurationLoaderEnvOptions {
    fn default() -> Self {
        Self {
            prefix: default::prefix(),
            separator: default::separator(),
            strip_prefix: default::strip_prefix(),
        }
    }
}

pub mod default {

    #[inline]
    pub fn prefix() -> String {
        let mut prefix = option_env!("CARGO_BIN_NAME").unwrap_or("").to_string();
        if prefix.is_empty() {
            prefix = option_env!("CARGO_CRATE_NAME").unwrap_or("").to_string();
        }
        if !prefix.is_empty() {
            prefix += separator().as_str();
        }
        prefix
    }

    #[inline(always)]
    pub fn separator() -> String {
        "__".to_string()
    }

    #[inline(always)]
    pub fn strip_prefix() -> bool {
        true
    }
}

impl ConfigurationLoaderEnv {
    /// Same as `default()` method.
    pub fn new() -> Self {
        Default::default()
    }

    /// Only loads keys with this prefix.
    pub fn set_prefix<P: AsRef<str>>(&mut self, prefix: P) {
        self.options.prefix = prefix.as_ref().to_string();
    }

    /// Only loads keys with this prefix.
    pub fn with_prefix<P: AsRef<str>>(mut self, prefix: P) -> Self {
        self.set_prefix(prefix);
        self
    }

    /// Used is separating plugin names.
    pub fn set_separator<S: AsRef<str>>(&mut self, separator: S) {
        self.options.separator = separator.as_ref().to_string();
    }

    /// Used is separating plugin names.
    pub fn with_separator<S: AsRef<str>>(mut self, separator: S) -> Self {
        self.set_separator(separator);
        self
    }

    /// Used is separating plugin names.
    pub fn set_strip_prefix(&mut self, strip_prefix: bool) {
        self.options.strip_prefix = strip_prefix;
    }

    /// Used is separating plugin names.
    pub fn with_strip_prefix(mut self, strip_prefix: bool) -> Self {
        self.set_strip_prefix(strip_prefix);
        self
    }
}

impl ConfigurationLoader for ConfigurationLoaderEnv {
    fn name(&self) -> String {
        NAME.into()
    }

    /// In this case `["env"]`.
    fn scheme_list(&self) -> Vec<String> {
        SCHEME_LIST.iter().cloned().map(String::from).collect()
    }

    /// This loader does not support `skip_soft_errors`.  
    fn load(
        &self,
        url: &Url,
        maybe_whitelist: Option<&[String]>,
        _skip_soft_errors: bool,
    ) -> Result<Vec<(String, ConfigurationEntity)>, ConfigurationLoadError> {
        let ConfigurationLoaderEnvOptions {
            mut prefix,
            mut separator,
            mut strip_prefix,
        } = loader::deserialize_query_string(NAME, url)?;
        if self.options.prefix != default::prefix() {
            prefix = self.options.prefix.clone()
        }
        if self.options.separator != default::separator() {
            separator = self.options.separator.clone()
        }
        if self.options.strip_prefix != default::strip_prefix() {
            strip_prefix = self.options.strip_prefix
        }
        if !separator.is_empty() && !prefix.is_empty() && !prefix.ends_with(separator.as_str()) {
            prefix += separator.as_str()
        }
        let mut result = Vec::new();
        env::vars()
            .filter(|(key, _)| prefix.is_empty() || key.starts_with(prefix.as_str()))
            .map(|(mut key, value)| {
                if !prefix.is_empty() && strip_prefix {
                    key = key.chars().skip(prefix.chars().count()).collect::<String>()
                }
                (key, value)
            })
            .filter(|(key, _)| !key.is_empty())
            .map(|(key, value)| {
                let key_list = if separator.is_empty() {
                    [key].to_vec()
                } else {
                    key.splitn(2, separator.as_str())
                        .map(|key| key.to_string())
                        .collect()
                };
                (key_list, value)
            })
            .filter(|(key_list, _)| !key_list[0].is_empty())
            .map(|(mut key_list, value)| {
                let plugin_name = key_list.remove(0).to_lowercase();
                let key = if key_list.len() == 1 {
                    key_list.remove(0)
                } else {
                    String::new()
                };
                (plugin_name, key, value)
            })
            .filter(|(_, key, _)| !key.is_empty())
            .map(|(_plugin_name, _key, _value)| {
                cfg_if! {
                    if #[cfg(feature = "tracing")] {
                        tracing::trace!(
                            plugin=_plugin_name,
                            key=_key,
                            value=_value,
                            "Detected environment-variable"
                        );
                    } else if #[cfg(feature = "logging")] {
                        log::trace!(
                            "msg=\"Detected environment-variable\" plugin={_plugin_name:?} key={_key:?} value={_value:?}"
                        );
                    }
                }
                (_plugin_name, _key, _value)
            })
            .filter(|(plugin_name, _, _)| {
                maybe_whitelist
                    .as_ref()
                    .map(|whitelist| whitelist.contains(plugin_name))
                    .unwrap_or(true)
            })
            .for_each(|(plugin_name, key, value)| {
                let key_value = format!("{key}={value:?}");
                if let Some((_, _, configuration)) =
                    result.iter_mut().find(|(name, _, _)| *name == plugin_name)
                {
                    *configuration += "\n";
                    *configuration += key_value.as_str();
                } else {
                    result.push((plugin_name, format!("{prefix}*"), key_value));
                }
            });
        Ok(result
            .into_iter()
            .map(|(plugin_name, key, contents)| {
                (
                    plugin_name.clone(),
                    ConfigurationEntity::new(key, url.clone(), plugin_name, NAME)
                        .with_format("env")
                        .with_contents(contents),
                )
            })
            .map(|(_plugin_name, _configuration)| {
                cfg_if! {
                    if #[cfg(feature = "tracing")] {
                        tracing::trace!(
                            plugin=_plugin_name,
                            format=_configuration.maybe_format().unwrap_or(&"<unknown>".to_string()),
                            contents=_configuration.maybe_contents().unwrap(),
                            "Detected configuration from environment-variable"
                        );
                    } else if #[cfg(feature = "logging")] {
                        log::trace!(
                            "msg=\"Detected configuration from environment-variable\" plugin={_plugin_name:?} format={:?} contents={:?}",
                            _configuration.maybe_format().unwrap_or(&"<unknown>".to_string()),
                            _configuration.maybe_contents().unwrap(),
                        );
                    }
                }
                (_plugin_name, _configuration)
            })
            .collect())
    }
}