Skip to content

Commit c552ec3

Browse files
committed
Add CfgExpr::walk_expr utility method
1 parent 2781bc4 commit c552ec3

File tree

1 file changed

+23
-0
lines changed
  • crates/cargo-platform/src

1 file changed

+23
-0
lines changed

crates/cargo-platform/src/cfg.rs

+23
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,29 @@ impl CfgExpr {
8989
CfgExpr::Value(ref e) => cfg.contains(e),
9090
}
9191
}
92+
93+
/// Walk over all the `CfgExpr`s of the given `CfgExpr`, recursing into `not(...)`, `all(...)`,
94+
/// `any(...)` and stopping at the first error and returning that error.
95+
pub fn walk_expr<E>(&self, mut f: impl FnMut(&CfgExpr) -> Result<(), E>) -> Result<(), E> {
96+
fn walk_expr_inner<E>(
97+
cfg_expr: &CfgExpr,
98+
f: &mut impl FnMut(&CfgExpr) -> Result<(), E>,
99+
) -> Result<(), E> {
100+
f(cfg_expr)?;
101+
match *cfg_expr {
102+
CfgExpr::Not(ref e) => walk_expr_inner(e, &mut *f),
103+
CfgExpr::All(ref e) | CfgExpr::Any(ref e) => {
104+
for e in e {
105+
let _ = walk_expr_inner(e, &mut *f)?;
106+
}
107+
Ok(())
108+
}
109+
CfgExpr::Value(_) => Ok(()),
110+
}
111+
}
112+
113+
walk_expr_inner(self, &mut f)
114+
}
92115
}
93116

94117
impl FromStr for CfgExpr {

0 commit comments

Comments
 (0)