Add builder style configuration of user input, output and error
This commit is contained in:
@@ -132,12 +132,9 @@ impl InputStreamConfig {
|
|||||||
|
|
||||||
/// 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 {
|
||||||
/// The configuration for the stdin of the [`Machine`](crate::Machine).
|
user_input: InputStreamConfig,
|
||||||
pub stdin: InputStreamConfig,
|
user_output: OutputStreamConfig,
|
||||||
/// The configuration for the stdout of the [`Machine`](crate::Machine).
|
user_error: OutputStreamConfig,
|
||||||
pub stdout: OutputStreamConfig,
|
|
||||||
/// The configuration for the stderr of the [`Machine`](crate::Machine).
|
|
||||||
pub stderr: OutputStreamConfig,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for StreamConfig {
|
impl Default for StreamConfig {
|
||||||
@@ -150,43 +147,61 @@ 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(),
|
user_input: InputStreamConfig::stdin(),
|
||||||
stdout: OutputStreamConfig::stdout(),
|
user_output: OutputStreamConfig::stdout(),
|
||||||
stderr: OutputStreamConfig::stderr(),
|
user_error: OutputStreamConfig::stderr(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Binds the output and error streams to memory buffers and has an empty input.
|
/// Binds the output and error streams to memory buffers and has an empty input.
|
||||||
pub fn in_memory() -> Self {
|
pub fn in_memory() -> Self {
|
||||||
StreamConfig {
|
StreamConfig {
|
||||||
stdin: InputStreamConfig::string(""),
|
user_input: InputStreamConfig::string(""),
|
||||||
stdout: OutputStreamConfig::memory(),
|
user_output: OutputStreamConfig::memory(),
|
||||||
stderr: OutputStreamConfig::memory(),
|
user_error: OutputStreamConfig::memory(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calls the given callbacks when the respective streams are written to.
|
/// Calls the given callbacks when the respective streams are written to.
|
||||||
///
|
///
|
||||||
/// This also returns a handler to the stdin do the [`Machine`](crate::Machine).
|
/// This also returns a handler to the stdin of the [`Machine`](crate::Machine).
|
||||||
pub fn with_callbacks(stdout: Option<Callback>, stderr: Option<Callback>) -> (UserInput, Self) {
|
pub fn from_callbacks(stdout: Option<Callback>, stderr: Option<Callback>) -> (UserInput, Self) {
|
||||||
let (user_input, channel_stream) = InputStreamConfig::channel();
|
let (user_input, channel_stream) = InputStreamConfig::channel();
|
||||||
(
|
(
|
||||||
user_input,
|
user_input,
|
||||||
StreamConfig {
|
StreamConfig {
|
||||||
stdin: channel_stream,
|
user_input: channel_stream,
|
||||||
stdout: stdout
|
user_output: stdout
|
||||||
.map_or_else(OutputStreamConfig::memory, OutputStreamConfig::callback),
|
.map_or_else(OutputStreamConfig::memory, OutputStreamConfig::callback),
|
||||||
stderr: stderr
|
user_error: stderr
|
||||||
.map_or_else(OutputStreamConfig::memory, OutputStreamConfig::callback),
|
.map_or_else(OutputStreamConfig::memory, OutputStreamConfig::callback),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configures the `user_input` stream.
|
||||||
|
pub fn with_user_input(self, user_input: InputStreamConfig) -> Self {
|
||||||
|
Self { user_input, ..self }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Configures the `user_output` stream.
|
||||||
|
pub fn with_user_output(self, user_output: OutputStreamConfig) -> Self {
|
||||||
|
Self {
|
||||||
|
user_output,
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Configures the `user_error` stream.
|
||||||
|
pub fn with_user_error(self, user_error: OutputStreamConfig) -> Self {
|
||||||
|
Self { user_error, ..self }
|
||||||
|
}
|
||||||
|
|
||||||
fn into_streams(self, arena: &mut Arena, add_history: bool) -> (Stream, Stream, Stream) {
|
fn into_streams(self, arena: &mut Arena, add_history: bool) -> (Stream, Stream, Stream) {
|
||||||
(
|
(
|
||||||
self.stdin.into_stream(arena, add_history),
|
self.user_input.into_stream(arena, add_history),
|
||||||
self.stdout.into_stream(arena),
|
self.user_output.into_stream(arena),
|
||||||
self.stderr.into_stream(arena),
|
self.user_error.into_stream(arena),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2112,10 +2112,8 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
#[cfg_attr(miri, ignore)]
|
#[cfg_attr(miri, ignore)]
|
||||||
fn user_input_string_stream() {
|
fn user_input_string_stream() {
|
||||||
let streams = StreamConfig {
|
let streams =
|
||||||
stdin: InputStreamConfig::string("a(1,2,3)."),
|
StreamConfig::default().with_user_input(InputStreamConfig::string("a(1,2,3)."));
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut machine = MachineBuilder::default().with_streams(streams).build();
|
let mut machine = MachineBuilder::default().with_streams(streams).build();
|
||||||
|
|
||||||
@@ -2144,11 +2142,7 @@ mod tests {
|
|||||||
#[cfg_attr(miri, ignore)]
|
#[cfg_attr(miri, ignore)]
|
||||||
fn user_input_channel_stream() {
|
fn user_input_channel_stream() {
|
||||||
let (mut user_input, channel_stream) = InputStreamConfig::channel();
|
let (mut user_input, channel_stream) = InputStreamConfig::channel();
|
||||||
let streams = StreamConfig {
|
let streams = StreamConfig::default().with_user_input(channel_stream);
|
||||||
stdin: channel_stream,
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut machine = MachineBuilder::default().with_streams(streams).build();
|
let mut machine = MachineBuilder::default().with_streams(streams).build();
|
||||||
|
|
||||||
let complete_answer: Vec<_> = machine
|
let complete_answer: Vec<_> = machine
|
||||||
@@ -2204,15 +2198,13 @@ mod tests {
|
|||||||
fn user_output_callback_stream() {
|
fn user_output_callback_stream() {
|
||||||
let test_string = Rc::new(RefCell::new(String::new()));
|
let test_string = Rc::new(RefCell::new(String::new()));
|
||||||
|
|
||||||
let streams = StreamConfig {
|
let streams =
|
||||||
stdout: OutputStreamConfig::callback(Box::new({
|
StreamConfig::default().with_user_output(OutputStreamConfig::callback(Box::new({
|
||||||
let test_string = test_string.clone();
|
let test_string = test_string.clone();
|
||||||
move |x| {
|
move |x| {
|
||||||
x.read_to_string(&mut test_string.borrow_mut()).unwrap();
|
x.read_to_string(&mut test_string.borrow_mut()).unwrap();
|
||||||
}
|
}
|
||||||
})),
|
})));
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut machine = MachineBuilder::default().with_streams(streams).build();
|
let mut machine = MachineBuilder::default().with_streams(streams).build();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user