aboutsummaryrefslogtreecommitdiffstats
path: root/src/repl.rs
blob: 3901e4c139bd30ad9f75a6239a1db8ccb04911c6 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use futures::future::{Future, IntoFuture};
use futures::stream::Stream;
use snafu::{ResultExt, Snafu};
use std::io::Write;

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

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

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

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

pub fn repl() {
    let loop_stream = futures::stream::unfold(false, |done| {
        if done {
            return None;
        }

        let repl = read().and_then(|line| {
            eprint!("running '{}'\r\n", line);
            eval(&line).fold(None, |acc, event| match event {
                crate::eval::CommandEvent::Output(out) => match print(&out) {
                    Ok(()) => futures::future::ok(acc),
                    Err(e) => futures::future::err(e),
                },
                crate::eval::CommandEvent::ProcessExit(status) => {
                    futures::future::ok(Some(format!("{}", status)))
                }
                crate::eval::CommandEvent::BuiltinExit => {
                    futures::future::ok(Some(format!("success")))
                }
            })
        });

        Some(repl.then(move |res| match res {
            Ok(Some(status)) => {
                eprint!("command exited: {}\r\n", status);
                return Ok((done, false));
            }
            Ok(None) => {
                eprint!("command exited weirdly?\r\n");
                return Ok((done, false));
            }
            Err(Error::ReadError {
                source: crate::readline::Error::EOF,
            }) => {
                return Ok((done, true));
            }
            Err(Error::EvalError {
                source:
                    crate::eval::Error::ParserError {
                        source: crate::parser::Error::CommandRequired,
                        line: _,
                    },
            }) => {
                return Ok((done, false));
            }
            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();
                return Ok((done, false));
            }
        }))
    });
    tokio::run(loop_stream.collect().map(|_| ()));
}

fn read() -> impl futures::future::Future<Item = String, Error = Error> {
    crate::readline::readline("$ ", true)
        .into_future()
        .flatten()
        .map_err(|e| Error::ReadError { source: e })
}

fn eval(
    line: &str,
) -> impl futures::stream::Stream<Item = crate::eval::CommandEvent, Error = Error>
{
    crate::eval::eval(line)
        .into_future()
        .flatten_stream()
        .map_err(|e| Error::EvalError { source: e })
}

fn print(out: &[u8]) -> Result<()> {
    let stdout = std::io::stdout();
    let mut stdout = stdout.lock();
    stdout.write(out).context(PrintError)?;
    stdout.flush().context(PrintError)
}