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
use {EXIT_ERROR, Spawn};
use conch_parser::ast;
use error::RuntimeError;
use env::LastStatusEnvironment;
use future::{EnvFuture, Poll};

/// A future representing the execution of a `Command`.
#[must_use = "futures do nothing unless polled"]
#[derive(Debug)]
pub struct Command<F> {
    inner: Inner<F>,
}

#[must_use = "futures do nothing unless polled"]
#[derive(Debug)]
enum Inner<F> {
    Pending(F),
    Unimplemented,
}

impl<E: ?Sized, T> Spawn<E> for ast::Command<T>
    where E: LastStatusEnvironment,
          T: Spawn<E>,
          T::Error: From<RuntimeError>,
{
    type Error = T::Error;
    type EnvFuture = Command<T::EnvFuture>;
    type Future = T::Future;

    fn spawn(self, env: &E) -> Self::EnvFuture {
        let inner = match self {
            ast::Command::Job(_) => Inner::Unimplemented,
            ast::Command::List(cmd) => Inner::Pending(cmd.spawn(env)),
        };

        Command {
            inner: inner,
        }
    }
}

impl<'a, E: ?Sized, T> Spawn<E> for &'a ast::Command<T>
    where E: LastStatusEnvironment,
          &'a T: Spawn<E>,
          <&'a T as Spawn<E>>::Error: From<RuntimeError>,
{
    type Error = <&'a T as Spawn<E>>::Error;
    type EnvFuture = Command<<&'a T as Spawn<E>>::EnvFuture>;
    type Future = <&'a T as Spawn<E>>::Future;

    fn spawn(self, env: &E) -> Self::EnvFuture {
        let inner = match *self {
            ast::Command::Job(_) => Inner::Unimplemented,
            ast::Command::List(ref cmd) => Inner::Pending(cmd.spawn(env)),
        };

        Command {
            inner: inner,
        }
    }
}

impl<E: ?Sized, F> EnvFuture<E> for Command<F>
    where F: EnvFuture<E>,
          F::Error: From<RuntimeError>,
          E: LastStatusEnvironment,
{
    type Item = F::Item;
    type Error = F::Error;

    fn poll(&mut self, env: &mut E) -> Poll<Self::Item, Self::Error> {
        match self.inner {
            Inner::Pending(ref mut f) => f.poll(env),
            Inner::Unimplemented => {
                // FIXME: eventual job control would be nice
                env.set_last_status(EXIT_ERROR);
                Err(RuntimeError::Unimplemented("job control is not currently supported").into())
            },
        }
    }

    fn cancel(&mut self, env: &mut E) {
        match self.inner {
            Inner::Pending(ref mut f) => f.cancel(env),
            Inner::Unimplemented => {},
        }
    }
}