Skip to content

Commit c7d6ffc

Browse files
committed
Add CfgExpr::walk utility method
1 parent 0322bdd commit c7d6ffc

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,29 @@ impl CfgExpr {
153153
CfgExpr::False => false,
154154
}
155155
}
156+
157+
/// Walk over all the `Cfg`s of the given `CfgExpr`, recursing into `not(...)`, `all(...)`,
158+
/// `any(...)` and stopping at the first error and returning that error.
159+
pub fn walk<E>(&self, mut f: impl FnMut(&Cfg) -> Result<(), E>) -> Result<(), E> {
160+
fn walk_inner<E>(
161+
cfg_expr: &CfgExpr,
162+
f: &mut impl FnMut(&Cfg) -> Result<(), E>,
163+
) -> Result<(), E> {
164+
match *cfg_expr {
165+
CfgExpr::Not(ref e) => walk_inner(e, &mut *f),
166+
CfgExpr::All(ref e) | CfgExpr::Any(ref e) => {
167+
for e in e {
168+
let _ = walk_inner(e, &mut *f)?;
169+
}
170+
Ok(())
171+
}
172+
CfgExpr::Value(ref e) => f(e),
173+
CfgExpr::True | CfgExpr::False => Ok(()),
174+
}
175+
}
176+
177+
walk_inner(self, &mut f)
178+
}
156179
}
157180

158181
impl FromStr for CfgExpr {

0 commit comments

Comments
 (0)