Skip to content

Commit bed213e

Browse files
committed
Add CfgExpr::try_for_each utility method
1 parent 8381d0c commit bed213e

File tree

1 file changed

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

1 file changed

+22
-0
lines changed

crates/cargo-platform/src/cfg.rs

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

94116
impl FromStr for CfgExpr {

0 commit comments

Comments
 (0)