Skip to content

Commit

Permalink
Add CfgExpr::walk_expr utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Nov 2, 2024
1 parent 921501e commit 4ed4532
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions crates/cargo-platform/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@ impl CfgExpr {
CfgExpr::Value(ref e) => cfg.contains(e),
}
}

/// Walk over all the `CfgExpr`s of the given `CfgExpr`, recursing into `not(...)`, `all(...)`,
/// `any(...)` and stopping at the first error and returning that error.
pub fn walk_expr<E>(&self, mut f: impl FnMut(&CfgExpr) -> Result<(), E>) -> Result<(), E> {
fn walk_expr_inner<E>(
cfg_expr: &CfgExpr,
f: &mut impl FnMut(&CfgExpr) -> Result<(), E>,
) -> Result<(), E> {
f(cfg_expr)?;
match *cfg_expr {
CfgExpr::Not(ref e) => walk_expr_inner(e, &mut *f),
CfgExpr::All(ref e) | CfgExpr::Any(ref e) => {
for e in e {
let _ = walk_expr_inner(e, &mut *f)?;
}
Ok(())
}
CfgExpr::Value(_) => Ok(()),
}
}

walk_expr_inner(self, &mut f)
}
}

impl FromStr for CfgExpr {
Expand Down

0 comments on commit 4ed4532

Please sign in to comment.