Line data Source code
1 : use anyhow::{anyhow, Result}; 2 : 3 : macro_rules! declare_const { 4 : ($var: ident) => { 5 : pub const $var: &str = stringify!($var); 6 : }; 7 : } 8 : 9 : declare_const!(CARGO); 10 : declare_const!(CARGO_CRATE_NAME); 11 : declare_const!(CARGO_HOME); 12 : declare_const!(CARGO_INCREMENTAL); 13 : declare_const!(CARGO_MANIFEST_DIR); 14 : declare_const!(CARGO_PKG_NAME); 15 : declare_const!(CARGO_PRIMARY_PACKAGE); 16 : declare_const!(CARGO_TARGET_DIR); 17 : declare_const!(CARGO_TERM_COLOR); 18 : declare_const!(CI); 19 : declare_const!(CLIPPY_DISABLE_DOCS_LINKS); 20 : declare_const!(CLIPPY_DRIVER_PATH); 21 : declare_const!(DOCS_RS); 22 : declare_const!(DYLINT_DRIVER_PATH); 23 : declare_const!(DYLINT_LIBRARY_PATH); 24 : declare_const!(DYLINT_LIBS); 25 : declare_const!(DYLINT_LIST); 26 : declare_const!(DYLINT_METADATA); 27 : declare_const!(DYLINT_NO_DEPS); 28 : declare_const!(DYLINT_RUSTFLAGS); 29 : declare_const!(DYLINT_TOML); 30 : declare_const!(OUT_DIR); 31 : declare_const!(PATH); 32 : declare_const!(RUSTC); 33 : declare_const!(RUSTC_WORKSPACE_WRAPPER); 34 : declare_const!(RUSTFLAGS); 35 : declare_const!(RUSTUP_HOME); 36 : declare_const!(RUSTUP_TOOLCHAIN); 37 : declare_const!(RUST_BACKTRACE); 38 : declare_const!(TARGET); 39 : 40 : /// Returns true if the environment variable `key` is set to a non-zero value. 41 : /// 42 : /// # Examples 43 : /// 44 : /// ``` 45 : /// use dylint_internal::env::enabled; 46 : /// use std::env; 47 : /// 48 : /// env::set_var("FOO", "1"); 49 : /// assert_eq!(enabled("FOO"), true); 50 : /// 51 : /// env::set_var("FOO", "0"); 52 : /// assert_eq!(enabled("FOO"), false); 53 : /// 54 : /// env::remove_var("FOO"); 55 : /// assert_eq!(enabled("FOO"), false); 56 : /// ``` 57 : #[must_use] 58 0 : pub fn enabled(key: &str) -> bool { 59 0 : std::env::var(key).map_or(false, |value| value != "0") 60 0 : } 61 : 62 : /// A wrapper around `std::env::var` that converts the error into an `anyhow::Error`. 63 89 : pub fn var(key: &str) -> Result<String> { 64 89 : std::env::var(key).map_err(|err| anyhow!(format!("{err}: {key}"))) 65 89 : }