You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tourmaline/src/task/mod.rs

75 lines
1.8 KiB
Rust

use std::cmp::Ordering;
use crate::{distro::OSConfig, error::AppResult};
use self::{base_task::BaseTask, custom_task::CustomTask, exec_builder::ExecBuilder};
pub mod base_task;
mod chrooting;
pub mod custom_task;
pub mod exec_builder;
pub mod task_executor;
pub trait TaskTrait {
fn up(&self, config: &OSConfig) -> AppResult<Option<ExecBuilder>>;
fn down(&self, config: &OSConfig) -> AppResult<Option<ExecBuilder>>;
/// Used to decide the execution order
/// smaller values mean the task get's executed earlier
fn order(&self) -> usize;
}
#[derive(Clone, Debug)]
pub enum Task {
Base(BaseTask),
Custom(CustomTask),
}
impl Task {
pub fn is_custom(&self) -> bool {
match self {
Task::Base(_) => false,
Task::Custom(_) => true,
}
}
pub fn is_base(&self) -> bool {
!self.is_custom()
}
pub fn compare(&self, other: &Self) -> Ordering {
if self.is_base() && other.is_custom() {
Ordering::Less
} else if self.is_custom() && other.is_base() || self.order() > other.order() {
Ordering::Greater
} else if self.order() < other.order() {
Ordering::Less
} else {
Ordering::Equal
}
}
}
impl TaskTrait for Task {
#[inline]
fn up(&self, config: &OSConfig) -> AppResult<Option<ExecBuilder>> {
match self {
Task::Base(b) => b.up(config),
Task::Custom(c) => c.up(config),
}
}
#[inline]
fn down(&self, config: &OSConfig) -> AppResult<Option<ExecBuilder>> {
match self {
Task::Base(b) => b.down(config),
Task::Custom(c) => c.down(config),
}
}
fn order(&self) -> usize {
match self {
Task::Base(b) => b.order(),
Task::Custom(c) => c.order(),
}
}
}