summaryrefslogtreecommitdiff
path: root/src/executor.rs
blob: a2a7856410170ade3c9ab4279942f4f53ce43049 (plain)
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
use crate::tasks::{CompletedTask, Task, TaskOutput};

pub struct Executor<'a, 't, N: Copy + PartialEq, T = ()> {
    task: &'t Task<N, T>,
    args: Box<[&'a str]>,
    ctx: T,
    complete_task: Option<CompletedTask<N>>,
}

impl<'a, 't, N: Copy + PartialEq, T> Executor<'a, 't, N, T> {
    pub fn new(task: &'t Task<N, T>, args: impl IntoIterator<Item = &'a str>, ctx: T) -> Self {
        Self {
            task: task,
            args: args
                .into_iter()
                .collect(),
            ctx,
            complete_task: None,
        }
    }

    pub fn run(&mut self) -> &TaskOutput {
        let output = (self
            .task
            .exec)(
            &self.args, &self.ctx,
        );
        self.complete_task = Some(
            CompletedTask {
                name: self
                    .task
                    .name,
                output: Some(output),
            },
        );
        self.complete_task
            .as_ref()
            .unwrap()
            .output
            .as_ref()
            .unwrap()
    }
}