env_extractor/
var.rs

1use crate::Error;
2use crate::Error::{NotPresent, ParseError};
3use std::env;
4use std::env::VarError;
5use std::fmt::Debug;
6use std::str::FromStr;
7
8pub struct EnvVar {
9    pub key: String,
10}
11
12type EnvResult<Y, X> = crate::Result<Y, <X as FromStr>::Err>;
13
14pub fn env_var<A: Into<String>>(key: A) -> EnvVar {
15    EnvVar { key: key.into() }
16}
17
18impl EnvVar {
19    pub fn as_optional<A>(&self) -> crate::Result<Option<A>, <A as FromStr>::Err>
20    where
21        A: FromStr,
22        <A as FromStr>::Err: Debug,
23    {
24        let if_not_found = || Ok(None);
25        let reify = |item| Ok(Some(item));
26        self.parse(if_not_found, reify)
27    }
28
29    pub fn as_required<A>(&self) -> crate::Result<A, <A as FromStr>::Err>
30    where
31        A: FromStr,
32        <A as FromStr>::Err: Debug,
33    {
34        let if_not_found = || Err(NotPresent(self.key.to_string()));
35        let reify = |item| Ok(item);
36        self.parse(if_not_found, reify)
37    }
38
39    fn parse<X, Y, F1, F2>(&self, if_not_found: F1, reify: F2) -> EnvResult<Y, X>
40    where
41        X: FromStr,
42        <X as FromStr>::Err: Debug,
43        F1: Fn() -> EnvResult<Y, X>,
44        F2: Fn(X) -> EnvResult<Y, X>,
45    {
46        let to_parsed = |value: String| {
47            X::from_str(&value)
48                .map_err(|e| to_parse_error(self.key.to_string(), value, e))
49                .and_then(reify)
50        };
51        let maybe = match env::var(&self.key) {
52            Ok(x1) => Some(x1),
53            Err(VarError::NotPresent) => None,
54            Err(e) => return Err(Error::EnvVarError(e)),
55        };
56        maybe.map(to_parsed).unwrap_or_else(if_not_found)
57    }
58}
59
60fn to_parse_error<E: Debug>(key: String, value: String, cause: E) -> Error<E> {
61    ParseError { key, value, cause }
62}