rb_sys_env/
defines.rs

1use std::{collections::HashMap, rc::Rc};
2
3/// The DEFINES variables from libruby.
4#[derive(Debug, Clone)]
5pub struct Defines {
6    raw_environment: Rc<HashMap<String, String>>,
7}
8
9impl Defines {
10    pub(crate) fn from_raw_environment(raw_environment: Rc<HashMap<String, String>>) -> Self {
11        Self { raw_environment }
12    }
13
14    /// Determines the given key is true.
15    pub fn is_value_true(&self, key: &str) -> bool {
16        self.raw_environment
17            .get(format!("DEFINES_{}", key).as_str())
18            .map(|v| v == "1" || v == "true")
19            .unwrap_or(false)
20    }
21
22    /// Fetches the raw value for the given key.
23    pub fn get_raw_value(&self, key: &str) -> Option<&str> {
24        self.raw_environment
25            .get(format!("DEFINES_{}", key).as_str())
26            .map(|v| v.as_str())
27    }
28
29    pub(crate) fn print_cargo_rustc_cfg(&self) {
30        for (key, val) in self.raw_environment.iter() {
31            rustc_cfg!(
32                key.starts_with("DEFINES_") && val == "true",
33                "ruby_{}",
34                key.trim_start_matches("DEFINES_").to_lowercase()
35            );
36        }
37    }
38}