use super::context::Context;
use super::description::Description;
use super::handler::Handler;
use super::Task;
use std::cmp::Ordering;
#[derive(PartialEq, PartialOrd, Eq, Ord, Debug, Clone)]
pub enum Operation {
None,
Any,
Delete,
Create,
Update,
}
#[derive(Debug, Clone)]
pub struct Job {
operation: Operation,
task: Task,
priority: u8,
}
impl Job {
pub(crate) fn new(task: Task) -> Self {
Job {
operation: Operation::Update,
task,
priority: 0,
}
}
pub fn id(&self) -> &str {
self.task.id()
}
pub fn operation(&self) -> &Operation {
&self.operation
}
pub fn priority(&self) -> u8 {
self.priority
}
pub fn with_priority(mut self, priority: u8) -> Self {
self.priority = priority;
self
}
fn with_operation(mut self, operation: Operation) -> Self {
self.operation = operation;
self
}
pub fn with_description<D, T>(mut self, description: D) -> Self
where
D: Description<T>,
{
self.task = self.task.with_description(description);
self
}
pub(crate) fn new_task(&self, context: Context) -> Task {
self.task.clone().with_context(context)
}
}
macro_rules! define_job {
($func_name:ident, $operation:expr) => {
#[doc = concat!("Create a new `Job` for the [`", stringify!($operation), "`] operation.")]
pub fn $func_name<H, T, O, I>(handler: H) -> Job
where
H: Handler<T, O, I>,
I: 'static,
{
Job::new(handler.into_task()).with_operation($operation)
}
};
}
define_job!(create, Operation::Create);
define_job!(update, Operation::Update);
define_job!(delete, Operation::Delete);
define_job!(any, Operation::Any);
define_job!(none, Operation::None);
impl PartialEq for Job {
fn eq(&self, other: &Self) -> bool {
self.task.id() == other.task.id()
&& self.operation == other.operation
&& self.priority == other.priority
}
}
impl Eq for Job {}
impl PartialOrd for Job {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Job {
fn cmp(&self, other: &Self) -> Ordering {
self.task.id().cmp(other.task.id())
}
}