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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
mod cfg_expr;
mod dnf;
#[cfg(test)]
mod tests;
use std::fmt;
use rustc_hash::FxHashSet;
use tt::SmolStr;
pub use cfg_expr::{CfgAtom, CfgExpr};
pub use dnf::DnfExpr;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CfgOptions {
enabled: FxHashSet<CfgAtom>,
}
impl CfgOptions {
pub fn check(&self, cfg: &CfgExpr) -> Option<bool> {
cfg.fold(&|atom| self.enabled.contains(atom))
}
pub fn insert_atom(&mut self, key: SmolStr) {
self.enabled.insert(CfgAtom::Flag(key));
}
pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) {
self.enabled.insert(CfgAtom::KeyValue { key, value });
}
pub fn apply_diff(&mut self, diff: CfgDiff) {
for atom in diff.enable {
self.enabled.insert(atom);
}
for atom in diff.disable {
self.enabled.remove(&atom);
}
}
}
pub struct CfgDiff {
enable: Vec<CfgAtom>,
disable: Vec<CfgAtom>,
}
impl CfgDiff {
pub fn len(&self) -> usize {
self.enable.len() + self.disable.len()
}
}
impl fmt::Display for CfgDiff {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if !self.enable.is_empty() {
f.write_str("enable ")?;
for (i, atom) in self.enable.iter().enumerate() {
let sep = match i {
0 => "",
_ if i == self.enable.len() - 1 => " and ",
_ => ", ",
};
f.write_str(sep)?;
write!(f, "{}", atom)?;
}
if !self.disable.is_empty() {
f.write_str("; ")?;
}
}
if !self.disable.is_empty() {
f.write_str("disable ")?;
for (i, atom) in self.disable.iter().enumerate() {
let sep = match i {
0 => "",
_ if i == self.enable.len() - 1 => " and ",
_ => ", ",
};
f.write_str(sep)?;
write!(f, "{}", atom)?;
}
}
Ok(())
}
}
pub struct InactiveReason {
enabled: Vec<CfgAtom>,
disabled: Vec<CfgAtom>,
}
impl fmt::Display for InactiveReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if !self.enabled.is_empty() {
for (i, atom) in self.enabled.iter().enumerate() {
let sep = match i {
0 => "",
_ if i == self.enabled.len() - 1 => " and ",
_ => ", ",
};
f.write_str(sep)?;
write!(f, "{}", atom)?;
}
let is_are = if self.enabled.len() == 1 { "is" } else { "are" };
write!(f, " {} enabled", is_are)?;
if !self.disabled.is_empty() {
f.write_str(" and ")?;
}
}
if !self.disabled.is_empty() {
for (i, atom) in self.disabled.iter().enumerate() {
let sep = match i {
0 => "",
_ if i == self.disabled.len() - 1 => " and ",
_ => ", ",
};
f.write_str(sep)?;
write!(f, "{}", atom)?;
}
let is_are = if self.disabled.len() == 1 { "is" } else { "are" };
write!(f, " {} disabled", is_are)?;
}
Ok(())
}
}