mod display;
#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct JobId {
pub namespace: Vec<Box<str>>,
pub name: Box<str>,
}
#[derive(Debug, Default)]
pub struct Job {
pub id: JobId,
pub source: Box<str>,
pub interpretter: Option<Box<str>>,
pub script: Vec<Box<str>>,
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Relation {
When(Rule),
Trigger {
target: Box<str>,
codes: Vec<Code>,
},
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Rule {
All {
name: Box<str>,
rules: Vec<Rule>,
},
Any {
name: Box<str>,
rules: Vec<Rule>,
},
After {
target: Box<str>,
codes: Vec<Code>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Code {
Explicit(usize),
Range {
min: usize,
max: usize,
mask: usize,
},
}
impl Code {
pub(crate) fn matches(&self, code: usize) -> bool {
match self {
Code::Explicit(explicit) => *explicit == code,
Code::Range { min, max, mask } => {
code >= *min && code <= *max && (*mask == 0 || (code & mask != 0))
}
}
}
}
impl From<usize> for Code {
fn from(value: usize) -> Self {
Code::Explicit(value)
}
}
impl From<Option<usize>> for Code {
fn from(value: Option<usize>) -> Self {
Code::Explicit(value.unwrap_or_default())
}
}
impl<Min, Max, Mask> From<(Min, Max, Mask)> for Code
where
Min: Into<Option<usize>>,
Max: Into<Option<usize>>,
Mask: Into<Option<usize>>,
{
fn from(value: (Min, Max, Mask)) -> Self {
Code::Range {
min: Into::<Option<usize>>::into(value.0).unwrap_or_default(),
max: Into::<Option<usize>>::into(value.1).unwrap_or(usize::MAX),
mask: Into::<Option<usize>>::into(value.2).unwrap_or_default(),
}
}
}
#[test]
fn code_0_in_0_3() {
assert!(Code::Range {
min: 0,
max: 3,
mask: 0
}
.matches(0))
}