Skip to content

Commit

Permalink
feat: error on invalid top-level config items (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtroo committed Feb 18, 2023
1 parent 9d2d4be commit 0bd8183
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ fn parse_cfg_raw(
let spanned_root_exprs = sexpr::parse(&text)?;
let root_exprs: Vec<_> = spanned_root_exprs.iter().map(|t| t.t.clone()).collect();

error_on_unknown_top_level_atoms(&root_exprs)?;

let cfg_expr = root_exprs
.iter()
.find(gen_first_atom_filter("defcfg"))
Expand Down Expand Up @@ -497,6 +499,35 @@ fn parse_cfg_raw(
Ok((cfg, src, layer_info, klayers, sequences, overrides))
}

fn error_on_unknown_top_level_atoms(exprs: &[Vec<SExpr>]) -> Result<()> {
for expr in exprs {
expr.first()
.ok_or_else(|| anyhow!("found an empty list as a configuration item"))?
.atom()
.map(|a| match a {
"defcfg"
| "defalias"
| "defsrc"
| "deflayer"
| "defoverrides"
| "deflocalkeys-linux"
| "deflocalkeys-win"
| "deflocalkeys-wintercept"
| "deffakekeys"
| "defchords"
| "defseq" => Ok(()),
_ => bail!("found unknown configuration item: {a}"),
})
.ok_or_else(|| {
anyhow!(
"found list as first item in a configuration item: {:?}",
expr.first().unwrap()
)
})??;
}
Ok(())
}

/// Return a closure that filters a root expression by the content of the first element. The
/// closure returns true if the first element is an atom that matches the input `a` and false
/// otherwise.
Expand Down

0 comments on commit 0bd8183

Please sign in to comment.