From 5f69311dfaaea3cdbc44900e93c0a256f9654743 Mon Sep 17 00:00:00 2001 From: Soispha Date: Tue, 7 Nov 2023 20:25:55 +0100 Subject: [PATCH] fix(keymaps/crossterm): Implement it fully --- keymaps/src/error/mod.rs | 6 ++++++ keymaps/src/key_repr/key/crossterm.rs | 15 ++++++--------- keymaps/src/key_repr/key/mod.rs | 2 +- keymaps/src/key_repr/key_value/crossterm.rs | 2 ++ keymaps/src/key_repr/key_value/mod.rs | 2 +- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/keymaps/src/error/mod.rs b/keymaps/src/error/mod.rs index ffc4a93..a2baacb 100644 --- a/keymaps/src/error/mod.rs +++ b/keymaps/src/error/mod.rs @@ -38,3 +38,9 @@ pub enum KeyValueParseError { #[error("The number associate with the F key ('{0}') can't be parsed as u8!")] CantParseFNumber(#[from] ParseIntError), } + +#[derive(Error, Debug)] +pub enum KeyFromCrossterm { + #[error("Can not parse non Key event to a Key code: ('{0:?}')")] + OnlyKey(crossterm::event::Event), +} diff --git a/keymaps/src/key_repr/key/crossterm.rs b/keymaps/src/key_repr/key/crossterm.rs index 720a009..d814d47 100644 --- a/keymaps/src/key_repr/key/crossterm.rs +++ b/keymaps/src/key_repr/key/crossterm.rs @@ -1,7 +1,9 @@ -use log::{debug, info}; - use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers}; +use crate::{error, key_repr::KeyValue}; + +use super::Key; + impl Into for Key { fn into(self) -> Event { let mut modifiers; @@ -43,9 +45,8 @@ impl Into for Key { } } -#[cfg(crossterm)] impl TryFrom<&Event> for Key { - type Error = anyhow::Error; + type Error = error::KeyFromCrossterm; fn try_from(value: &Event) -> std::result::Result { let mut output_key: Key = Key::new(); @@ -69,11 +70,7 @@ impl TryFrom<&Event> for Key { Ok(output_key) } - Event::Mouse(_) - | Event::Paste(_) - | Event::Resize(_, _) - | Event::FocusGained - | Event::FocusLost => bail!("Only supports parsing from key event"), + event => Err(error::KeyFromCrossterm::OnlyKey(event.to_owned())), } } } diff --git a/keymaps/src/key_repr/key/mod.rs b/keymaps/src/key_repr/key/mod.rs index 66a744d..e1fae0a 100644 --- a/keymaps/src/key_repr/key/mod.rs +++ b/keymaps/src/key_repr/key/mod.rs @@ -1,4 +1,4 @@ -#[cfg(crossterm)] +#[cfg(feature = "crossterm")] mod crossterm; mod parsing; diff --git a/keymaps/src/key_repr/key_value/crossterm.rs b/keymaps/src/key_repr/key_value/crossterm.rs index 126946d..178e860 100644 --- a/keymaps/src/key_repr/key_value/crossterm.rs +++ b/keymaps/src/key_repr/key_value/crossterm.rs @@ -1,5 +1,7 @@ use crossterm::event::KeyCode; +use super::KeyValue; + impl From for KeyValue { fn from(value: KeyCode) -> Self { match value { diff --git a/keymaps/src/key_repr/key_value/mod.rs b/keymaps/src/key_repr/key_value/mod.rs index ea6b233..61240d4 100644 --- a/keymaps/src/key_repr/key_value/mod.rs +++ b/keymaps/src/key_repr/key_value/mod.rs @@ -1,4 +1,4 @@ -#[cfg(crossterm)] +#[cfg(feature = "crossterm")] mod crossterm; use std::{