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
#![warn(warnings)]
#![cfg_attr(docsrs, feature(doc_cfg))]

mod errors;
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
mod serde;

#[cfg(feature = "serde")]
pub use serde::*;

pub use errors::{Error, Result};

/**
 * Loads the *.env* files.
 */
#[cfg(feature = "dotenv")]
#[cfg_attr(docsrs, doc(cfg(feature = "dotenv")))]
pub fn dotenv() {
    dotenvy::dotenv().ok();
}

/**
 * Retreives all environment variables as an easy printable form.
 */
#[must_use]
pub fn collect() -> std::collections::HashMap<String, String> {
    std::env::vars().collect()
}

/**
 * Likes `try_get` but directly parses the variable value in desired `T` type.
 */
pub fn try_parse<T: std::str::FromStr>(key: &str) -> crate::Result<Option<T>>
where
    T::Err: ToString,
{
    let value = match crate::try_get(key)? {
        Some(v) => v
            .parse::<T>()
            .map_err(|e| crate::Error::parse::<T, _>(key, e.to_string()))?,
        None => return Ok(None),
    };

    Ok(Some(value))
}

/**
 * Likes `try_parse` but returns a `crate::Error::Missing` if the variable isn’t set.
 */
pub fn parse<T: std::str::FromStr>(key: &str) -> crate::Result<T>
where
    T::Err: ToString,
{
    crate::try_parse(key)?.ok_or_else(|| crate::Error::Missing(key.to_string()))
}

/**
 * Gets the environment variable `key`. This returns a `crate::Error::Unicode` if the variable
 * value isn’t valid unicode.
 */
pub fn try_get(key: &str) -> crate::Result<Option<String>> {
    let Some(value) = std::env::var_os(key) else {
        return Ok(None);
    };

    let value = match value.to_str() {
        Some(v) => v.to_string(),
        None => return Err(crate::Error::unicode(key, value)),
    };

    Ok(Some(value))
}

/**
 * Likes `try_get` but returns a `crate::Error::Missing` if the variable isn’t set.
 */
pub fn get(key: &str) -> crate::Result<String> {
    crate::try_get(key)?.ok_or_else(|| crate::Error::Missing(key.to_string()))
}

/**
 * Sets the environment variable `key` to the `value`.
 */
pub fn set<T: ToString>(key: &str, value: T) {
    std::env::set_var(key, value.to_string());
}

#[cfg(test)]
mod test {
    #[test]
    fn collect() {
        assert!(!crate::collect().is_empty());
    }

    #[test]
    fn try_parse() -> crate::Result {
        assert!(crate::try_parse::<String>("MISSING_ENV")?.is_none());

        Ok(())
    }

    #[test]
    fn parse() -> crate::Result {
        crate::set("TEST", 1);
        assert_eq!(crate::parse::<u8>("TEST")?, 1u8);

        Ok(())
    }

    #[test]
    fn try_get() -> crate::Result {
        assert!(crate::try_get("MISSING_ENV")?.is_none());

        Ok(())
    }

    #[test]
    fn get() -> crate::Result {
        crate::set("TEST", 1);
        assert_eq!(crate::get("TEST")?, "1");

        Ok(())
    }
}