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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use std::io;
use std::collections::HashMap;

use crate::command_handler::CommandHandler;

/// Command interpreter implemented as struct that contains
/// boxed CommandHandlers in a hashmap with Strings as the keys
#[derive(Debug)]
pub struct Cmd<R: io::BufRead, W: io::Write>{
    handles: HashMap<String, Box<dyn CommandHandler<W>>>,
    stdin: R,
    stdout: W
}


impl<R: io::BufRead + 'static, W: io::Write + 'static> Cmd<R, W>{
    /// Create new Cmd instance
    pub fn new(reader: R, writer: W) -> Cmd<R, W>
    where
        W: io::Write,
        R: io::Read
    {
        Cmd {
            handles: HashMap::new(),
            stdin: reader,
            stdout: writer
        }
    }


    /// Start the command interpreter
    ///
    /// Handlers with return code 0 will break the loop
    pub fn run(&mut self) -> Result<(), io::Error>{
        loop {
            // print promt at every iteration and flush stdout to ensure user
            // can type on same line as promt
            self.stdout.write(b"(cmd) ")?;
            self.stdout.flush()?;

            // get user input from stdin
            let mut inputs = String::new();
            self.stdin.read_line(&mut inputs)?;
            let inputs = inputs.trim();

            // separate user input into a command and optional args
            if !inputs.is_empty() {
                let (command, args) = self.parse_cmd(inputs);

                // attempt to execute command
                if let Some(handler) = self.handles.get(&command) {
                    if let 0 = handler.execute(&mut self.stdout, args) { break; }
                } else {
                    self.stdout.write(format!("No command {command}\n").as_bytes())?;
                }
            }
        }
        Ok(())
    }


    /// Insert new command into the Cmd handles HashMap
    ///
    /// ## Note: Will not overwrite existing handler names
    pub fn add_cmd(&mut self, name: String, handler: Box<dyn CommandHandler<W>>) -> Result<(), io::Error> {
        match self.handles.get(&name) {
            Some(_) => { self.stdout.write(format!("Warning: Command with handle {name} already exists.").as_bytes())?; },
            None => { self.handles.insert(name, handler); }
        }

        Ok(())
    }


    // Parse command string into command, and args Strings
    fn parse_cmd(&self, line: &str) -> (String, String) {
        let mut words = line.split_whitespace();
        let command = words.next().unwrap_or_default().to_string();
        let args: String = words.collect::<Vec<_>>().join(" ");
        (command, args)
    }


    #[cfg(test)]
    fn get_cmd(&self, key: String) -> Option<&Box<dyn CommandHandler<W>>> {
        self.handles.get(&key)
    }
}


#[cfg(test)]
mod tests {
    use std::{any::Any, io::BufRead};
    use std::io::{self, BufReader, Write};

    use super::*;
    use crate::handlers::Quit;

    #[derive(Debug, Default)]
    pub struct Greeting { }

    impl<W: io::Write> CommandHandler<W> for Greeting {
        fn execute(&self, stdout: &mut W, _args: String) -> usize {
            write!(stdout, "Hello there!").unwrap();
            1
        }
    }

    // Mock object for stdin that always errs on stdin.read()
    struct StdinAlwaysErr;

    impl io::Read for StdinAlwaysErr {
        fn read(&mut self, _: &mut [u8]) -> Result<usize, std::io::Error> {
            Err(io::Error::new(io::ErrorKind::Other, "failed on read"))
        }
    }

    // Mock object for stdout that always errs on stdout.write()
    struct StdoutWriteErr;

    impl io::Write for StdoutWriteErr {
        fn write(&mut self, _: &[u8]) -> Result<usize, std::io::Error> {
            Err(io::Error::new(io::ErrorKind::Other, "failed on write"))
        }
        fn flush(&mut self) -> Result<(), std::io::Error> {
            Ok(())
        }
    }
    // Mock object for stdout that always errs on stdout.flush()
    struct StdoutFlushErr;

    impl io::Write for StdoutFlushErr {
        fn write(&mut self, _: &[u8]) -> Result<usize, std::io::Error> {
            Ok(0)
        }
        fn flush(&mut self) -> Result<(), std::io::Error> {
            Err(io::Error::new(io::ErrorKind::Other, "failed on flush"))
        }
    }

    fn setup() -> Cmd<io::BufReader<std::fs::File>, Vec<u8>> {
        let f = std::fs::File::open("test_files/test_in.txt").unwrap();
        let stdin = io::BufReader::new(f);

        let stdout: Vec<u8> = Vec::new();
        let mut app: Cmd<io::BufReader<std::fs::File>, Vec<u8>> = Cmd::new( stdin, stdout );
        let greet_handler = Greeting::default();

        // Add the trait object to the HashMap
        app.add_cmd(String::from("greet"), Box::new(greet_handler)).unwrap();
        app.add_cmd(String::from("quit"), Box::new(Quit::default())).unwrap();
        app

    }

    #[test]
    fn test_add_cmd() {
        let mut app = setup();

        let h = app.get_cmd(String::from("greet"));

        // Verify that the key-value pair exists in the HashMap
        assert!(h.is_some());

        // Verify the value can cast down to Greeting
        let it: &dyn Any = h.unwrap().as_any();
        assert!(!it.downcast_ref::<Greeting>().is_none());

        // Verify message is printed out when a handle with existing name is added
        app.add_cmd("greet".to_string(), Box::new(Greeting {} )).unwrap();

        let mut std_out_lines = app.stdout.lines();
        let line1 = std_out_lines.next().unwrap().unwrap();

        assert_eq!(line1, "Warning: Command with handle greet already exists.");
    }

    #[test]
    fn test_add_cmd_always_error() {
        let f = std::fs::File::open("test_files/test_in.txt").unwrap();
        let stdin = io::BufReader::new(f);
        let stdout = StdoutWriteErr;
        let mut app = Cmd::new( stdin, stdout );

        // add same command twice, which will cause the self.stdout.write() path to output error
        let _ok = app.add_cmd("greet".to_string(), Box::new(Greeting {} )).unwrap();
        let e = app.add_cmd("greet".to_string(), Box::new(Greeting {} )).unwrap_err();

        assert_eq!(e.to_string(), "failed on write");
        assert_eq!(e.kind(), io::ErrorKind::Other);
    }

    #[test]
    fn test_parse_cmd(){
        let app = setup();
        let line = "command arg1 arg2";
        assert_eq!(app.parse_cmd(line), ("command".to_string(), "arg1 arg2".to_string()))
    }

    #[test]
    fn test_run() {
        let mut app = setup();

        app.run().unwrap();

        let std_out_lines = app.stdout;
        let line1 = String::from_utf8(std_out_lines).unwrap();

        assert_eq!(line1, "(cmd) Hello there!(cmd) (cmd) No command non\n(cmd) ");
    }

    #[test]
    fn test_run_stdout_write_err() {
        let f = std::fs::File::open("test_files/test_in.txt").unwrap();
        let stdin = io::BufReader::new(f);
        let stdout = StdoutWriteErr;
        let mut app = Cmd::new( stdin, stdout );
        app.stdout.flush().unwrap();

        let e = app.run().unwrap_err();

        assert_eq!(e.kind(), io::ErrorKind::Other);
        assert_eq!(e.to_string(), "failed on write");
    }

    #[test]
    fn test_run_stdout_flush_err() {
        let f = std::fs::File::open("test_files/test_in.txt").unwrap();
        let stdin = io::BufReader::new(f);
        let stdout = StdoutFlushErr;
        let mut app = Cmd::new( stdin, stdout );
        app.stdout.write(b"hi").unwrap();

        let e = app.run().unwrap_err();

        assert_eq!(e.kind(), io::ErrorKind::Other);
        assert_eq!(e.to_string(), "failed on flush");
    }

    #[test]
    fn test_run_stdin_read_err() {
        let stdin = BufReader::new(StdinAlwaysErr);
        let stdout = io::stdout();
        let mut app = Cmd::new( stdin, stdout );

        let e = app.run().unwrap_err();

        assert_eq!(e.kind(), io::ErrorKind::Other);
        assert_eq!(e.to_string(), "failed on read");
    }

}