vce-engine 0.1.1

The generic state substrate of the Virtual Computing Engine (VCE)
Documentation
//! Generic input/output primitives.
//!
//! The Engine provides the *mechanisms* for streams, pipes, and interactive
//! screens; it does not assign them operating-system meaning. The Shell
//! layer routes command stdin/stdout/stderr through these primitives, the OS
//! commands consume and produce them, and the Terminal layer plugs its
//! screen in as an output.
//!
//! The [`Terminal`] interface is an Engine primitive for the same reason
//! [`Input`] and [`Output`] are: a rectangular screen, a cursor, and a key
//! stream are generic abstractions usable by editors, games, and any other
//! stateful application — not operating-system concepts. Concrete rendering
//! (ANSI parsing, screen buffers) lives above the Engine.
//!
//! # Notes
//!
//! - [`Pipe`] is an in-memory, effectively unbounded channel. It exists to
//!   coordinate *sequential* virtual command execution (producer finishes,
//!   then consumer reads); it is not a host OS pipe and does not claim host
//!   concurrency semantics. The Shell runtime layer decides how commands are
//!   scheduled.
//! - Streams are `Send + Sync` so the Shell may move them between command
//!   invocations.

mod input;
mod output;
mod pipe;
mod terminal;

pub use input::{BytesInput, EmptyInput, Input, InputError, StringInput};
pub use output::{BytesOutput, Output, OutputError, SinkOutput};
pub use pipe::Pipe;
pub use terminal::{InputMode, Key, KeyEvent, Size, Terminal};

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn bytes_round_trip_through_a_pipe() {
        let pipe = Pipe::new();
        let mut input = pipe.input();
        let mut output = pipe.output();
        output.write(b"hello ").unwrap();
        output.write(b"world").unwrap();
        drop(output);
        let mut bytes = Vec::new();
        input.read_to_end(&mut bytes).unwrap();
        assert_eq!(bytes, b"hello world");
    }

    #[test]
    fn empty_input_reports_eof() {
        let mut input = EmptyInput::new();
        let mut bytes = Vec::new();
        input.read_to_end(&mut bytes).unwrap();
        assert!(bytes.is_empty());
    }

    #[test]
    fn bytes_input_replays_its_data() {
        let mut input = BytesInput::new(b"line one\nline two\n");
        let mut line = String::new();
        input.read_line(&mut line).unwrap();
        assert_eq!(line, "line one\n");
    }

    #[test]
    fn bytes_output_accumulates() {
        let mut output = BytesOutput::new();
        output.write(b"a").unwrap();
        output.write(b"b").unwrap();
        assert_eq!(output.into_bytes(), b"ab");
    }

    #[test]
    fn sink_output_discards() {
        let mut output = SinkOutput::new();
        output.write(b"anything").unwrap();
    }
}