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
use std::io::Write;
use crate::error::Error;
use crate::GuiCommand;
#[derive(Debug)]
pub struct GuiCommandWriter<W: Write> {
writer: W,
}
impl<W: Write> GuiCommandWriter<W> {
pub fn new(writer: W) -> GuiCommandWriter<W> {
GuiCommandWriter { writer }
}
pub fn send(&mut self, command: &GuiCommand) -> Result<(), Error> {
let s = format!("{}\n", command);
self.writer.write_all(s.as_bytes())?;
self.writer.flush()?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let mut buf: Vec<u8> = Vec::new();
let mut writer = GuiCommandWriter::new(&mut buf);
writer
.send(&GuiCommand::Usi)
.expect("failed to write to the buffer");
writer
.send(&GuiCommand::IsReady)
.expect("failed to write to the buffer");
writer
.send(&GuiCommand::SetOption(
"key".to_string(),
Some("val".to_string()),
))
.expect("failed to write to the buffer");
assert_eq!(
"usi\nisready\nsetoption name key value val\n",
std::str::from_utf8(&buf).unwrap()
);
}
}