#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AppCommand {
SwitchDarkLightMode,
}
impl AppCommand {
pub const fn all() -> &'static [Self] {
&[Self::SwitchDarkLightMode]
}
pub const fn title(self) -> &'static str {
match self {
Self::SwitchDarkLightMode => "switch dark/light mode",
}
}
pub const fn description(self) -> &'static str {
match self {
Self::SwitchDarkLightMode => "Toggle the application theme",
}
}
pub fn matches_query(self, query: &str) -> bool {
let query = query.trim().to_lowercase();
query.is_empty()
|| self.title().contains(&query)
|| self.description().to_lowercase().contains(&query)
}
}
pub fn filter_commands(query: &str) -> Vec<AppCommand> {
AppCommand::all()
.iter()
.copied()
.filter(|command| command.matches_query(query))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lists_switch_dark_light_mode_command() {
assert_eq!(AppCommand::all(), &[AppCommand::SwitchDarkLightMode]);
assert_eq!(
AppCommand::SwitchDarkLightMode.title(),
"switch dark/light mode"
);
}
#[test]
fn filters_commands_by_title() {
assert_eq!(
filter_commands("dark"),
vec![AppCommand::SwitchDarkLightMode]
);
assert!(filter_commands("missing").is_empty());
}
#[test]
fn filters_commands_empty_query() {
assert_eq!(filter_commands(""), vec![AppCommand::SwitchDarkLightMode]);
}
#[test]
fn filters_commands_whitespace_only_query() {
assert_eq!(
filter_commands(" "),
vec![AppCommand::SwitchDarkLightMode]
);
}
#[test]
fn filters_commands_by_description() {
assert_eq!(
filter_commands("theme"),
vec![AppCommand::SwitchDarkLightMode]
);
}
}