From ad211d5e0557efeaf1a9784213377b43fcc017a7 Mon Sep 17 00:00:00 2001 From: bakaq Date: Fri, 31 Jan 2025 07:45:13 -0300 Subject: [PATCH] Disallow null streams in output --- src/machine/config.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/machine/config.rs b/src/machine/config.rs index 395dbb2f..2e71ae26 100644 --- a/src/machine/config.rs +++ b/src/machine/config.rs @@ -15,7 +15,6 @@ use super::{ #[derive(Default)] enum OutputStreamConfigInner { #[default] - Null, Memory, Stdout, Stderr, @@ -25,7 +24,6 @@ enum OutputStreamConfigInner { impl std::fmt::Debug for OutputStreamConfigInner { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::Null => write!(f, "Null"), Self::Memory => write!(f, "Memory"), Self::Stdout => write!(f, "Stdout"), Self::Stderr => write!(f, "Stderr"), @@ -41,30 +39,27 @@ pub struct OutputStreamConfig { } impl OutputStreamConfig { - /// Ignores all output. - pub fn null() -> Self { - Self { - inner: OutputStreamConfigInner::Null, - } - } /// Sends output to stdout. pub fn stdout() -> Self { Self { inner: OutputStreamConfigInner::Stdout, } } + /// Sends output to stderr. pub fn stderr() -> Self { Self { inner: OutputStreamConfigInner::Stderr, } } + /// Keeps output in a memory buffer. pub fn memory() -> Self { Self { inner: OutputStreamConfigInner::Memory, } } + /// Calls a callback with the output whenever the stream is written to. pub fn callback(callback: Callback) -> Self { Self { @@ -74,7 +69,6 @@ impl OutputStreamConfig { fn into_stream(self, arena: &mut Arena) -> Stream { match self.inner { - OutputStreamConfigInner::Null => Stream::Null(StreamOptions::default()), OutputStreamConfigInner::Memory => Stream::from_owned_string("".to_owned(), arena), OutputStreamConfigInner::Stdout => Stream::stdout(arena), OutputStreamConfigInner::Stderr => Stream::stderr(arena), @@ -104,12 +98,14 @@ impl InputStreamConfig { inner: InputStreamConfigInner::Null, } } + /// Gets input from stdin. pub fn stdin() -> Self { Self { inner: InputStreamConfigInner::Stdin, } } + /// Connects the input to the receiving end of a channel. pub fn channel() -> (UserInput, Self) { let (sender, receiver) = channel(); @@ -176,8 +172,10 @@ impl StreamConfig { user_input, StreamConfig { stdin: channel_stream, - stdout: stdout.map_or_else(OutputStreamConfig::null, OutputStreamConfig::callback), - stderr: stderr.map_or_else(OutputStreamConfig::null, OutputStreamConfig::callback), + stdout: stdout + .map_or_else(OutputStreamConfig::memory, OutputStreamConfig::callback), + stderr: stderr + .map_or_else(OutputStreamConfig::memory, OutputStreamConfig::callback), }, ) } -- 2.54.0