operator based command spawning and direction in rust (wip)
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
#![allow(non_camel_case_types)]

use std::io::Write;

use crate::{processor::Io, Process};

impl std::fmt::Debug for echo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "echo {}",
            String::from_utf8_lossy(self.what).escape_debug()
        )
    }
}
pub struct echo {
    what: &'static [u8],
    written: usize,
}

unsafe impl Process for echo {
    fn run(&mut self, on: Io) -> anyhow::Result<usize> {
        match on {
            Io::First(mut w) => {
                let writ = w.write(&self.what[..self.written])?;
                self.written += writ;
                Ok(writ)
            }
            Io::Middle(..) | Io::Last(_) => unreachable!(),
        }
    }

    fn done(&mut self, _: Option<bool>) -> anyhow::Result<bool> {
        Ok(self.written == self.what.len())
    }
}