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
// Copyright (c) 2018 Joseph R. Quinn <quinn.josephr@protonmail.com>
// SPDX-License-Identifier: ISC

//! [![Build Status](https://travis-ci.org/quinnjr/is-dev-rs.svg?branch=master)](https://travis-ci.org/quinnjr/is-dev-rs)
//!
//! A simple macro to determine if an environment is
//! a "development" environment.
//!
//! Built because some of us (me) are lazy and don't feel like
//! copy-pasting the same check for development
//! environments over and over again.
//!
//! :heart:

#![allow(unused_imports)]
use std::env;
use std::ffi::OsStr;

#[allow(unused_macros)]
macro_rules! is_dev {
    ($var: expr, $m: expr) => {{
        match env::var_os($var)
        {
            Some(val) => val == OsStr::new($m),
            None => false
        }
    }};
}

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

    #[test]
    // Only tests positive with ENV=development
    // set on the testing machine.
    fn test_is_dev() {
        let e: bool = is_dev!("ENV", "development");
        assert!(e)
    }

    #[test]
    fn test_is_not_dev() {
        let e: bool = is_dev!("NODE_ENV", "development");
        assert!(!e);
        let f: bool = is_dev!("SUPERSEKRITKEY", "development");
        assert!(!f)
    }
}