aboutsummaryrefslogtreecommitdiffstats
path: root/src/state.rs
blob: 0965aea7290cfe829d199a0e52ddc4ab124b042b (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use futures::future::Future as _;
use futures::sink::Sink as _;
use futures::stream::Stream as _;
use snafu::{OptionExt as _, ResultExt as _};
use std::io::Write as _;

#[derive(Debug, snafu::Snafu)]
pub enum Error {
    #[snafu(display("invalid command index: {}", idx))]
    InvalidCommandIndex { idx: usize },

    #[snafu(display("error sending message: {}", source))]
    Send {
        source: futures::sync::mpsc::SendError<StateEvent>,
    },

    #[snafu(display("error printing output: {}", source))]
    PrintOutput { source: std::io::Error },

    #[snafu(display("this error should not be possible"))]
    Unreachable,
}

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

#[derive(Debug)]
pub enum StateEvent {
    Start(usize, String, Vec<String>),
    Output(usize, Vec<u8>),
    Exit(usize, std::process::ExitStatus),
}

#[derive(Debug)]
pub struct State {
    r: futures::sync::mpsc::Receiver<StateEvent>,
    commands: std::collections::HashMap<usize, Command>,
}

impl State {
    pub fn new(r: futures::sync::mpsc::Receiver<StateEvent>) -> Self {
        Self {
            r,
            commands: std::collections::HashMap::new(),
        }
    }

    pub fn command_start(
        &mut self,
        idx: usize,
        cmd: &str,
        args: &[String],
    ) -> Result<()> {
        snafu::ensure!(
            !self.commands.contains_key(&idx),
            InvalidCommandIndex { idx }
        );
        let command = Command::new(cmd, args);
        self.commands.insert(idx, command.clone());
        eprint!("running '{} {:?}'\r\n", command.cmd, command.args);
        Ok(())
    }

    pub fn command_output(
        &mut self,
        idx: usize,
        output: &[u8],
    ) -> Result<()> {
        let command = self
            .commands
            .get_mut(&idx)
            .context(InvalidCommandIndex { idx })?;
        command.output.append(&mut output.to_vec());

        let stdout = std::io::stdout();
        let mut stdout = stdout.lock();
        stdout.write(output).context(PrintOutput)?;
        stdout.flush().context(PrintOutput)?;

        Ok(())
    }

    pub fn command_exit(
        &mut self,
        idx: usize,
        status: std::process::ExitStatus,
    ) -> Result<()> {
        let command = self
            .commands
            .get_mut(&idx)
            .context(InvalidCommandIndex { idx })?;
        command.status = Some(status);
        eprint!("command exited: {}\r\n", status);
        Ok(())
    }
}

impl futures::future::Future for State {
    type Item = ();
    type Error = Error;

    fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
        loop {
            let event = futures::try_ready!(self
                .r
                .poll()
                .map_err(|_| Error::Unreachable));
            match event {
                Some(StateEvent::Start(idx, cmd, args)) => {
                    self.command_start(idx, &cmd, &args)?;
                }
                Some(StateEvent::Output(idx, output)) => {
                    self.command_output(idx, &output)?;
                }
                Some(StateEvent::Exit(idx, status)) => {
                    self.command_exit(idx, status)?;
                }
                None => return Ok(futures::Async::Ready(())),
            }
        }
    }
}

pub fn update(
    w: futures::sync::mpsc::Sender<StateEvent>,
    idx: usize,
    event: &crate::eval::CommandEvent,
) -> impl futures::future::Future<Item = (), Error = Error> {
    match event {
        crate::eval::CommandEvent::CommandStart(cmd, args) => {
            w.send(crate::state::StateEvent::Start(
                idx,
                cmd.to_string(),
                args.to_vec(),
            ))
        }
        crate::eval::CommandEvent::Output(out) => {
            w.send(crate::state::StateEvent::Output(idx, out.to_vec()))
        }
        crate::eval::CommandEvent::CommandExit(status) => {
            w.send(crate::state::StateEvent::Exit(idx, *status))
        }
    }
    .map(|_| ())
    .map_err(|e| Error::Send { source: e })
}

#[derive(Debug, Clone)]
struct Command {
    cmd: String,
    args: Vec<String>,
    output: Vec<u8>,
    status: Option<std::process::ExitStatus>,
}

impl Command {
    fn new(cmd: &str, args: &[String]) -> Self {
        Self {
            cmd: cmd.to_string(),
            args: args.to_vec(),
            output: vec![],
            status: None,
        }
    }
}