cat_gateway/settings/
str_env_var.rs

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
//! Processing for String Environment Variables

// cspell: words smhdwy

use std::{
    env::{self, VarError},
    fmt::{self, Display},
    str::FromStr,
    time::Duration,
};

use duration_string::DurationString;
use strum::VariantNames;
use tracing::{error, info};

/// An environment variable read as a string.
#[derive(Clone)]
pub(crate) struct StringEnvVar {
    /// Value of the env var.
    value: String,
    /// Whether the env var is displayed redacted or not.
    redacted: bool,
}

/// Ergonomic way of specifying if a env var needs to be redacted or not.
pub(super) enum StringEnvVarParams {
    /// The env var is plain and should not be redacted.
    Plain(String, Option<String>),
    /// The env var is redacted and should be redacted.
    Redacted(String, Option<String>),
}

impl From<&str> for StringEnvVarParams {
    fn from(s: &str) -> Self {
        StringEnvVarParams::Plain(String::from(s), None)
    }
}

impl From<String> for StringEnvVarParams {
    fn from(s: String) -> Self {
        StringEnvVarParams::Plain(s, None)
    }
}

impl From<(&str, bool)> for StringEnvVarParams {
    fn from((s, r): (&str, bool)) -> Self {
        if r {
            StringEnvVarParams::Redacted(String::from(s), None)
        } else {
            StringEnvVarParams::Plain(String::from(s), None)
        }
    }
}

impl From<(&str, bool, &str)> for StringEnvVarParams {
    fn from((s, r, c): (&str, bool, &str)) -> Self {
        if r {
            StringEnvVarParams::Redacted(String::from(s), Some(String::from(c)))
        } else {
            StringEnvVarParams::Plain(String::from(s), Some(String::from(c)))
        }
    }
}

/// An environment variable read as a string.
impl StringEnvVar {
    /// Read the env var from the environment.
    ///
    /// If not defined, read from a .env file.
    /// If still not defined, use the default.
    ///
    /// # Arguments
    ///
    /// * `var_name`: &str - the name of the env var
    /// * `default_value`: &str - the default value
    ///
    /// # Returns
    ///
    /// * Self - the value
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// #use cat_data_service::settings::StringEnvVar;
    ///
    /// let var = StringEnvVar::new("MY_VAR", "default");
    /// assert_eq!(var.as_str(), "default");
    /// ```
    pub(super) fn new(var_name: &str, param: StringEnvVarParams) -> Self {
        let (default_value, redacted, choices) = match param {
            StringEnvVarParams::Plain(s, c) => (s, false, c),
            StringEnvVarParams::Redacted(s, c) => (s, true, c),
        };

        match env::var(var_name) {
            Ok(value) => {
                let value = Self { value, redacted };
                info!(env=var_name, value=%value, "Env Var Defined");
                value
            },
            Err(err) => {
                let value = Self {
                    value: default_value,
                    redacted,
                };
                if err == VarError::NotPresent {
                    if let Some(choices) = choices {
                        info!(env=var_name, default=%value, choices=choices, "Env Var Defaulted");
                    } else {
                        info!(env=var_name, default=%value, "Env Var Defaulted");
                    }
                } else if let Some(choices) = choices {
                    info!(env=var_name, default=%value, choices=choices, error=?err,
                        "Env Var Error");
                } else {
                    info!(env=var_name, default=%value, error=?err, "Env Var Error");
                }

                value
            },
        }
    }

    /// New Env Var that is optional.
    pub(super) fn new_optional(var_name: &str, redacted: bool) -> Option<Self> {
        match env::var(var_name) {
            Ok(value) => {
                let value = Self { value, redacted };
                info!(env = var_name, value = %value, "Env Var Defined");
                Some(value)
            },
            Err(VarError::NotPresent) => {
                info!(env = var_name, "Env Var Not Set");
                None
            },
            Err(error) => {
                error!(env = var_name, error = ?error, "Env Var Error");
                None
            },
        }
    }

    /// Convert an Envvar into the required Enum Type.
    pub(super) fn new_as_enum<T: FromStr + Display + VariantNames>(
        var_name: &str, default: T, redacted: bool,
    ) -> T
    where <T as std::str::FromStr>::Err: std::fmt::Display {
        let mut choices = String::new();
        for name in T::VARIANTS {
            if choices.is_empty() {
                choices.push('[');
            } else {
                choices.push(',');
            }
            choices.push_str(name);
        }
        choices.push(']');

        let choice = StringEnvVar::new(
            var_name,
            (default.to_string().as_str(), redacted, choices.as_str()).into(),
        );

        let value = match T::from_str(choice.as_str()) {
            Ok(var) => var,
            Err(error) => {
                error!(error=%error, default=%default, choices=choices, choice=%choice,
                    "Invalid choice. Using Default.");
                default
            },
        };

        value
    }

    /// Convert an Envvar into the required Duration type.
    pub(crate) fn new_as_duration(var_name: &str, default: &str) -> Duration {
        let choices = "A value in the format of `[0-9]+(ns|us|ms|[smhdwy])`";

        let raw_value = StringEnvVar::new(
            var_name,
            (default.to_string().as_str(), false, choices).into(),
        )
        .as_string();

        match DurationString::try_from(raw_value.clone()) {
            Ok(duration) => duration.into(),
            Err(error) => {
                error!(
                    "Invalid Duration: {} : {}. Defaulting to {}.",
                    raw_value, error, default
                );

                match DurationString::try_from(default.to_string()) {
                    Ok(duration) => duration.into(),
                    // The error from parsing the default value must not happen
                    Err(error) => {
                        error!(
                            "Invalid Default Duration: {} : {}. Defaulting to 1s.",
                            default, error
                        );
                        Duration::from_secs(1)
                    },
                }
            },
        }
    }

    /// Convert an Envvar into an integer in the bounded range.
    pub(super) fn new_as<T>(var_name: &str, default: T, min: T, max: T) -> T
    where
        T: FromStr + Display + PartialOrd + tracing::Value,
        <T as std::str::FromStr>::Err: std::fmt::Display,
    {
        let choices = format!("A value in the range {min} to {max} inclusive");

        let raw_value = StringEnvVar::new(
            var_name,
            (default.to_string().as_str(), false, choices.as_str()).into(),
        )
        .as_string();

        match raw_value.parse::<T>() {
            Ok(value) => {
                if value < min {
                    error!("{var_name} out of range. Range = {min} to {max} inclusive. Clamped to {min}");
                    min
                } else if value > max {
                    error!("{var_name} out of range. Range = {min} to {max} inclusive. Clamped to {max}");
                    max
                } else {
                    value
                }
            },
            Err(error) => {
                error!(error=%error, default=default, "{var_name} not an integer. Range = {min} to {max} inclusive. Defaulted");
                default
            },
        }
    }

    /// Get the read env var as a str.
    ///
    /// # Returns
    ///
    /// * &str - the value
    pub(crate) fn as_str(&self) -> &str {
        &self.value
    }

    /// Get the read env var as a str.
    ///
    /// # Returns
    ///
    /// * &str - the value
    pub(crate) fn as_string(&self) -> String {
        self.value.clone()
    }
}

impl fmt::Display for StringEnvVar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.redacted {
            return write!(f, "REDACTED");
        }
        write!(f, "{}", self.value)
    }
}

impl fmt::Debug for StringEnvVar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.redacted {
            return write!(f, "REDACTED");
        }
        write!(f, "env: {}", self.value)
    }
}