Skip to content

Commit

Permalink
refactor(switch): refine switch error expr choice
Browse files Browse the repository at this point in the history
  • Loading branch information
jtroo committed Oct 11, 2024
1 parent d54257a commit 10448d8
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions parser/src/cfg/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn parse_switch_case_bool(
.list(s.vars())
.expect("must be a list, checked atom");
if l.is_empty() {
bail_expr!(op_expr, "key match cannot contain empty lists inside");
bail_expr!(op_expr, "switch logic cannot contain empty lists inside");
}
#[derive(PartialEq)]
enum AllowedListOps {
Expand Down Expand Up @@ -122,7 +122,9 @@ pub fn parse_switch_case_bool(
.ok_or_else(|| {
anyhow_expr!(
op_expr,
"lists inside key match must begin with one of: or | and | not | key-history"
"lists inside switch logic must begin with one of:\n\
or | and | not | key-history | key-timing\n\
| input | input-history | layer | base-layer",
)
})?;

Expand All @@ -137,7 +139,7 @@ pub fn parse_switch_case_bool(
let osc = l[1]
.atom(s.vars())
.and_then(str_to_oscode)
.ok_or_else(|| anyhow_expr!(op_expr, "invalid key name"))?;
.ok_or_else(|| anyhow_expr!(&l[1], "invalid key name"))?;
let key_recency = parse_u8_with_range(&l[2], s, "key-recency", 1, 8)? - 1;
ops.push(OpCode::new_key_history(osc.into(), key_recency));
Ok(())
Expand All @@ -152,23 +154,23 @@ pub fn parse_switch_case_bool(

let input_type = match l[1]
.atom(s.vars())
.ok_or_else(|| anyhow_expr!(op_expr, "key-type must be virtual|real"))?
.ok_or_else(|| anyhow_expr!(&l[1], "key-type must be virtual|real"))?
{
"real" => InputType::Real,
"fake" | "virtual" => InputType::Virtual,
_ => bail_expr!(op_expr, "key-type must be virtual|real"),
};
let input = l[2]
.atom(s.vars())
.ok_or_else(|| anyhow_expr!(op_expr, "input key name must not be a list"))?;
.ok_or_else(|| anyhow_expr!(&l[2], "input key name must not be a list"))?;
let input = match input_type {
InputType::Real => u16::from(
str_to_oscode(input)
.ok_or_else(|| anyhow_expr!(op_expr, "invalid input key name"))?,
.ok_or_else(|| anyhow_expr!(&l[2], "invalid input key name"))?,
),
InputType::Virtual => {
let vk = s.virtual_keys.get(input).ok_or_else(|| {
anyhow_expr!(op_expr, "virtual key name is not defined")
anyhow_expr!(&l[2], "virtual key name is not defined")
})?;
assert!(vk.0 < usize::from(KEY_MAX));
vk.0 as u16
Expand All @@ -185,23 +187,23 @@ pub fn parse_switch_case_bool(

let input_type = match l[1]
.atom(s.vars())
.ok_or_else(|| anyhow_expr!(op_expr, "key-type must be virtual|real"))?
.ok_or_else(|| anyhow_expr!(&l[1], "key-type must be virtual|real"))?
{
"real" => InputType::Real,
"fake" | "virtual" => InputType::Virtual,
_ => bail_expr!(op_expr, "key-type must be virtual|real"),
_ => bail_expr!(&l[1], "key-type must be virtual|real"),
};
let input = l[2]
.atom(s.vars())
.ok_or_else(|| anyhow_expr!(op_expr, "input key name must not be a list"))?;
.ok_or_else(|| anyhow_expr!(&l[2], "input key name must not be a list"))?;
let input = match input_type {
InputType::Real => u16::from(
str_to_oscode(input)
.ok_or_else(|| anyhow_expr!(op_expr, "invalid input key name"))?,
.ok_or_else(|| anyhow_expr!(&l[2], "invalid input key name"))?,
),
InputType::Virtual => {
let vk = s.virtual_keys.get(input).ok_or_else(|| {
anyhow_expr!(op_expr, "virtual key name is not defined")
anyhow_expr!(&l[2], "virtual key name is not defined")
})?;
assert!(vk.0 < usize::from(KEY_MAX));
vk.0 as u16
Expand Down

0 comments on commit 10448d8

Please sign in to comment.