From fc86d7620e960f3e30adef55b6af755a3c5b6c13 Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Tue, 7 Jan 2020 15:59:23 +0000 Subject: [PATCH] Fix keybinding deserialization Fixes #3160. --- alacritty/src/cli.rs | 2 +- alacritty/src/config/bindings.rs | 46 +++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/alacritty/src/cli.rs b/alacritty/src/cli.rs index 8abee061..a7b97385 100644 --- a/alacritty/src/cli.rs +++ b/alacritty/src/cli.rs @@ -165,7 +165,7 @@ impl Options { .help("Start the shell in the specified working directory"), ) .arg(Arg::with_name("config-file").long("config-file").takes_value(true).help( - &format!("Specify alternative configuration file [default: {}]", CONFIG_PATH) + &format!("Specify alternative configuration file [default: {}]", CONFIG_PATH), )) .arg( Arg::with_name("command") diff --git a/alacritty/src/config/bindings.rs b/alacritty/src/config/bindings.rs index bdbb7c07..abdb1727 100644 --- a/alacritty/src/config/bindings.rs +++ b/alacritty/src/config/bindings.rs @@ -469,19 +469,35 @@ pub fn platform_key_bindings() -> Vec { vec![] } -#[derive(Deserialize, Copy, Clone, Debug, Eq, PartialEq, Hash)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum Key { Scancode(u32), Keycode(VirtualKeyCode), } +impl<'a> Deserialize<'a> for Key { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'a>, + { + let value = serde_yaml::Value::deserialize(deserializer)?; + match u32::deserialize(value.clone()) { + Ok(scancode) => Ok(Key::Scancode(scancode)), + Err(_) => { + let keycode = VirtualKeyCode::deserialize(value).map_err(D::Error::custom)?; + Ok(Key::Keycode(keycode)) + }, + } + } +} + struct ModeWrapper { pub mode: TermMode, pub not_mode: TermMode, } impl<'a> Deserialize<'a> for ModeWrapper { - fn deserialize(deserializer: D) -> ::std::result::Result + fn deserialize(deserializer: D) -> Result where D: Deserializer<'a>, { @@ -494,7 +510,7 @@ impl<'a> Deserialize<'a> for ModeWrapper { f.write_str("Combination of AppCursor | AppKeypad, possibly with negation (~)") } - fn visit_str(self, value: &str) -> ::std::result::Result + fn visit_str(self, value: &str) -> Result where E: de::Error, { @@ -528,7 +544,7 @@ impl MouseButtonWrapper { } impl<'a> Deserialize<'a> for MouseButtonWrapper { - fn deserialize(deserializer: D) -> ::std::result::Result + fn deserialize(deserializer: D) -> Result where D: Deserializer<'a>, { @@ -541,7 +557,7 @@ impl<'a> Deserialize<'a> for MouseButtonWrapper { f.write_str("Left, Right, Middle, or a number") } - fn visit_str(self, value: &str) -> ::std::result::Result + fn visit_str(self, value: &str) -> Result where E: de::Error, { @@ -577,7 +593,7 @@ struct RawBinding { } impl RawBinding { - fn into_mouse_binding(self) -> ::std::result::Result { + fn into_mouse_binding(self) -> Result { if let Some(mouse) = self.mouse { Ok(Binding { trigger: mouse, @@ -591,7 +607,7 @@ impl RawBinding { } } - fn into_key_binding(self) -> ::std::result::Result { + fn into_key_binding(self) -> Result { if let Some(key) = self.key { Ok(KeyBinding { trigger: key, @@ -607,7 +623,7 @@ impl RawBinding { } impl<'a> Deserialize<'a> for RawBinding { - fn deserialize(deserializer: D) -> ::std::result::Result + fn deserialize(deserializer: D) -> Result where D: Deserializer<'a>, { @@ -622,7 +638,7 @@ impl<'a> Deserialize<'a> for RawBinding { } impl<'a> Deserialize<'a> for Field { - fn deserialize(deserializer: D) -> ::std::result::Result + fn deserialize(deserializer: D) -> Result where D: Deserializer<'a>, { @@ -638,7 +654,7 @@ impl<'a> Deserialize<'a> for RawBinding { f.write_str("binding fields") } - fn visit_str(self, value: &str) -> ::std::result::Result + fn visit_str(self, value: &str) -> Result where E: de::Error, { @@ -667,7 +683,7 @@ impl<'a> Deserialize<'a> for RawBinding { f.write_str("binding specification") } - fn visit_map(self, mut map: V) -> ::std::result::Result + fn visit_map(self, mut map: V) -> Result where V: MapAccess<'a>, { @@ -787,7 +803,7 @@ impl<'a> Deserialize<'a> for RawBinding { } impl<'a> Deserialize<'a> for MouseBinding { - fn deserialize(deserializer: D) -> ::std::result::Result + fn deserialize(deserializer: D) -> Result where D: Deserializer<'a>, { @@ -797,7 +813,7 @@ impl<'a> Deserialize<'a> for MouseBinding { } impl<'a> Deserialize<'a> for KeyBinding { - fn deserialize(deserializer: D) -> ::std::result::Result + fn deserialize(deserializer: D) -> Result where D: Deserializer<'a>, { @@ -847,7 +863,7 @@ impl ModsWrapper { } impl<'a> de::Deserialize<'a> for ModsWrapper { - fn deserialize(deserializer: D) -> ::std::result::Result + fn deserialize(deserializer: D) -> Result where D: de::Deserializer<'a>, { @@ -860,7 +876,7 @@ impl<'a> de::Deserialize<'a> for ModsWrapper { f.write_str("Some subset of Command|Shift|Super|Alt|Option|Control") } - fn visit_str(self, value: &str) -> ::std::result::Result + fn visit_str(self, value: &str) -> Result where E: de::Error, {