blob: c44174ac0f55e1a8423c705ea63a01830a00ac99 (
plain) (
blame)
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
|
use futures::future::Future;
use std::io::Write;
#[derive(Debug)]
enum Error {
ReadError(crate::readline::Error),
// EvalError(std::io::Error),
PrintError(std::io::Error),
// LoopError,
}
pub fn repl() {
tokio::run(tokio::prelude::future::lazy(|| {
let mut done = false;
while !done {
let res = read()
.and_then(move |line| eval(&line))
.and_then(move |out| print(&out))
.wait();
match res {
Ok(_) => {}
Err(Error::ReadError(crate::readline::Error::EOF)) => {
done = true;
}
Err(e) => {
let stderr = std::io::stderr();
let mut stderr = stderr.lock();
write!(stderr, "error: {:?}", e).unwrap();
stderr.flush().unwrap();
done = true;
}
}
}
futures::future::ok(())
}));
}
fn read() -> impl futures::future::Future<Item = String, Error = Error> {
crate::readline::readline("$ ", true).map_err(|e| Error::ReadError(e))
}
fn eval(line: &str) -> Result<String, Error> {
Ok(format!("got line '{}'\r\n", line))
}
fn print(out: &str) -> Result<(), Error> {
let stdout = std::io::stdout();
let mut stdout = stdout.lock();
stdout
.write(out.as_bytes())
.map_err(|e| Error::PrintError(e))?;
stdout.flush().map_err(|e| Error::PrintError(e))
}
|