Skip to content

Commit

Permalink
chore: cleanup generics
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski committed Sep 18, 2024
1 parent 8959409 commit c451c2f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 38 deletions.
21 changes: 1 addition & 20 deletions rm-config/src/keymap/actions/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,6 @@ pub enum GeneralActionMergable {
}

impl UserAction for GeneralAction {
fn is_mergable_with(&self, other: &GeneralAction) -> bool {
let other = *other;
match self {
GeneralAction::SwitchToTorrents => other == Self::SwitchToSearch,
GeneralAction::SwitchToSearch => other == Self::SwitchToTorrents,
GeneralAction::Left => other == Self::Right,
GeneralAction::Right => other == Self::Left,
GeneralAction::Down => other == Self::Up,
GeneralAction::Up => other == Self::Down,
GeneralAction::ScrollPageDown => other == Self::ScrollPageUp,
GeneralAction::ScrollPageUp => other == Self::ScrollPageDown,
GeneralAction::GoToBeginning => other == Self::GoToEnd,
GeneralAction::GoToEnd => other == Self::GoToBeginning,
GeneralAction::MoveToColumnLeft => other == Self::MoveToColumnRight,
GeneralAction::MoveToColumnRight => other == Self::MoveToColumnLeft,
_ => false,
}
}

fn desc(&self) -> &'static str {
match self {
GeneralAction::ShowHelp => "toggle help",
Expand All @@ -80,7 +61,7 @@ impl UserAction for GeneralAction {
}
}

fn merged_desc(&self, other: &GeneralAction) -> Option<&'static str> {
fn merge_desc_with(&self, other: &GeneralAction) -> Option<&'static str> {
match (&self, other) {
(Self::Left, Self::Right) => Some("switch to tab left / right"),
(Self::Right, Self::Left) => Some("switch to tab right / left"),
Expand Down
6 changes: 1 addition & 5 deletions rm-config/src/keymap/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ pub mod torrents_tab;

pub trait UserAction: Into<Action> {
fn desc(&self) -> &'static str;
fn merged_desc(&self, other: &Self) -> Option<&'static str> {
fn merge_desc_with(&self, other: &Self) -> Option<&'static str> {
let _ = other;
None
}
fn is_mergable_with(&self, other: &Self) -> bool {
let _ = other;
false
}
}
21 changes: 8 additions & 13 deletions rm-config/src/keymap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct KeybindsHolder<T: Into<Action> + UserAction> {
pub keybindings: Vec<Keybinding<T>>,
}

impl<T: Into<Action> + Ord + UserAction> KeybindsHolder<T> {
impl<T: Ord + UserAction> KeybindsHolder<T> {
const KEYS_DELIMITER: &'static str = ", ";

pub fn get_help_repr(&self) -> Vec<(String, &'static str)> {
Expand Down Expand Up @@ -73,19 +73,15 @@ impl<T: Into<Action> + Ord + UserAction> KeybindsHolder<T> {
}

if let Some((next_action, next_keycodes)) = new_keys.get(idx + 1) {
if action.is_mergable_with(next_action) {
if let Some(merged_desc) = action.merge_desc_with(next_action) {
skip_next_loop = true;
let keys = format!(
"{} / {}",
keycodes.join(Self::KEYS_DELIMITER),
next_keycodes.join(Self::KEYS_DELIMITER)
);

let desc = action
.merged_desc(next_action)
.expect("keys checked for mergability before");

res.push((keys, desc));
res.push((keys, merged_desc));
continue;
}
}
Expand All @@ -100,15 +96,15 @@ impl<T: Into<Action> + Ord + UserAction> KeybindsHolder<T> {
}

#[derive(Serialize, Clone)]
pub struct Keybinding<T: UserAction> {
pub struct Keybinding<T> {
pub on: KeyCode,
#[serde(default)]
pub modifier: KeyModifier,
pub action: T,
pub show_in_help: bool,
}

impl<T: Into<Action> + UserAction> Keybinding<T> {
impl<T> Keybinding<T> {
pub fn keycode_string(&self) -> String {
let key = match self.on {
KeyCode::Backspace => "Backspace".into(),
Expand Down Expand Up @@ -154,7 +150,7 @@ impl<T: Into<Action> + UserAction> Keybinding<T> {
}
}

impl<T: UserAction> Keybinding<T> {
impl<T> Keybinding<T> {
fn new(on: KeyCode, action: T, modifier: Option<KeyModifier>, show_in_help: bool) -> Self {
Self {
on,
Expand All @@ -165,7 +161,7 @@ impl<T: UserAction> Keybinding<T> {
}
}

impl<'de, T: Deserialize<'de> + UserAction> Deserialize<'de> for Keybinding<T> {
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Keybinding<T> {
fn deserialize<D>(deserializer: D) -> std::prelude::v1::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
Expand All @@ -183,7 +179,7 @@ impl<'de, T: Deserialize<'de> + UserAction> Deserialize<'de> for Keybinding<T> {
phantom: PhantomData<T>,
}

impl<'de, T: Deserialize<'de> + UserAction> Visitor<'de> for KeybindingVisitor<T> {
impl<'de, T: Deserialize<'de>> Visitor<'de> for KeybindingVisitor<T> {
type Value = Keybinding<T>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
Expand Down Expand Up @@ -398,7 +394,6 @@ impl KeymapConfig {
} else {
Some(keys)
}

}

fn populate_hashmap(&mut self) {
Expand Down

0 comments on commit c451c2f

Please sign in to comment.