use std::cmp::Ordering;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Specificity {
pub inline: bool,
pub important: bool,
pub ids: usize,
pub classes: usize,
pub types: usize,
pub order: usize,
}
impl Specificity {
pub fn new(ids: usize, classes: usize, types: usize, order: usize) -> Self {
Self {
inline: false,
important: false,
ids,
classes,
types,
order,
}
}
pub fn inline() -> Self {
Self {
inline: true,
..Default::default()
}
}
pub fn important(mut self) -> Self {
self.important = true;
self
}
}
impl Ord for Specificity {
fn cmp(&self, other: &Self) -> Ordering {
match (self.important, other.important) {
(true, false) => return Ordering::Greater,
(false, true) => return Ordering::Less,
_ => {}
}
match (self.inline, other.inline) {
(true, false) => return Ordering::Greater,
(false, true) => return Ordering::Less,
_ => {}
}
match self.ids.cmp(&other.ids) {
Ordering::Equal => {}
ord => return ord,
}
match self.classes.cmp(&other.classes) {
Ordering::Equal => {}
ord => return ord,
}
match self.types.cmp(&other.types) {
Ordering::Equal => {}
ord => return ord,
}
self.order.cmp(&other.order)
}
}
impl PartialOrd for Specificity {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}