Skip to main content

Module command

Module command 

Source
Expand description

Undoable command infrastructure for the undo/redo system.

This module provides the UndoableCmd trait for reversible operations and common command implementations for text editing and UI interactions.

§Design Principles

  1. Explicit state: Commands capture all state needed for undo/redo
  2. Memory-efficient: Commands report their size for budget management
  3. Mergeable: Consecutive similar commands can merge (e.g., typing)
  4. Traceable: Commands include metadata for debugging and UI display

§Invariants

  • execute() followed by undo() restores prior state exactly
  • undo() followed by redo() restores the executed state exactly
  • Commands with can_merge() == true MUST successfully merge
  • size_bytes() MUST be accurate for memory budgeting

§Failure Modes

  • Stale reference: Command holds reference to deleted target
    • Mitigation: Validate target existence in execute/undo
  • State drift: External changes invalidate undo data
    • Mitigation: Clear undo stack on external modifications
  • Memory exhaustion: Unbounded history growth
    • Mitigation: History stack enforces size limits via size_bytes()

Structs§

CommandBatch
A batch of commands that execute and undo together.
CommandMetadata
Metadata attached to every command for tracing and UI display.
MergeConfig
Configuration for command merging behavior.
TextDeleteCmd
Command to delete text at a position.
TextInsertCmd
Command to insert text at a position.
TextReplaceCmd
Command to replace text at a position.
WidgetId
Unique identifier for a widget that commands operate on.

Enums§

CommandError
Errors that can occur during command execution.
CommandSource
Source of a command - who/what triggered it.

Traits§

UndoableCmd
A reversible command that can be undone and redone.

Type Aliases§

CommandResult
Result of command execution or undo.
TextApplyFn
Callback type for applying text operations.
TextRemoveFn
Callback type for removing text.
TextReplaceFn
Callback type for replacing text.