Skip to content

Commit 770aa46

Browse files
committed
Command: add documentation
1 parent 09820c3 commit 770aa46

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

behavioral/command/README.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
11
# Command
22

3-
Command is behavioral design pattern that converts requests or simple operations into objects.
3+
**Command** is behavioral design pattern that converts requests or
4+
simple operations into objects.
5+
6+
A specific thing about Command Pattern implementation in 🦀 Rust is that
7+
a command instance should NOT hold a permanent reference to global context,
8+
instead the latter should be passed from top to down as a mutable parameter
9+
of the "`execute`" method:
10+
11+
```rust
12+
fn execute(&mut self, app: &mut cursive::Cursive) -> bool;
13+
```
414

515
## Text Editor: Commands and Undo
616

17+
How to launch:
18+
719
```bash
8-
cargo run
20+
cargo run --bin command
921
```
1022

23+
Key points:
24+
25+
- Each button runs a separate command.
26+
- Because a command is represented as an object, it can be pushed into a
27+
`history` array in order to be undone later.
28+
- TUI is created with `cursive` crate.
29+
1130
![Text Editor screenshot](res/editor.png)
31+
32+
## Notes
33+
34+
This example is inspired by [Command in Java (Example)](https://refactoring.guru/design-patterns/command/java/example).

behavioral/command/command.rs

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ pub use copy::CopyCommand;
66
pub use cut::CutCommand;
77
pub use paste::PasteCommand;
88

9+
/// Declares a method for executing (and undoing) a command.
10+
///
11+
/// Each command receives an application context to access
12+
/// visual components (e.g. edit view) and a clipboard.
913
pub trait Command {
1014
fn execute(&mut self, app: &mut cursive::Cursive) -> bool;
1115
fn undo(&mut self, app: &mut cursive::Cursive);

behavioral/command/main.rs

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use cursive::{
88

99
use command::{Command, CopyCommand, CutCommand, PasteCommand};
1010

11+
/// An application context to be passed into visual component callbacks.
12+
/// It contains a clipboard and a history of commands to be undone.
1113
#[derive(Default)]
1214
struct AppContext {
1315
clipboard: String,
@@ -31,6 +33,7 @@ fn main() {
3133
app.run();
3234
}
3335

36+
/// Executes a command and then pushes it to a history array.
3437
fn execute(app: &mut Cursive, mut command: impl Command + 'static) {
3538
if command.execute(app) {
3639
app.with_user_data(|context: &mut AppContext| {
@@ -39,6 +42,7 @@ fn execute(app: &mut Cursive, mut command: impl Command + 'static) {
3942
}
4043
}
4144

45+
/// Pops the last command and executes an undo action.
4246
fn undo(app: &mut Cursive) {
4347
let mut context = app.take_user_data::<AppContext>().unwrap();
4448
if let Some(mut command) = context.history.pop() {

0 commit comments

Comments
 (0)