Skip to main content

lefthk_core/config/command/
exit_chord.rs

1use ron::ser::PrettyConfig;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5    config::command::utils::denormalize_function::DenormalizeCommandFunction, errors::Error,
6    worker::Worker,
7};
8
9use super::{Command, NormalizedCommand};
10
11inventory::submit! {DenormalizeCommandFunction::new::<ExitChord>()}
12
13#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize, Default)]
14pub struct ExitChord;
15
16impl ExitChord {
17    #[must_use]
18    pub fn new() -> Self {
19        Self
20    }
21}
22
23impl Command for ExitChord {
24    fn normalize(&self) -> NormalizedCommand {
25        let serialized_string =
26            ron::ser::to_string_pretty(self, PrettyConfig::new().struct_names(true)).unwrap();
27        NormalizedCommand(serialized_string)
28    }
29
30    fn denormalize(generalized: &NormalizedCommand) -> Option<Box<Self>> {
31        ron::from_str(&generalized.0).ok()
32    }
33
34    fn execute(&self, worker: &mut Worker) -> Error {
35        if worker.chord_ctx.keybinds.is_some() {
36            worker.chord_ctx.elapsed = true;
37        }
38
39        Ok(())
40    }
41
42    fn get_name(&self) -> &'static str {
43        "ExitChord"
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use crate::config::Command;
50
51    use super::ExitChord;
52
53    #[test]
54    fn normalize_process() {
55        let command = ExitChord::new();
56
57        let normalized = command.normalize();
58        let denormalized = ExitChord::denormalize(&normalized).unwrap();
59
60        assert_eq!(
61            Box::new(command),
62            denormalized,
63            "{normalized:?}, {denormalized:?}",
64        );
65    }
66}