nu_protocol/pipeline/
list_stream.rs

1//! Module managing the streaming of individual [`Value`]s as a [`ListStream`] between pipeline
2//! elements
3//!
4//! For more general infos regarding our pipelining model refer to [`PipelineData`]
5use crate::{Config, PipelineData, ShellError, Signals, Span, Value};
6use std::fmt::Debug;
7
8pub type ValueIterator = Box<dyn Iterator<Item = Value> + Send + 'static>;
9
10/// A potentially infinite, interruptible stream of [`Value`]s.
11///
12/// In practice, a "stream" here means anything which can be iterated and produces Values.
13/// Like other iterators in Rust, observing values from this stream will drain the items
14/// as you view them and the stream cannot be replayed.
15pub struct ListStream {
16    stream: ValueIterator,
17    span: Span,
18    caller_spans: Vec<Span>,
19}
20
21impl ListStream {
22    /// Create a new [`ListStream`] from a [`Value`] `Iterator`.
23    pub fn new(
24        iter: impl Iterator<Item = Value> + Send + 'static,
25        span: Span,
26        signals: Signals,
27    ) -> Self {
28        Self {
29            stream: Box::new(InterruptIter::new(iter, signals)),
30            span,
31            caller_spans: vec![],
32        }
33    }
34
35    /// Returns the [`Span`] associated with this [`ListStream`].
36    pub fn span(&self) -> Span {
37        self.span
38    }
39
40    /// Push a caller [`Span`] to the bytestream, it's useful to construct a backtrace.
41    pub fn push_caller_span(&mut self, span: Span) {
42        if span != self.span {
43            self.caller_spans.push(span)
44        }
45    }
46
47    /// Get all caller [`Span`], it's useful to construct a backtrace.
48    pub fn get_caller_spans(&self) -> &Vec<Span> {
49        &self.caller_spans
50    }
51
52    /// Changes the [`Span`] associated with this [`ListStream`].
53    pub fn with_span(mut self, span: Span) -> Self {
54        self.span = span;
55        self
56    }
57
58    /// Convert a [`ListStream`] into its inner [`Value`] `Iterator`.
59    pub fn into_inner(self) -> ValueIterator {
60        self.stream
61    }
62
63    /// Take a single value from the inner `Iterator`, modifying the stream.
64    pub fn next_value(&mut self) -> Option<Value> {
65        self.stream.next()
66    }
67
68    /// Converts each value in a [`ListStream`] into a string and then joins the strings together
69    /// using the given separator.
70    pub fn into_string(self, separator: &str, config: &Config) -> String {
71        self.into_iter()
72            .map(|val| val.to_expanded_string(", ", config))
73            .collect::<Vec<String>>()
74            .join(separator)
75    }
76
77    /// Collect the values of a [`ListStream`] into a list [`Value`].
78    ///
79    /// If any of the values in the stream is a [Value::Error], its inner [ShellError] is returned.
80    pub fn into_value(self) -> Result<Value, ShellError> {
81        Ok(Value::list(
82            self.stream
83                .map(Value::unwrap_error)
84                .collect::<Result<_, _>>()?,
85            self.span,
86        ))
87    }
88
89    /// Collect the values of a [`ListStream`] into a [`Value::List`], preserving [Value::Error]
90    /// items for debugging purposes.
91    pub fn into_debug_value(self) -> Value {
92        Value::list(self.stream.collect(), self.span)
93    }
94
95    /// Consume all values in the stream, returning an error if any of the values is a `Value::Error`.
96    pub fn drain(self) -> Result<(), ShellError> {
97        for next in self {
98            if let Value::Error { error, .. } = next {
99                return Err(*error);
100            }
101        }
102        Ok(())
103    }
104
105    /// Modify the inner iterator of a [`ListStream`] using a function.
106    ///
107    /// This can be used to call any number of standard iterator functions on the [`ListStream`].
108    /// E.g., `take`, `filter`, `step_by`, and more.
109    ///
110    /// ```
111    /// use nu_protocol::{ListStream, Signals, Span, Value};
112    ///
113    /// let span = Span::unknown();
114    /// let stream = ListStream::new(std::iter::repeat(Value::int(0, span)), span, Signals::empty());
115    /// let new_stream = stream.modify(|iter| iter.take(100));
116    /// ```
117    pub fn modify<I>(self, f: impl FnOnce(ValueIterator) -> I) -> Self
118    where
119        I: Iterator<Item = Value> + Send + 'static,
120    {
121        Self {
122            stream: Box::new(f(self.stream)),
123            span: self.span,
124            caller_spans: self.caller_spans,
125        }
126    }
127
128    /// Create a new [`ListStream`] whose values are the results of applying the given function
129    /// to each of the values in the original [`ListStream`].
130    pub fn map(self, mapping: impl FnMut(Value) -> Value + Send + 'static) -> Self {
131        self.modify(|iter| iter.map(mapping))
132    }
133}
134
135impl Debug for ListStream {
136    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
137        f.debug_struct("ListStream").finish()
138    }
139}
140
141impl IntoIterator for ListStream {
142    type Item = Value;
143
144    type IntoIter = IntoIter;
145
146    fn into_iter(self) -> Self::IntoIter {
147        IntoIter {
148            stream: self.into_inner(),
149        }
150    }
151}
152
153impl From<ListStream> for PipelineData {
154    fn from(stream: ListStream) -> Self {
155        Self::list_stream(stream, None)
156    }
157}
158
159pub struct IntoIter {
160    stream: ValueIterator,
161}
162
163impl Iterator for IntoIter {
164    type Item = Value;
165
166    fn next(&mut self) -> Option<Self::Item> {
167        self.stream.next()
168    }
169}
170
171struct InterruptIter<I: Iterator> {
172    iter: I,
173    signals: Signals,
174}
175
176impl<I: Iterator> InterruptIter<I> {
177    fn new(iter: I, signals: Signals) -> Self {
178        Self { iter, signals }
179    }
180}
181
182impl<I: Iterator> Iterator for InterruptIter<I> {
183    type Item = <I as Iterator>::Item;
184
185    fn next(&mut self) -> Option<Self::Item> {
186        if self.signals.interrupted() {
187            None
188        } else {
189            self.iter.next()
190        }
191    }
192
193    fn size_hint(&self) -> (usize, Option<usize>) {
194        self.iter.size_hint()
195    }
196}