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
mod error;

pub use error::Error;
pub use error::Result;

mod var;

pub use var::{env_var, EnvVar};

pub mod required {
    use std::str::FromStr;

    pub type Result<A> = crate::Result<A, <A as FromStr>::Err>;
}

pub mod optional {
    use std::str::FromStr;

    pub type Result<A> = crate::Result<Option<A>, <A as FromStr>::Err>;
}

#[cfg(test)]
mod tests {
    mod test_optional {
        use crate::env_var;
        use std::str::FromStr;

        #[test]
        fn return_none_if_not_found() {
            let sample = Sample {
                x: env_var("unknown_variable_name").as_optional().unwrap(),
            };
            assert_eq!(sample.x.is_none(), true);
        }

        #[test]
        fn can_call_from_str_if_defined() {
            let sample: Option<Sample> = env_var("PATH").as_optional().unwrap();
            assert_eq!(sample.is_some(), true);
        }

        #[derive(Debug)]
        struct Sample {
            x: Option<String>,
        }

        impl FromStr for Sample {
            type Err = <String as FromStr>::Err;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                Ok(Sample {
                    x: Some(s.to_string()),
                })
            }
        }
    }

    mod test_required {
        use crate::Error::NotPresent;
        use crate::{env_var, required};
        use std::str::FromStr;

        #[test]
        fn return_error_if_not_found() {
            let x: required::Result<String> = env_var("unknown_variable_name").as_required();
            match x {
                Err(NotPresent(key)) => assert_eq!(key, "unknown_variable_name"),
                Ok(_) => assert!(false, "unexpected success"),
                _ => assert!(false, "unexpected error type"),
            }
        }

        #[test]
        fn return_value_if_key_found() {
            let sample = Sample {
                x: env_var("PATH").as_required().unwrap(),
            };
            assert_eq!(sample.x.is_empty(), false);
        }

        #[test]
        fn can_call_from_str_if_defined() {
            let sample: Sample = env_var("PATH").as_required().unwrap();
            assert_eq!(sample.x.is_empty(), false);
        }

        #[derive(Debug)]
        struct Sample {
            x: String,
        }

        impl FromStr for Sample {
            type Err = <String as FromStr>::Err;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                Ok(Sample { x: s.to_string() })
            }
        }
    }
}