Skip to content

Commit

Permalink
Fail if click handler refers to unknown button (#1951)
Browse files Browse the repository at this point in the history
Fixes #1918
  • Loading branch information
MaxVerevkin authored Oct 6, 2023
1 parent 2fc3d32 commit 830c398
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/click.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub enum MouseButton {
WheelDown,
Forward,
Back,
Unknown,
DoubleLeft,
}

Expand Down Expand Up @@ -93,7 +92,7 @@ impl<'de> Deserialize<'de> for MouseButton {
type Value = MouseButton;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("u64 or string")
formatter.write_str("button as int or string")
}

// ```toml
Expand All @@ -114,7 +113,7 @@ impl<'de> Deserialize<'de> for MouseButton {
"back" => Back,
// Experimental
"double_left" => DoubleLeft,
_ => Unknown,
other => return Err(E::custom(format!("unknown button '{other}'"))),
})
}

Expand All @@ -132,9 +131,9 @@ impl<'de> Deserialize<'de> for MouseButton {
3 => Right,
4 => WheelUp,
5 => WheelDown,
9 => Forward,
8 => Back,
_ => Unknown,
9 => Forward,
other => return Err(E::custom(format!("unknown button '{other}'"))),
})
}
fn visit_u64<E>(self, number: u64) -> Result<MouseButton, E>
Expand Down
9 changes: 8 additions & 1 deletion src/protocol/i3bar_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ fn unprocessed_events_stream(invert_scrolling: bool) -> BoxedStream<I3BarEvent>
button: MouseButton,
}

let event: I3BarEventRaw = serde_json::from_str(line).unwrap();
let event: I3BarEventRaw = match serde_json::from_str(line) {
Ok(event) => event,
Err(err) => {
eprintln!("Failed to deserialize click event.\nData: {line}\nError: {err}");
continue;
}
};

let (id, instance) = match event.instance {
Some(name) => {
let (id, instance) = name.split_once(':').unwrap();
Expand Down

0 comments on commit 830c398

Please sign in to comment.