nu_stream/
output.rs

1use crate::prelude::*;
2use nu_protocol::{ReturnSuccess, ReturnValue, Value};
3use std::iter::IntoIterator;
4
5pub type OutputStream = InputStream;
6
7pub struct ActionStream {
8    pub values: Box<dyn Iterator<Item = ReturnValue> + Send + Sync>,
9}
10
11impl Iterator for ActionStream {
12    type Item = ReturnValue;
13
14    fn next(&mut self) -> Option<Self::Item> {
15        self.values.next()
16    }
17}
18
19impl ActionStream {
20    pub fn new(values: impl Iterator<Item = ReturnValue> + Send + Sync + 'static) -> ActionStream {
21        ActionStream {
22            values: Box::new(values),
23        }
24    }
25
26    pub fn empty() -> ActionStream {
27        ActionStream {
28            values: Box::new(std::iter::empty()),
29        }
30    }
31
32    pub fn one(item: impl Into<ReturnValue>) -> ActionStream {
33        let item = item.into();
34        ActionStream {
35            values: Box::new(std::iter::once(item)),
36        }
37    }
38
39    pub fn from_input(input: impl Iterator<Item = Value> + Send + Sync + 'static) -> ActionStream {
40        ActionStream {
41            values: Box::new(input.map(ReturnSuccess::value)),
42        }
43    }
44
45    pub fn drain_vec(&mut self) -> Vec<ReturnValue> {
46        let mut output = vec![];
47
48        for x in &mut self.values {
49            output.push(x);
50        }
51
52        output
53    }
54}
55
56impl From<InputStream> for ActionStream {
57    fn from(input: InputStream) -> ActionStream {
58        ActionStream {
59            values: Box::new(input.into_iter().map(ReturnSuccess::value)),
60        }
61    }
62}
63
64// impl From<impl Iterator<Item = Value> + Send + Sync + 'static> for OutputStream {
65//     fn from(input: impl Iterator<Item = Value> + Send + Sync + 'static) -> OutputStream {
66//         OutputStream {
67//             values: Box::new(input.map(ReturnSuccess::value)),
68//         }
69//     }
70// }
71
72// impl From<BoxStream<'static, ReturnValue>> for OutputStream {
73//     fn from(input: BoxStream<'static, ReturnValue>) -> OutputStream {
74//         OutputStream { values: input }
75//     }
76// }
77
78impl From<VecDeque<ReturnValue>> for ActionStream {
79    fn from(input: VecDeque<ReturnValue>) -> ActionStream {
80        ActionStream {
81            values: Box::new(input.into_iter()),
82        }
83    }
84}
85
86impl From<VecDeque<Value>> for ActionStream {
87    fn from(input: VecDeque<Value>) -> ActionStream {
88        let stream = input.into_iter().map(ReturnSuccess::value);
89        ActionStream {
90            values: Box::new(stream),
91        }
92    }
93}
94
95impl From<Vec<ReturnValue>> for ActionStream {
96    fn from(input: Vec<ReturnValue>) -> ActionStream {
97        ActionStream {
98            values: Box::new(input.into_iter()),
99        }
100    }
101}
102
103impl From<Vec<Value>> for ActionStream {
104    fn from(input: Vec<Value>) -> ActionStream {
105        let stream = input.into_iter().map(ReturnSuccess::value);
106        ActionStream {
107            values: Box::new(stream),
108        }
109    }
110}