esp_config/
lib.rs

1#![doc = include_str!("../README.md")]
2//! ## Feature Flags
3#![doc = document_features::document_features!(feature_label = r#"<span class="stab portability"><code>{feature}</code></span>"#)]
4#![doc(html_logo_url = "https://avatars.githubusercontent.com/u/46717278")]
5#![cfg_attr(not(feature = "build"), no_std)]
6#![deny(missing_docs, rust_2018_idioms, rustdoc::all)]
7
8#[cfg(feature = "build")]
9mod generate;
10#[cfg(feature = "build")]
11pub use generate::{
12    ConfigOption,
13    DisplayHint,
14    Error,
15    Stability,
16    generate_config,
17    generate_config_from_yaml_definition,
18    validator::Validator,
19    value::Value,
20};
21#[cfg(feature = "tui")]
22pub use generate::{do_checks, evaluate_yaml_config};
23
24/// Parse the value of an environment variable as a [bool] at compile time.
25#[macro_export]
26macro_rules! esp_config_bool {
27    ( $var:expr ) => {
28        match env!($var).as_bytes() {
29            b"true" => true,
30            b"false" => false,
31            _ => ::core::panic!("boolean value must be either 'true' or 'false'"),
32        }
33    };
34}
35
36/// Parse the value of an environment variable as an integer at compile time.
37#[macro_export]
38macro_rules! esp_config_int {
39    ( $ty:ty, $var:expr ) => {
40        const { $crate::esp_config_int_parse!($ty, env!($var)) }
41    };
42}
43
44/// Get the string value of an environment variable at compile time.
45#[macro_export]
46macro_rules! esp_config_str {
47    ( $var:expr ) => {
48        env!($var)
49    };
50}
51
52/// Parse a string like "777" into an integer, which _can_ be used in a `const`
53/// context
54///
55/// Not inlined into `esp_config_int` to make this easy to test.
56#[doc(hidden)] // To avoid confusion with `esp_config_int`, hide this in the docs
57#[macro_export]
58macro_rules! esp_config_int_parse {
59    ( $ty:ty, $s:expr ) => {{
60        let val: $ty = match <$ty>::from_str_radix($s, 10) {
61            Ok(val) => val as $ty,
62            Err(_) => {
63                core::assert!(false, "Unable to parse a config value as a number.");
64                0
65            }
66        };
67        val
68    }};
69}
70
71#[cfg(test)]
72mod test {
73    // We can only test success in the const context
74    const _: () = {
75        core::assert!(esp_config_int_parse!(i64, "-77777") == -77777);
76        core::assert!(esp_config_int_parse!(isize, "-7777") == -7777);
77        core::assert!(esp_config_int_parse!(i32, "-999") == -999);
78        core::assert!(esp_config_int_parse!(i16, "-99") == -99);
79        core::assert!(esp_config_int_parse!(i8, "-9") == -9);
80
81        core::assert!(esp_config_int_parse!(u64, "77777") == 77777);
82        core::assert!(esp_config_int_parse!(usize, "7777") == 7777);
83        core::assert!(esp_config_int_parse!(u32, "999") == 999);
84        core::assert!(esp_config_int_parse!(u16, "99") == 99);
85        core::assert!(esp_config_int_parse!(u8, "9") == 9);
86    };
87
88    #[test]
89    #[should_panic]
90    fn test_expect_positive() {
91        esp_config_int_parse!(u8, "-5");
92    }
93
94    #[test]
95    #[should_panic]
96    fn test_invalid_digit() {
97        esp_config_int_parse!(u32, "a");
98    }
99}