1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2pub enum AppCommand {
3 SwitchDarkLightMode,
4}
5
6impl AppCommand {
7 pub const fn all() -> &'static [Self] {
8 &[Self::SwitchDarkLightMode]
9 }
10
11 pub const fn title(self) -> &'static str {
12 match self {
13 Self::SwitchDarkLightMode => "switch dark/light mode",
14 }
15 }
16
17 pub const fn description(self) -> &'static str {
18 match self {
19 Self::SwitchDarkLightMode => "Toggle the application theme",
20 }
21 }
22
23 pub fn matches_query(self, query: &str) -> bool {
24 let query = query.trim().to_lowercase();
25
26 query.is_empty()
27 || self.title().contains(&query)
28 || self.description().to_lowercase().contains(&query)
29 }
30}
31
32pub fn filter_commands(query: &str) -> Vec<AppCommand> {
33 AppCommand::all()
34 .iter()
35 .copied()
36 .filter(|command| command.matches_query(query))
37 .collect()
38}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43
44 #[test]
45 fn lists_switch_dark_light_mode_command() {
46 assert_eq!(AppCommand::all(), &[AppCommand::SwitchDarkLightMode]);
47 assert_eq!(
48 AppCommand::SwitchDarkLightMode.title(),
49 "switch dark/light mode"
50 );
51 }
52
53 #[test]
54 fn filters_commands_by_title() {
55 assert_eq!(
56 filter_commands("dark"),
57 vec![AppCommand::SwitchDarkLightMode]
58 );
59 assert!(filter_commands("missing").is_empty());
60 }
61
62 #[test]
63 fn filters_commands_empty_query() {
64 assert_eq!(filter_commands(""), vec![AppCommand::SwitchDarkLightMode]);
65 }
66
67 #[test]
68 fn filters_commands_whitespace_only_query() {
69 assert_eq!(
70 filter_commands(" "),
71 vec![AppCommand::SwitchDarkLightMode]
72 );
73 }
74
75 #[test]
76 fn filters_commands_by_description() {
77 assert_eq!(
78 filter_commands("theme"),
79 vec![AppCommand::SwitchDarkLightMode]
80 );
81 }
82}