assemble_core/task/
action.rs

1use crate::{BuildResult, Executable, Project, Task};
2use std::any::type_name;
3use std::fmt::{Debug, Formatter};
4
5/// Represents some work that can be done by a task
6pub trait TaskAction<T: Task>: Send {
7    /// Executes the task action on some executable task along with it's owning
8    /// project.
9    fn execute(&self, task: &mut Executable<T>, project: &Project) -> BuildResult<()>;
10}
11
12impl<F, T> TaskAction<T> for F
13where
14    F: Fn(&mut Executable<T>, &Project) -> BuildResult,
15    F: Send,
16    T: Task,
17{
18    fn execute(&self, task: &mut Executable<T>, project: &Project) -> BuildResult<()> {
19        (self)(task, project)
20    }
21}
22
23impl<T: Task> TaskAction<T> for Action<T> {
24    fn execute(&self, task: &mut Executable<T>, project: &Project) -> BuildResult<()> {
25        (self.func)(task, project)
26    }
27}
28
29assert_obj_safe!(TaskAction<crate::defaults::tasks::Empty>);
30
31pub type DynamicTaskAction<T> = dyn Fn(&mut Executable<T>, &Project) -> BuildResult + Send + Sync;
32/// A structure to generically own a task action over `'static` lifetime
33pub struct Action<T: Task> {
34    func: Box<DynamicTaskAction<T>>,
35}
36
37impl<T: Task> Debug for Action<T> {
38    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
39        write!(f, "Action<{}>", type_name::<T>())
40    }
41}
42
43impl<T: Task> Action<T> {
44    /// Creates a new action from a function
45    pub fn new<F>(func: F) -> Self
46    where
47        F: Fn(&mut Executable<T>, &Project) -> BuildResult + 'static,
48        F: Send + Sync,
49    {
50        Self {
51            func: Box::new(func),
52        }
53    }
54}