aboutsummaryrefslogtreecommitdiffstats
path: root/src/repl.rs
blob: 3ec7084dc6b5a7122c38f1d88bc0c100add49f50 (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
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
use futures::future::Future as _;
use futures::stream::Stream as _;
use snafu::futures01::{FutureExt as _, StreamExt as _};
use snafu::ResultExt as _;
use std::io::Write as _;

#[derive(Debug, snafu::Snafu)]
enum Error {
    #[snafu(display("error during read: {}", source))]
    Read { source: crate::readline::Error },

    #[snafu(display("error during eval: {}", source))]
    Eval { source: crate::eval::Error },

    #[snafu(display("error during print: {}", source))]
    Print { source: std::io::Error },
}

type Result<T> = std::result::Result<T, Error>;

pub fn repl() {
    tokio::run(futures::future::loop_fn((), |_| {
        read()
            .and_then(|line| eval(&line).for_each(|event| print(&event)))
            .then(|res| match res {
                // successful run or empty input means prompt again
                Ok(_)
                | Err(Error::Eval {
                    source:
                        crate::eval::Error::Parser {
                            source: crate::parser::Error::CommandRequired,
                            ..
                        },
                }) => Ok(futures::future::Loop::Continue(())),
                // eof means we're done
                Err(Error::Read {
                    source: crate::readline::Error::EOF,
                }) => Ok(futures::future::Loop::Break(())),
                // any other errors should be displayed, then we
                // prompt again
                Err(e) => {
                    let stderr = std::io::stderr();
                    let mut stderr = stderr.lock();
                    // panics seem fine for errors during error handling
                    write!(stderr, "{}\r\n", e).unwrap();
                    stderr.flush().unwrap();
                    Ok(futures::future::Loop::Continue(()))
                }
            })
    }))
}

fn read() -> impl futures::future::Future<Item = String, Error = Error> {
    crate::readline::readline().context(Read)
}

fn eval(
    line: &str,
) -> impl futures::stream::Stream<
    Item = tokio_pty_process_stream::Event,
    Error = Error,
> {
    crate::eval::eval(line).context(Eval)
}

fn print(event: &tokio_pty_process_stream::Event) -> Result<()> {
    match event {
        tokio_pty_process_stream::Event::CommandStart { .. } => {}
        tokio_pty_process_stream::Event::Output { data: out } => {
            let stdout = std::io::stdout();
            let mut stdout = stdout.lock();
            stdout.write(out).context(Print)?;
            stdout.flush().context(Print)?;
        }
        tokio_pty_process_stream::Event::CommandExit { .. } => {}
        tokio_pty_process_stream::Event::Resize { .. } => {}
    }
    Ok(())
}