-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrepl-client.rs
55 lines (44 loc) · 1.33 KB
/
repl-client.rs
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
//! An example client program for the `repl` example. See the `repl` example
//! for details.
use io_streams::BufReaderLineWriter;
use nameless::InteractiveTextStream;
use std::io::{BufRead, Read, Write};
use std::str;
const PROMPT: &str = "prompt> \u{34f}";
#[kommand::main]
fn main(io: InteractiveTextStream) -> anyhow::Result<()> {
let mut io = BufReaderLineWriter::new(io);
let mut v = [0_u8; PROMPT.len()];
let mut s = String::new();
// Read the "prompt> ".
io.read_exact(&mut v)?;
if str::from_utf8(&v).unwrap() != PROMPT {
panic!("missed prompt");
}
// Write "hello".
writeln!(io, "hello")?;
io.read_line(&mut s)?;
if s != "[received \"hello\"]\n" {
panic!("missed response: '{}'", s);
}
// Read another "prompt> ".
io.read_exact(&mut v)?;
if str::from_utf8(&v).unwrap() != PROMPT {
panic!("missed second prompt: {:?}", String::from_utf8_lossy(&v));
}
// Write "world".
writeln!(io, "world")?;
s.clear();
io.read_line(&mut s)?;
if s != "[received \"world\"]\n" {
panic!("missed response: '{}'", s);
}
// Read one more "prompt> ".
io.read_exact(&mut v)?;
if str::from_utf8(&v).unwrap() != PROMPT {
panic!("missed last prompt");
}
// Walk away! `repl` is cool with this.
drop(io);
Ok(())
}