1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//! Contains the CommandNoParameters and CommandTwoParameters implementations.

use super::command_icommand_trait::ICommand;
use super::CommandTextObject;

//-----------------------------------------------------------------------------

/// Alias for a function that takes no parameters other than a CommandTextObject.
type NoParameterOperation = fn(source: &mut CommandTextObject);

/// Alias for a function that takes two string parameters along with a
/// CommandTextObject.
type TwoParametersOperation = fn(source: &mut CommandTextObject, arg1:&str, arg2:&str);

//-----------------------------------------------------------------------------

/// Represents an operation that can be applied to a CommandTextObject with
/// no additional parameters.
///
/// This struct implements the ICommand trait so it can be treated the same as
/// any other kind of command.
pub struct CommandNoParameters {
    /// Easy-to-read command name.
    name: String,
    /// No parameter operation to apply to a CommandTextObject.
    operation: NoParameterOperation,
}

impl CommandNoParameters {
    /// Constructor for a command that applies an operation to a
    /// CommandTextObject but does not require any additional parameters.
    ///
    /// # Parameters
    /// - name
    ///
    ///   Easy-to-read name of the command.
    /// - operation
    ///
    ///   The operation to apply to a CommandTextObject and that takes no
    ///   parameters.
    ///
    /// # Returns
    /// Returns an ICommand object representing the command.
    pub fn new(name: &str, operation: NoParameterOperation) -> Box<dyn ICommand> {
        Box::new(CommandNoParameters{
            name: name.to_string(),
            operation: operation,
        })
    }
}

impl ICommand for CommandNoParameters {
    fn execute(&self, receiver: &mut CommandTextObject) {
        (self.operation)(receiver);
    }
    fn to_string(&self) -> String {
        self.name.clone()
    }
}

//=============================================================================
//=============================================================================


/// Represents an operation that can be applied to a CommandTextObject that
/// requires two additional parameters.
///
/// This struct implements the ICommand trait so it can be treated the same as
/// any other kind of command.
pub struct CommandTwoParameters {
    /// Easy-to-read command name.
    name: String,
    /// Two parameter operation to apply to a CommandTextObject.
    operation: TwoParametersOperation,
    /// The first argument to the operation.
    arg1: String,
    /// The second argument to the operation.
    arg2: String,
}

impl CommandTwoParameters {
    /// Constructor for a command that applies an operation to a
    /// CommandTextObject, where the operation takes two parameters.
    ///
    /// # Parameters
    /// - name
    ///
    ///   Easy-to-read name of the command.
    /// - operation
    ///
    ///   The operation to apply to a CommandTextObject and that takes two
    ///   parameters.
    /// - arg1
    ///
    ///   First argument to the operation (after the CommandTextObject).
    /// - arg2
    ///
    ///   Second argument to the operation (after the CommandTextObject).
    /// # Returns
    /// Returns an ICommand object representing the command.
    pub fn new(name: &str, operation: TwoParametersOperation, arg1: &str, arg2: &str) -> Box<dyn ICommand> {
        Box::new(CommandTwoParameters{
            name: name.to_string(),
            operation: operation,
            arg1: arg1.to_string(),
            arg2: arg2.to_string(),
        })
    }
}

impl ICommand for CommandTwoParameters {
    fn execute(&self, receiver: &mut CommandTextObject) {
        (self.operation)(receiver, &self.arg1, &self.arg2);
    }

    fn to_string(&self) -> String {
        format!("{0} \"{1}\" with \"{2}\"", self.name, self.arg1, self.arg2)
    }
}