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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use std::fmt;

pub use bool_logic::ast::All;
pub use bool_logic::ast::Any;
pub use bool_logic::ast::Not;
pub use bool_logic::ast::Var;

pub type Expr = bool_logic::ast::Expr<Pred>;

pub fn expr(x: impl Into<Expr>) -> Expr {
    x.into()
}

pub fn any(x: impl Into<Any<Pred>>) -> Any<Pred> {
    x.into()
}

pub fn all(x: impl Into<All<Pred>>) -> All<Pred> {
    x.into()
}

pub fn not(x: impl Into<Not<Pred>>) -> Not<Pred> {
    x.into()
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Pred {
    TargetFamily(String),
    TargetVendor(String),
    TargetArch(String),
    TargetOs(String),
    TargetEnv(String),
    TargetPointerWidth(String),
}

pub fn target_family(s: impl Into<String>) -> Pred {
    Pred::TargetFamily(s.into())
}

pub fn target_vendor(s: impl Into<String>) -> Pred {
    Pred::TargetVendor(s.into())
}

pub fn target_arch(s: impl Into<String>) -> Pred {
    Pred::TargetArch(s.into())
}

pub fn target_os(s: impl Into<String>) -> Pred {
    Pred::TargetOs(s.into())
}

pub fn target_env(s: impl Into<String>) -> Pred {
    Pred::TargetEnv(s.into())
}

pub fn target_pointer_width(s: impl Into<String>) -> Pred {
    Pred::TargetPointerWidth(s.into())
}

impl From<Pred> for Expr {
    fn from(x: Pred) -> Self {
        Expr::Var(Var(x))
    }
}

impl fmt::Display for Pred {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Pred::TargetFamily(s) => match s.as_str() {
                "unix" | "windows" | "wasm" => write!(f, "{}", s),
                _ => fmt_kv(f, "target_family", s),
            },
            Pred::TargetVendor(s) => fmt_kv(f, "target_vendor", s),
            Pred::TargetArch(s) => fmt_kv(f, "target_arch", s),
            Pred::TargetOs(s) => fmt_kv(f, "target_os", s),
            Pred::TargetEnv(s) => fmt_kv(f, "target_env", s),
            Pred::TargetPointerWidth(s) => fmt_kv(f, "target_pointer_width", s),
        }
    }
}

fn fmt_kv(f: &mut fmt::Formatter<'_>, key: &str, value: &str) -> fmt::Result {
    write!(f, "{key} = {value:?}")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cfg_string() {
        {
            let cfg = expr(target_family("unix"));
            let expected = "unix";
            assert_eq!(cfg.to_string(), expected);
        }
        {
            let cfg = expr(any((target_os("linux"), target_os("android"))));
            let expected = r#"any(target_os = "linux", target_os = "android")"#;
            assert_eq!(cfg.to_string(), expected);
        }
    }
}