bool_logic/cfg/
ast.rs

1pub use crate::ast::{All, Any, Not, Var};
2
3use std::fmt;
4
5pub type Expr = crate::ast::Expr<Pred>;
6
7pub fn expr(x: impl Into<Expr>) -> Expr {
8    x.into()
9}
10
11pub fn any(x: impl Into<Any<Pred>>) -> Any<Pred> {
12    x.into()
13}
14
15pub fn all(x: impl Into<All<Pred>>) -> All<Pred> {
16    x.into()
17}
18
19pub fn not(x: impl Into<Not<Pred>>) -> Not<Pred> {
20    x.into()
21}
22
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct Pred {
25    pub key: String,
26    pub value: Option<String>,
27}
28
29impl From<Pred> for Expr {
30    fn from(x: Pred) -> Self {
31        Expr::Var(Var(x))
32    }
33}
34
35pub fn flag(s: impl Into<String>) -> Pred {
36    Pred {
37        key: s.into(),
38        value: None,
39    }
40}
41
42pub fn key_value(s: impl Into<String>, v: impl Into<String>) -> Pred {
43    Pred {
44        key: s.into(),
45        value: Some(v.into()),
46    }
47}
48
49pub fn target_family(s: impl Into<String>) -> Pred {
50    key_value("target_family", s)
51}
52
53pub fn target_vendor(s: impl Into<String>) -> Pred {
54    key_value("target_vendor", s)
55}
56
57pub fn target_arch(s: impl Into<String>) -> Pred {
58    key_value("target_arch", s)
59}
60
61pub fn target_os(s: impl Into<String>) -> Pred {
62    key_value("target_os", s)
63}
64
65pub fn target_env(s: impl Into<String>) -> Pred {
66    key_value("target_env", s)
67}
68
69pub fn target_pointer_width(s: impl Into<String>) -> Pred {
70    key_value("target_pointer_width", s)
71}
72
73impl fmt::Display for Pred {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        let key = self.key.as_str();
76        match &self.value {
77            Some(value) => write!(f, "{key} = {value:?}"),
78            None => write!(f, "{key}"),
79        }
80    }
81}
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86
87    #[test]
88    fn cfg_string() {
89        {
90            let cfg = expr(target_family("unix"));
91            let expected = r#"target_family = "unix""#;
92            assert_eq!(cfg.to_string(), expected);
93        }
94        {
95            let cfg = expr(any((target_os("linux"), target_os("android"))));
96            let expected = r#"any(target_os = "linux", target_os = "android")"#;
97            assert_eq!(cfg.to_string(), expected);
98        }
99    }
100}