Make input and output stream configuration public

This commit is contained in:
bakaq
2025-01-31 07:23:39 -03:00
parent 0a2457943e
commit 5386c183d2

View File

@@ -13,7 +13,7 @@ use super::{
}; };
#[derive(Default)] #[derive(Default)]
enum OutputStreamConfig { enum OutputStreamConfigInner {
#[default] #[default]
Null, Null,
Memory, Memory,
@@ -22,7 +22,7 @@ enum OutputStreamConfig {
Callback(Callback), Callback(Callback),
} }
impl std::fmt::Debug for OutputStreamConfig { impl std::fmt::Debug for OutputStreamConfigInner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Self::Null => write!(f, "Null"), Self::Null => write!(f, "Null"),
@@ -34,41 +34,110 @@ impl std::fmt::Debug for OutputStreamConfig {
} }
} }
/// Configuration for an output stream.
#[derive(Debug, Default)]
pub struct OutputStreamConfig {
inner: OutputStreamConfigInner,
}
impl 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 {
inner: OutputStreamConfigInner::Callback(callback),
}
}
fn into_stream(self, arena: &mut Arena) -> Stream { fn into_stream(self, arena: &mut Arena) -> Stream {
match self { match self.inner {
OutputStreamConfig::Null => Stream::Null(StreamOptions::default()), OutputStreamConfigInner::Null => Stream::Null(StreamOptions::default()),
OutputStreamConfig::Memory => Stream::from_owned_string("".to_owned(), arena), OutputStreamConfigInner::Memory => Stream::from_owned_string("".to_owned(), arena),
OutputStreamConfig::Stdout => Stream::stdout(arena), OutputStreamConfigInner::Stdout => Stream::stdout(arena),
OutputStreamConfig::Stderr => Stream::stderr(arena), OutputStreamConfigInner::Stderr => Stream::stderr(arena),
OutputStreamConfig::Callback(callback) => Stream::from_callback(callback, arena), OutputStreamConfigInner::Callback(callback) => Stream::from_callback(callback, arena),
} }
} }
} }
#[derive(Debug, Default)] #[derive(Debug, Default)]
enum InputStreamConfig { enum InputStreamConfigInner {
#[default] #[default]
Null, Null,
Stdin, Stdin,
Channel(Receiver<Vec<u8>>), Channel(Receiver<Vec<u8>>),
} }
/// Configuration for an input stream;
#[derive(Debug, Default)]
pub struct InputStreamConfig {
inner: InputStreamConfigInner,
}
impl InputStreamConfig { impl InputStreamConfig {
/// Ignores all input.
pub fn null() -> Self {
Self {
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();
(
UserInput { inner: sender },
Self {
inner: InputStreamConfigInner::Channel(receiver),
},
)
}
fn into_stream(self, arena: &mut Arena, add_history: bool) -> Stream { fn into_stream(self, arena: &mut Arena, add_history: bool) -> Stream {
match self { match self.inner {
InputStreamConfig::Null => Stream::Null(StreamOptions::default()), InputStreamConfigInner::Null => Stream::Null(StreamOptions::default()),
InputStreamConfig::Stdin => Stream::stdin(arena, add_history), InputStreamConfigInner::Stdin => Stream::stdin(arena, add_history),
InputStreamConfig::Channel(channel) => Stream::input_channel(channel, arena), InputStreamConfigInner::Channel(channel) => Stream::input_channel(channel, arena),
} }
} }
} }
/// Describes how the streams of a [`Machine`](crate::Machine) will be handled. /// Describes how the streams of a [`Machine`](crate::Machine) will be handled.
pub struct StreamConfig { pub struct StreamConfig {
stdin: InputStreamConfig, /// The configuration for the stdin of the [`Machine`](crate::Machine).
stdout: OutputStreamConfig, pub stdin: InputStreamConfig,
stderr: OutputStreamConfig, /// The configuration for the stdout of the [`Machine`](crate::Machine).
pub stdout: OutputStreamConfig,
/// The configuration for the stderr of the [`Machine`](crate::Machine).
pub stderr: OutputStreamConfig,
} }
impl Default for StreamConfig { impl Default for StreamConfig {
@@ -81,9 +150,9 @@ impl StreamConfig {
/// Binds the input, output and error streams to stdin, stdout and stderr. /// Binds the input, output and error streams to stdin, stdout and stderr.
pub fn stdio() -> Self { pub fn stdio() -> Self {
StreamConfig { StreamConfig {
stdin: InputStreamConfig::Stdin, stdin: InputStreamConfig::stdin(),
stdout: OutputStreamConfig::Stdout, stdout: OutputStreamConfig::stdout(),
stderr: OutputStreamConfig::Stderr, stderr: OutputStreamConfig::stderr(),
} }
} }
@@ -92,9 +161,9 @@ impl StreamConfig {
/// The input stream is ignored. /// The input stream is ignored.
pub fn in_memory() -> Self { pub fn in_memory() -> Self {
StreamConfig { StreamConfig {
stdin: InputStreamConfig::Null, stdin: InputStreamConfig::null(),
stdout: OutputStreamConfig::Memory, stdout: OutputStreamConfig::memory(),
stderr: OutputStreamConfig::Stderr, stderr: OutputStreamConfig::stderr(),
} }
} }
@@ -102,17 +171,13 @@ impl StreamConfig {
/// ///
/// This also returns a handler to the stdin do the [`Machine`](crate::Machine). /// This also returns a handler to the stdin do the [`Machine`](crate::Machine).
pub fn with_callbacks(stdout: Option<Callback>, stderr: Option<Callback>) -> (UserInput, Self) { pub fn with_callbacks(stdout: Option<Callback>, stderr: Option<Callback>) -> (UserInput, Self) {
let (sender, receiver) = channel(); let (user_input, channel_stream) = InputStreamConfig::channel();
( (
UserInput { inner: sender }, user_input,
StreamConfig { StreamConfig {
stdin: InputStreamConfig::Channel(receiver), stdin: channel_stream,
stdout: stdout.map_or(OutputStreamConfig::Null, |x| { stdout: stdout.map_or_else(OutputStreamConfig::null, OutputStreamConfig::callback),
OutputStreamConfig::Callback(x) stderr: stderr.map_or_else(OutputStreamConfig::null, OutputStreamConfig::callback),
}),
stderr: stderr.map_or(OutputStreamConfig::Null, |x| {
OutputStreamConfig::Callback(x)
}),
}, },
) )
} }