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
//! Provides the ability to load values from the environment
use anyhow::Result;

/// This type provides functionality to load values from environment variables
pub struct EnvironmentLoader {}

impl EnvironmentLoader {
    pub fn new() -> Self {
        EnvironmentLoader {}
    }
}

#[async_trait::async_trait]
impl crate::ValueLoader for EnvironmentLoader {
    /// Load a value from the environment. The key is the name of the environment variable
    /// containing the value
    async fn load(&self, key: &String) -> Result<String> {
        Ok(std::env::var(&key)?)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::ValueLoader;

    #[tokio::test]
    async fn test_environment_loader() {
        let expected = String::from("success");
        let key = String::from("ORC_TEST_VAR");
        std::env::set_var(&key, &expected);

        let loader = EnvironmentLoader::new();
        let actual = loader.load(&key).await.unwrap();

        assert_eq!(expected, actual);
    }
}