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
use conch_parser::ast;
use error::IsFatalError;
use env::{LastStatusEnvironment, ReportErrorEnvironment};
use spawn::{AndOr, AndOrList, ExitResult, Spawn, and_or_list};
use std::slice;
use std::vec;

impl<T> From<ast::AndOr<T>> for AndOr<T> {
    fn from(and_or: ast::AndOr<T>) -> Self {
        match and_or {
            ast::AndOr::And(t) => AndOr::And(t),
            ast::AndOr::Or(t) => AndOr::Or(t),
        }
    }
}

/// An iterator that converts `&conch_parser::ast::AndOr<T>` to `conch_runtime::spawn::AndOr<&T>`.
#[must_use = "iterators do nothing unless polled"]
#[derive(Debug)]
pub struct AndOrRefIter<I> {
    iter: I,
}

impl<E: ?Sized, T> Spawn<E> for ast::AndOrList<T>
    where E: LastStatusEnvironment + ReportErrorEnvironment,
          T: Spawn<E>,
          T::Error: IsFatalError,
{
    type Error = T::Error;
    type EnvFuture = AndOrList<T, vec::IntoIter<AndOr<T>>, E>;
    type Future = ExitResult<T::Future>;

    fn spawn(self, env: &E) -> Self::EnvFuture {
        let rest: Vec<_> = self.rest.into_iter().map(AndOr::from).collect();
        and_or_list(self.first, rest, env)
    }
}

impl<'a, E: ?Sized, T> Spawn<E> for &'a ast::AndOrList<T>
    where E: LastStatusEnvironment + ReportErrorEnvironment,
          &'a T: Spawn<E>,
          <&'a T as Spawn<E>>::Error: IsFatalError,
{
    type Error = <&'a T as Spawn<E>>::Error;
    type EnvFuture = AndOrList<&'a T, AndOrRefIter<slice::Iter<'a, ast::AndOr<T>>>, E>;
    type Future = ExitResult<<&'a T as Spawn<E>>::Future>;

    fn spawn(self, env: &E) -> Self::EnvFuture {
        let iter = AndOrRefIter { iter: self.rest.iter() };
        and_or_list(&self.first, iter, env)
    }
}

impl<'a, I, T: 'a> Iterator for AndOrRefIter<I>
    where I: Iterator<Item = &'a ast::AndOr<T>>,
{
    type Item = AndOr<&'a T>;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|and_or| match *and_or {
            ast::AndOr::And(ref t) => AndOr::And(t),
            ast::AndOr::Or(ref t) => AndOr::Or(t),
        })
    }
}