use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Action {
MoveUp,
MoveDown,
MoveLeft,
MoveRight,
PageUp,
PageDown,
GoToTop,
GoToEnd,
Home,
End,
Confirm,
Cancel,
ToggleSelect,
SelectAll,
DeselectAll,
Quit,
Help,
Delete,
Edit,
Create,
Search,
Refresh,
Sync,
CheckStatus,
Install,
Import,
Move,
Backspace,
DeleteChar,
NextTab,
PrevTab,
ScrollUp,
ScrollDown,
Yes,
No,
Save,
ToggleBackup,
}
impl Action {
#[must_use]
pub fn description(&self) -> &'static str {
match self {
Action::MoveUp => "Move up",
Action::MoveDown => "Move down",
Action::MoveLeft => "Move left",
Action::MoveRight => "Move right",
Action::PageUp => "Page up",
Action::PageDown => "Page down",
Action::GoToTop => "Go to top",
Action::GoToEnd => "Go to end",
Action::Home => "Home",
Action::End => "End",
Action::Confirm => "Confirm",
Action::Cancel => "Cancel / Go back",
Action::ToggleSelect => "Toggle selection",
Action::SelectAll => "Select all",
Action::DeselectAll => "Deselect all",
Action::Quit => "Quit",
Action::Help => "Show help",
Action::Delete => "Delete",
Action::Edit => "Edit",
Action::Create => "Create new",
Action::Search => "Search",
Action::Move => "Move",
Action::Refresh => "Refresh",
Action::Sync => "Sync with remote",
Action::CheckStatus => "Check status",
Action::Install => "Install",
Action::Import => "Import from system",
Action::Backspace => "Backspace",
Action::DeleteChar => "Delete character",
Action::NextTab => "Next field",
Action::PrevTab => "Previous field",
Action::ScrollUp => "Scroll up",
Action::ScrollDown => "Scroll down",
Action::Yes => "Yes",
Action::No => "No",
Action::Save => "Save",
Action::ToggleBackup => "Toggle backup",
}
}
#[must_use]
pub fn category(&self) -> &'static str {
match self {
Action::MoveUp
| Action::MoveDown
| Action::MoveLeft
| Action::MoveRight
| Action::PageUp
| Action::PageDown
| Action::GoToTop
| Action::GoToEnd
| Action::Home
| Action::End => "Navigation",
Action::Confirm
| Action::Cancel
| Action::ToggleSelect
| Action::SelectAll
| Action::DeselectAll => "Selection",
Action::Quit | Action::Help => "Global",
Action::Delete
| Action::Edit
| Action::Create
| Action::Search
| Action::Refresh
| Action::Move
| Action::Sync
| Action::CheckStatus
| Action::Install
| Action::Import => "Actions",
Action::Backspace | Action::DeleteChar => "Text Editing",
Action::NextTab | Action::PrevTab => "Field Navigation",
Action::ScrollUp | Action::ScrollDown => "Scroll",
Action::Yes | Action::No => "Prompts",
Action::Save | Action::ToggleBackup => "Actions",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_action_description() {
assert_eq!(Action::MoveUp.description(), "Move up");
assert_eq!(Action::Quit.description(), "Quit");
}
#[test]
fn test_action_category() {
assert_eq!(Action::MoveUp.category(), "Navigation");
assert_eq!(Action::Quit.category(), "Global");
assert_eq!(Action::Sync.category(), "Actions");
}
#[test]
fn test_action_serialization() {
let action = Action::MoveUp;
let json = serde_json::to_string(&action).unwrap();
assert_eq!(json, "\"move_up\"");
}
#[test]
fn test_action_deserialization() {
let action: Action = serde_json::from_str("\"move_down\"").unwrap();
assert_eq!(action, Action::MoveDown);
}
}