ListStream touchup (#12524)

# Description

Does some misc changes to `ListStream`:
- Moves it into its own module/file separate from `RawStream`.
- `ListStream`s now have an associated `Span`.
- This required changes to `ListStreamInfo` in `nu-plugin`. Note sure if
this is a breaking change for the plugin protocol.
- Hides the internals of `ListStream` but also adds a few more methods.
- This includes two functions to more easily alter a stream (these take
a `ListStream` and return a `ListStream` instead of having to go through
the whole `into_pipeline_data(..)` route).
  -  `map`: takes a `FnMut(Value) -> Value`
  - `modify`: takes a function to modify the inner stream.
This commit is contained in:
Ian Manske
2024-05-05 16:00:59 +00:00
committed by GitHub
parent 3143ded374
commit e879d4ecaf
106 changed files with 957 additions and 874 deletions

View File

@ -11,8 +11,8 @@ use nu_plugin_protocol::{
ProtocolInfo,
};
use nu_protocol::{
engine::Closure, Config, IntoInterruptiblePipelineData, LabeledError, ListStream, PipelineData,
PluginSignature, ShellError, Span, Spanned, Value,
engine::Closure, Config, LabeledError, PipelineData, PluginSignature, ShellError, Span,
Spanned, Value,
};
use std::{
collections::{btree_map, BTreeMap, HashMap},
@ -336,14 +336,15 @@ impl InterfaceManager for EngineInterfaceManager {
PluginCustomValue::deserialize_custom_values_in(value)?;
Ok(data)
}
PipelineData::ListStream(ListStream { stream, ctrlc, .. }, meta) => Ok(stream
.map(|mut value| {
PipelineData::ListStream(stream, meta) => {
let stream = stream.map(|mut value| {
let span = value.span();
PluginCustomValue::deserialize_custom_values_in(&mut value)
.map(|()| value)
.unwrap_or_else(|err| Value::error(err, span))
})
.into_pipeline_data_with_metadata(meta, ctrlc)),
});
Ok(PipelineData::ListStream(stream, meta))
}
PipelineData::Empty | PipelineData::ExternalStream { .. } => Ok(data),
}
}
@ -910,14 +911,15 @@ impl Interface for EngineInterface {
PluginCustomValue::serialize_custom_values_in(value)?;
Ok(data)
}
PipelineData::ListStream(ListStream { stream, ctrlc, .. }, meta) => Ok(stream
.map(|mut value| {
PipelineData::ListStream(stream, meta) => {
let stream = stream.map(|mut value| {
let span = value.span();
PluginCustomValue::serialize_custom_values_in(&mut value)
.map(|_| value)
.unwrap_or_else(|err| Value::error(err, span))
})
.into_pipeline_data_with_metadata(meta, ctrlc)),
});
Ok(PipelineData::ListStream(stream, meta))
}
PipelineData::Empty | PipelineData::ExternalStream { .. } => Ok(data),
}
}

View File

@ -56,7 +56,10 @@ fn manager_consume_all_exits_after_streams_and_interfaces_are_dropped() -> Resul
// Create a stream...
let stream = manager.read_pipeline_data(
PipelineDataHeader::ListStream(ListStreamInfo { id: 0 }),
PipelineDataHeader::ListStream(ListStreamInfo {
id: 0,
span: Span::test_data(),
}),
None,
)?;
@ -109,7 +112,10 @@ fn manager_consume_all_propagates_io_error_to_readers() -> Result<(), ShellError
test.set_read_error(test_io_error());
let stream = manager.read_pipeline_data(
PipelineDataHeader::ListStream(ListStreamInfo { id: 0 }),
PipelineDataHeader::ListStream(ListStreamInfo {
id: 0,
span: Span::test_data(),
}),
None,
)?;
@ -395,7 +401,10 @@ fn manager_consume_call_run_forwards_to_receiver_with_pipeline_data() -> Result<
positional: vec![],
named: vec![],
},
input: PipelineDataHeader::ListStream(ListStreamInfo { id: 6 }),
input: PipelineDataHeader::ListStream(ListStreamInfo {
id: 6,
span: Span::test_data(),
}),
}),
))?;
@ -534,7 +543,10 @@ fn manager_consume_engine_call_response_forwards_to_subscriber_with_pipeline_dat
manager.consume(PluginInput::EngineCallResponse(
0,
EngineCallResponse::PipelineData(PipelineDataHeader::ListStream(ListStreamInfo { id: 0 })),
EngineCallResponse::PipelineData(PipelineDataHeader::ListStream(ListStreamInfo {
id: 0,
span: Span::test_data(),
})),
))?;
for i in 0..2 {
@ -590,7 +602,7 @@ fn manager_prepare_pipeline_data_deserializes_custom_values_in_streams() -> Resu
[Value::test_custom_value(Box::new(
test_plugin_custom_value(),
))]
.into_pipeline_data(None),
.into_pipeline_data(Span::test_data(), None),
)?;
let value = data
@ -621,7 +633,8 @@ fn manager_prepare_pipeline_data_embeds_deserialization_errors_in_streams() -> R
let span = Span::new(20, 30);
let data = manager.prepare_pipeline_data(
[Value::custom(Box::new(invalid_custom_value), span)].into_pipeline_data(None),
[Value::custom(Box::new(invalid_custom_value), span)]
.into_pipeline_data(Span::test_data(), None),
)?;
let value = data
@ -703,7 +716,8 @@ fn interface_write_response_with_stream() -> Result<(), ShellError> {
interface
.write_response(Ok::<_, ShellError>(
[Value::test_int(3), Value::test_int(4), Value::test_int(5)].into_pipeline_data(None),
[Value::test_int(3), Value::test_int(4), Value::test_int(5)]
.into_pipeline_data(Span::test_data(), None),
))?
.write()?;
@ -1105,7 +1119,7 @@ fn interface_prepare_pipeline_data_serializes_custom_values_in_streams() -> Resu
[Value::test_custom_value(Box::new(
expected_test_custom_value(),
))]
.into_pipeline_data(None),
.into_pipeline_data(Span::test_data(), None),
&(),
)?;
@ -1163,7 +1177,8 @@ fn interface_prepare_pipeline_data_embeds_serialization_errors_in_streams() -> R
let span = Span::new(40, 60);
let data = interface.prepare_pipeline_data(
[Value::custom(Box::new(CantSerialize::BadVariant), span)].into_pipeline_data(None),
[Value::custom(Box::new(CantSerialize::BadVariant), span)]
.into_pipeline_data(Span::test_data(), None),
&(),
)?;