Skip to content

Commit

Permalink
Implement Ord for Keybind
Browse files Browse the repository at this point in the history
  • Loading branch information
nick42d committed Dec 4, 2024
1 parent a85f549 commit b9e842f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions youtui/src/app/keycommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::config::keybinds::KeyActionTree;

use super::component::actionhandler::Action;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use crossterm::event::{KeyCode, KeyModifiers};
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, char::ParseCharError, fmt::Display, str::FromStr};

Expand All @@ -20,12 +20,20 @@ pub enum CommandVisibility {
Hidden,
}

#[derive(Hash, Ord, Eq, PartialEq, PartialOrd, Debug, Deserialize, Clone, Serialize)]
#[derive(Hash, Eq, PartialEq, PartialOrd, Debug, Deserialize, Clone, Serialize)]
#[serde(try_from = "String")]
pub struct Keybind {
pub code: KeyCode,
pub modifiers: KeyModifiers,
}
// Since KeyCode and KeyModifiers derive PartialOrd, it's safe to implement this
// as per below. TODO: PR upstream
#[allow(clippy::derive_ord_xor_partial_ord)]
impl Ord for Keybind {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.partial_cmp(other).expect("Keybind should be able to provide ordering for any values. Has crossterm made a breaking change?")
}
}
impl TryFrom<String> for Keybind {
type Error = <Keybind as FromStr>::Err;
fn try_from(value: String) -> Result<Self, Self::Error> {
Expand Down

0 comments on commit b9e842f

Please sign in to comment.