ferroui_base/styling/
selector_match.rs1use super::activators::{AndActivatorBuilder, IStyleActivator};
2use std::fmt;
3use std::rc::Rc;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub enum SelectorMatchResult {
8 NeverThisType,
10 NeverThisInstance,
12 Sometimes,
14 AlwaysThisInstance,
17 AlwaysThisType,
19}
20
21#[derive(Clone)]
27pub struct SelectorMatch {
28 result: SelectorMatchResult,
29 activator: Option<Rc<dyn IStyleActivator>>,
30}
31
32impl SelectorMatch {
33 pub const NEVER_THIS_TYPE: SelectorMatch = SelectorMatch::new(SelectorMatchResult::NeverThisType);
35
36 pub const NEVER_THIS_INSTANCE: SelectorMatch = SelectorMatch::new(SelectorMatchResult::NeverThisInstance);
38
39 pub const ALWAYS_THIS_TYPE: SelectorMatch = SelectorMatch::new(SelectorMatchResult::AlwaysThisType);
41
42 pub const ALWAYS_THIS_INSTANCE: SelectorMatch = SelectorMatch::new(SelectorMatchResult::AlwaysThisInstance);
44
45 pub fn sometimes(activator: Rc<dyn IStyleActivator>) -> Self {
48 Self { result: SelectorMatchResult::Sometimes, activator: Some(activator) }
49 }
50
51 pub const fn new(result: SelectorMatchResult) -> Self {
53 Self { result, activator: None }
54 }
55
56 #[inline]
61 pub fn is_match(&self) -> bool {
62 self.result >= SelectorMatchResult::Sometimes
63 }
64
65 #[inline]
67 pub fn result(&self) -> SelectorMatchResult {
68 self.result
69 }
70
71 #[inline]
74 pub fn activator(&self) -> Option<&Rc<dyn IStyleActivator>> {
75 self.activator.as_ref()
76 }
77
78 pub(crate) fn into_activator(self) -> Option<Rc<dyn IStyleActivator>> {
79 self.activator
80 }
81
82 pub fn and(self, other: SelectorMatch) -> SelectorMatch {
84 let result = self.result.min(other.result);
85
86 if result == SelectorMatchResult::Sometimes {
87 let mut activators = AndActivatorBuilder::new();
88 activators.add(self.activator);
89 activators.add(other.activator);
90 SelectorMatch::sometimes(activators.get())
91 } else {
92 SelectorMatch::new(result)
93 }
94 }
95}
96
97impl fmt::Debug for SelectorMatch {
98 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99 fmt::Debug::fmt(&self.result, f)
100 }
101}
102
103impl fmt::Display for SelectorMatch {
104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105 fmt::Debug::fmt(&self.result, f)
106 }
107}