rb_sys_env/
defines.rs

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
use std::{collections::HashMap, rc::Rc};

/// The DEFINES variables from libruby.
#[derive(Debug, Clone)]
pub struct Defines {
    raw_environment: Rc<HashMap<String, String>>,
}

impl Defines {
    pub(crate) fn from_raw_environment(raw_environment: Rc<HashMap<String, String>>) -> Self {
        Self { raw_environment }
    }

    /// Determines the given key is true.
    pub fn is_value_true(&self, key: &str) -> bool {
        self.raw_environment
            .get(format!("DEFINES_{}", key).as_str())
            .map(|v| v == "1" || v == "true")
            .unwrap_or(false)
    }

    /// Fetches the raw value for the given key.
    pub fn get_raw_value(&self, key: &str) -> Option<&str> {
        self.raw_environment
            .get(format!("DEFINES_{}", key).as_str())
            .map(|v| v.as_str())
    }

    pub(crate) fn print_cargo_rustc_cfg(&self) {
        for (key, val) in self.raw_environment.iter() {
            rustc_cfg!(
                key.starts_with("DEFINES_") && val == "true",
                "ruby_{}",
                key.trim_start_matches("DEFINES_").to_lowercase()
            );
        }
    }
}