1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
use crate::prelude::*;
use nu_protocol::{ReturnSuccess, ReturnValue, Value};
use std::iter::IntoIterator;

pub type OutputStream = InputStream;

pub struct ActionStream {
    pub values: Box<dyn Iterator<Item = ReturnValue> + Send + Sync>,
}

impl Iterator for ActionStream {
    type Item = ReturnValue;

    fn next(&mut self) -> Option<Self::Item> {
        self.values.next()
    }
}

impl ActionStream {
    pub fn new(values: impl Iterator<Item = ReturnValue> + Send + Sync + 'static) -> ActionStream {
        ActionStream {
            values: Box::new(values),
        }
    }

    pub fn empty() -> ActionStream {
        ActionStream {
            values: Box::new(std::iter::empty()),
        }
    }

    pub fn one(item: impl Into<ReturnValue>) -> ActionStream {
        let item = item.into();
        ActionStream {
            values: Box::new(std::iter::once(item)),
        }
    }

    pub fn from_input(input: impl Iterator<Item = Value> + Send + Sync + 'static) -> ActionStream {
        ActionStream {
            values: Box::new(input.map(ReturnSuccess::value)),
        }
    }

    pub fn drain_vec(&mut self) -> Vec<ReturnValue> {
        let mut output = vec![];

        for x in &mut self.values {
            output.push(x);
        }

        output
    }
}

impl From<InputStream> for ActionStream {
    fn from(input: InputStream) -> ActionStream {
        ActionStream {
            values: Box::new(input.into_iter().map(ReturnSuccess::value)),
        }
    }
}

// impl From<impl Iterator<Item = Value> + Send + Sync + 'static> for OutputStream {
//     fn from(input: impl Iterator<Item = Value> + Send + Sync + 'static) -> OutputStream {
//         OutputStream {
//             values: Box::new(input.map(ReturnSuccess::value)),
//         }
//     }
// }

// impl From<BoxStream<'static, ReturnValue>> for OutputStream {
//     fn from(input: BoxStream<'static, ReturnValue>) -> OutputStream {
//         OutputStream { values: input }
//     }
// }

impl From<VecDeque<ReturnValue>> for ActionStream {
    fn from(input: VecDeque<ReturnValue>) -> ActionStream {
        ActionStream {
            values: Box::new(input.into_iter()),
        }
    }
}

impl From<VecDeque<Value>> for ActionStream {
    fn from(input: VecDeque<Value>) -> ActionStream {
        let stream = input.into_iter().map(ReturnSuccess::value);
        ActionStream {
            values: Box::new(stream),
        }
    }
}

impl From<Vec<ReturnValue>> for ActionStream {
    fn from(input: Vec<ReturnValue>) -> ActionStream {
        ActionStream {
            values: Box::new(input.into_iter()),
        }
    }
}

impl From<Vec<Value>> for ActionStream {
    fn from(input: Vec<Value>) -> ActionStream {
        let stream = input.into_iter().map(ReturnSuccess::value);
        ActionStream {
            values: Box::new(stream),
        }
    }
}