#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DebugAction {
Toggle,
CopyFrame,
ToggleState,
ToggleActionLog,
ToggleComponents,
ToggleMouseCapture,
InspectCell { column: u16, row: u16 },
CloseOverlay,
RequestCapture,
ActionLogScrollUp,
ActionLogScrollDown,
ActionLogScrollTop,
ActionLogScrollBottom,
ActionLogPageUp,
ActionLogPageDown,
ActionLogShowDetail,
ActionLogBackToList,
StateTreeShowDetail,
StateTreeBackToTree,
ComponentShowDetail,
ComponentBackToList,
}
impl DebugAction {
pub const CMD_TOGGLE: &'static str = "debug.toggle";
pub const CMD_COPY_FRAME: &'static str = "debug.copy";
pub const CMD_TOGGLE_STATE: &'static str = "debug.state";
pub const CMD_TOGGLE_ACTION_LOG: &'static str = "debug.action_log";
pub const CMD_TOGGLE_COMPONENTS: &'static str = "debug.components";
pub const CMD_TOGGLE_MOUSE: &'static str = "debug.mouse";
pub const CMD_CLOSE_OVERLAY: &'static str = "debug.close";
pub fn from_command(cmd: &str) -> Option<Self> {
match cmd {
Self::CMD_TOGGLE => Some(Self::Toggle),
Self::CMD_COPY_FRAME => Some(Self::CopyFrame),
Self::CMD_TOGGLE_STATE => Some(Self::ToggleState),
Self::CMD_TOGGLE_ACTION_LOG => Some(Self::ToggleActionLog),
Self::CMD_TOGGLE_COMPONENTS => Some(Self::ToggleComponents),
Self::CMD_TOGGLE_MOUSE => Some(Self::ToggleMouseCapture),
Self::CMD_CLOSE_OVERLAY => Some(Self::CloseOverlay),
_ => None,
}
}
pub fn command(&self) -> Option<&'static str> {
match self {
Self::Toggle => Some(Self::CMD_TOGGLE),
Self::CopyFrame => Some(Self::CMD_COPY_FRAME),
Self::ToggleState => Some(Self::CMD_TOGGLE_STATE),
Self::ToggleActionLog => Some(Self::CMD_TOGGLE_ACTION_LOG),
Self::ToggleComponents => Some(Self::CMD_TOGGLE_COMPONENTS),
Self::ToggleMouseCapture => Some(Self::CMD_TOGGLE_MOUSE),
Self::CloseOverlay => Some(Self::CMD_CLOSE_OVERLAY),
Self::InspectCell { .. }
| Self::RequestCapture
| Self::ActionLogScrollUp
| Self::ActionLogScrollDown
| Self::ActionLogScrollTop
| Self::ActionLogScrollBottom
| Self::ActionLogPageUp
| Self::ActionLogPageDown
| Self::ActionLogShowDetail
| Self::ActionLogBackToList
| Self::StateTreeShowDetail
| Self::StateTreeBackToTree
| Self::ComponentShowDetail
| Self::ComponentBackToList => None,
}
}
}
#[derive(Debug)]
pub enum DebugSideEffect<A> {
ProcessQueuedActions(Vec<A>),
CopyToClipboard(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_command() {
assert_eq!(
DebugAction::from_command("debug.toggle"),
Some(DebugAction::Toggle)
);
assert_eq!(
DebugAction::from_command("debug.copy"),
Some(DebugAction::CopyFrame)
);
assert_eq!(
DebugAction::from_command("debug.state"),
Some(DebugAction::ToggleState)
);
assert_eq!(
DebugAction::from_command("debug.action_log"),
Some(DebugAction::ToggleActionLog)
);
assert_eq!(
DebugAction::from_command("debug.components"),
Some(DebugAction::ToggleComponents)
);
assert_eq!(DebugAction::from_command("unknown"), None);
}
#[test]
fn test_command_roundtrip() {
let actions = [
DebugAction::Toggle,
DebugAction::CopyFrame,
DebugAction::ToggleState,
DebugAction::ToggleActionLog,
DebugAction::ToggleComponents,
DebugAction::ToggleMouseCapture,
DebugAction::CloseOverlay,
];
for action in actions {
let cmd = action.command().expect("should have command");
let parsed = DebugAction::from_command(cmd).expect("should parse");
assert_eq!(parsed, action);
}
}
#[test]
fn test_scroll_actions_no_command() {
assert!(DebugAction::ActionLogScrollUp.command().is_none());
assert!(DebugAction::ActionLogScrollDown.command().is_none());
assert!(DebugAction::ActionLogScrollTop.command().is_none());
assert!(DebugAction::ActionLogScrollBottom.command().is_none());
}
}