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
306
307
308
309
310
311
312
313
314
315
316
317
318
//! # environment
//!
//! Environment variables direct access functions.
//!

#[cfg(test)]
#[path = "./environment_test.rs"]
mod environment_test;

use crate::util;
use std::env;
use std::ffi::OsStr;

static UNIX_ENV_SYMBOL: &str = "$";
static UNIX_ENV_PREFIX: &str = "${";
static UNIX_ENV_SUFFIX: &str = "}";
static WINDOWS_ENV_SYMBOL: &str = "%";

/// Get/Set list options
#[derive(Debug, Clone)]
pub struct ListOptions {
    /// The separator used to merge/split the values
    pub separator: Option<String>,
    /// if true, empty list will not be set and empty string will be considered as a list with a single empty value
    pub ignore_empty: bool,
}

impl ListOptions {
    /// Creates and returns a new instance.
    pub fn new() -> ListOptions {
        ListOptions {
            separator: None,
            ignore_empty: false,
        }
    }
}

/// Expansion Type - unix/windows style
#[derive(Debug, Copy, Clone)]
pub enum ExpansionType {
    /// Unix prefix environment style, for example: $MY_ENV
    UnixPrefix,
    /// Unix brackets environment style, for example: ${MY_ENV}
    UnixBrackets,
    /// All unix supported styles
    Unix,
    /// Windows environment style, for example: %MY_ENV%
    Windows,
    /// Current OS supported styles (UnixAll/Windows)
    OS,
    /// All supported styles for all platforms
    All,
}

fn get_os_expansion_type() -> ExpansionType {
    if cfg!(windows) {
        ExpansionType::Windows
    } else {
        ExpansionType::Unix
    }
}

pub(crate) fn exists<K: AsRef<OsStr>>(key: K) -> bool {
    match env::var(key) {
        Ok(_) => true,
        _ => false,
    }
}

pub(crate) fn remove<K: AsRef<OsStr>>(key: K) {
    env::remove_var(key)
}

pub(crate) fn get_remove<K: AsRef<OsStr>>(key: K) -> Option<String> {
    let pre_value = if exists(&key) {
        Some(get_or(&key, ""))
    } else {
        None
    };

    remove(key);

    pre_value
}

pub(crate) fn get_or<K: AsRef<OsStr>>(key: K, default_value: &str) -> String {
    match env::var(key) {
        Ok(value) => value.to_string(),
        _ => default_value.to_string(),
    }
}

pub(crate) fn get_or_panic<K: AsRef<OsStr>>(key: K) -> String {
    env::var(key).unwrap()
}

pub(crate) fn get_any<K: AsRef<OsStr>>(keys: &Vec<K>, default_value: &str) -> String {
    let mut output = default_value.to_string();

    for key in keys.iter() {
        let current_value = env::var(key);

        if current_value.is_ok() {
            output = current_value.unwrap();
            break;
        }
    }

    output
}

pub(crate) fn is_or<K: AsRef<OsStr>>(key: K, default_value: bool) -> bool {
    let default_str = util::bool_to_string(default_value);

    let value = get_or(key, &default_str);

    util::string_to_bool(&value)
}

pub(crate) fn is<K: AsRef<OsStr>>(key: K) -> bool {
    is_or(&key, false)
}

pub(crate) fn set<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) {
    env::set_var(&key, &value);
}

pub(crate) fn set_bool<K: AsRef<OsStr>>(key: K, value: bool) {
    let value_str = util::bool_to_string(value);
    set(key, &value_str);
}

pub(crate) fn set_optional<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: &Option<V>) -> bool {
    match value {
        Some(ref value_ref) => {
            set(key, value_ref);
            true
        }
        None => false,
    }
}

pub(crate) fn get_set<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) -> Option<String> {
    let pre_value = if exists(&key) {
        Some(get_or(&key, ""))
    } else {
        None
    };

    set(&key, &value);

    pre_value
}

pub(crate) fn vars() -> Vec<(String, String)> {
    env::vars().collect()
}

pub(crate) fn is_equal<K: AsRef<OsStr>>(key: K, value: &str) -> bool {
    if exists(&key) {
        let current_value = get_or(&key, "");

        current_value == value
    } else {
        false
    }
}

pub(crate) fn contains<K: AsRef<OsStr>>(key: K, value: &str) -> bool {
    if exists(&key) {
        let current_value = get_or(&key, "");

        current_value.contains(value)
    } else {
        false
    }
}

pub(crate) fn contains_ignore_case<K: AsRef<OsStr>>(key: K, value: &str) -> bool {
    if exists(&key) {
        let current_value = get_or(&key, "").to_lowercase();

        current_value.contains(&value.to_lowercase())
    } else {
        false
    }
}

pub(crate) fn set_list<K: AsRef<OsStr>>(key: K, values: &Vec<String>) {
    let options = ListOptions::new();
    set_list_with_options(key, values, &options)
}

pub(crate) fn get_list<K: AsRef<OsStr>>(key: K) -> Option<Vec<String>> {
    let options = ListOptions::new();
    get_list_with_options(key, &options)
}

fn get_list_separator(options: &ListOptions) -> String {
    match options.separator {
        Some(ref separator) => separator.to_string(),
        None => ";".to_string(),
    }
}

pub(crate) fn set_list_with_options<K: AsRef<OsStr>>(
    key: K,
    values: &Vec<String>,
    options: &ListOptions,
) {
    let separator = get_list_separator(&options);

    if values.is_empty() && options.ignore_empty {
        remove(key)
    } else {
        let value = values.join(&separator);

        set(key, value)
    }
}

pub(crate) fn get_list_with_options<K: AsRef<OsStr>>(
    key: K,
    options: &ListOptions,
) -> Option<Vec<String>> {
    let separator = get_list_separator(&options);

    match env::var(key) {
        Ok(value_string) => {
            if value_string.len() == 0 && !options.ignore_empty {
                Some(vec![])
            } else {
                let values = value_string.split(&separator);
                let mut values_vec = Vec::new();

                for value in values {
                    values_vec.push(value.to_string());
                }

                Some(values_vec)
            }
        }
        _ => None,
    }
}

pub(crate) fn expand_by_prefix(value: &str, prefix: &str) -> String {
    let mut value_string = value.to_string();

    match value_string.find(prefix) {
        Some(_) => {
            for (existing_key, mut existing_value) in env::vars() {
                let mut key_pattern = prefix.to_string();
                key_pattern.push_str(&existing_key);

                let value_length = existing_value.len();
                let pattern_length = key_pattern.len();

                for suffix in " \t\n".chars() {
                    key_pattern.push(suffix);
                    existing_value.push(suffix);

                    value_string = str::replace(&value_string, &key_pattern, &existing_value);

                    key_pattern.remove(pattern_length);
                    existing_value.remove(value_length);
                }

                if value_string.ends_with(&key_pattern) {
                    value_string.truncate(value_string.len() - pattern_length);
                    value_string.push_str(&existing_value);
                }
            }

            value_string
        }
        None => value_string,
    }
}

pub(crate) fn expand_by_wrapper(value: &str, prefix: &str, suffix: &str) -> String {
    let mut value_string = value.to_string();

    match value_string.find(prefix) {
        Some(_) => {
            for (existing_key, existing_value) in env::vars() {
                let mut key_pattern = prefix.to_string();
                key_pattern.push_str(&existing_key);
                key_pattern.push_str(suffix);

                value_string = str::replace(&value_string, &key_pattern, &existing_value);
            }

            value_string
        }
        None => value_string,
    }
}

pub(crate) fn expand(value: &str, expansion_type: ExpansionType) -> String {
    match expansion_type {
        ExpansionType::UnixPrefix => expand_by_prefix(&value, UNIX_ENV_SYMBOL),
        ExpansionType::UnixBrackets => expand_by_wrapper(&value, UNIX_ENV_PREFIX, UNIX_ENV_SUFFIX),
        ExpansionType::Unix => {
            let expanded_value = expand(&value, ExpansionType::UnixBrackets);
            expand(&expanded_value, ExpansionType::UnixPrefix)
        }
        ExpansionType::Windows => expand_by_wrapper(&value, WINDOWS_ENV_SYMBOL, WINDOWS_ENV_SYMBOL),
        ExpansionType::OS => {
            let os_expansion_type = get_os_expansion_type();
            expand(&value, os_expansion_type)
        }
        ExpansionType::All => {
            let expanded_value = expand(&value, ExpansionType::Unix);
            expand(&expanded_value, ExpansionType::Windows)
        }
    }
}