assemble_core/task/
task_ordering.rs

1use crate::__export::TaskId;
2use crate::prelude::ProjectResult;
3use crate::project::buildable::{Buildable, IntoBuildable};
4
5use crate::Project;
6use std::collections::HashSet;
7use std::fmt::{Debug, Formatter};
8use std::sync::Arc;
9
10/// Represents some task ordering, describing a temporal constraint between
11/// the execution of some task and a [`Buildable`](Buildable).
12///
13/// The exact constraint is determined by the [`TaskOrderingKind`][0] used
14/// by the constraint. The `DependsOn` and `FinalizedBy` orderings put
15/// those tasks on the critical path, while all other kinds just inform the
16/// task execution plan general constraints for how tasks should be ran if they're
17/// both already on the critical path.
18///
19/// [0]: TaskOrderingKind
20#[derive(Clone)]
21pub struct TaskOrdering {
22    buildable: Arc<dyn Buildable>,
23    ordering_kind: TaskOrderingKind,
24}
25
26impl Debug for TaskOrdering {
27    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
28        write!(f, "{:?}({:?})", self.ordering_kind, self.buildable)
29    }
30}
31
32impl Buildable for TaskOrdering {
33    fn get_dependencies(&self, project: &Project) -> ProjectResult<HashSet<TaskId>> {
34        self.buildable.get_dependencies(project)
35    }
36}
37
38impl TaskOrdering {
39    /// Create a depends_on ordering
40    pub fn depends_on<B: IntoBuildable>(buildable: B) -> Self
41    where
42        B::Buildable: 'static,
43    {
44        Self {
45            buildable: Arc::new(buildable.into_buildable()),
46            ordering_kind: TaskOrderingKind::DependsOn,
47        }
48    }
49
50    pub fn buildable(&self) -> &Arc<dyn Buildable> {
51        &self.buildable
52    }
53    pub fn ordering_kind(&self) -> &TaskOrderingKind {
54        &self.ordering_kind
55    }
56}
57
58/// The kind of task ordering to establish temporal dependencies between tasks and buildables
59#[derive(Debug, Eq, PartialEq, Copy, Clone)]
60pub enum TaskOrderingKind {
61    DependsOn,
62    FinalizedBy,
63    RunsBefore,
64    RunsAfter,
65}