Add some documentation and a better name for children_selector

This commit is contained in:
Schrottkatze 2025-02-15 08:25:12 +01:00
parent ba4d1dacaf
commit ff51535d7d
No known key found for this signature in database
2 changed files with 15 additions and 9 deletions

View File

@ -93,9 +93,8 @@ pub enum Selector {
Before { selector: Arc<Self>, end: Arc<Self>, inclusive: bool }, Before { selector: Arc<Self>, end: Arc<Self>, inclusive: bool },
/// Matches all matches of `selector` after `start`. /// Matches all matches of `selector` after `start`.
After { selector: Arc<Self>, start: Arc<Self>, inclusive: bool }, After { selector: Arc<Self>, start: Arc<Self>, inclusive: bool },
/// Matches all children of `selector` matchin `c` /// Matches all children of `selector` matching `children_selector`
/// TODO: Better name Contains { selector: Arc<Self>, children_selector: Arc<Self> },
Contains { selector: Arc<Self>, c: Arc<Self> },
} }
impl Selector { impl Selector {
@ -228,9 +227,14 @@ impl Selector {
} }
} }
/// Returns a modified selector that only matches children of what `self`
/// selects that match `children_selector`.
#[func] #[func]
pub fn contains(self, c: LocatableSelector) -> Selector { pub fn contains(self, children_selector: LocatableSelector) -> Selector {
Self::Contains { selector: Arc::new(self), c: Arc::new(c.0) } Self::Contains {
selector: Arc::new(self),
children_selector: Arc::new(children_selector.0),
}
} }
} }
@ -277,7 +281,9 @@ impl Repr for Selector {
inclusive_arg inclusive_arg
) )
} }
Self::Contains { .. } => todo!(), Self::Contains { selector, children_selector } => {
eco_format!("{}.contains({})", selector.repr(), children_selector.repr())
}
} }
} }
} }
@ -365,7 +371,7 @@ impl FromValue for LocatableSelector {
} }
Selector::Before { selector, end: split, .. } Selector::Before { selector, end: split, .. }
| Selector::After { selector, start: split, .. } | Selector::After { selector, start: split, .. }
| Selector::Contains { selector, c: split } => { | Selector::Contains { selector, children_selector: split } => {
for selector in [selector, split] { for selector in [selector, split] {
validate(selector)?; validate(selector)?;
} }

View File

@ -185,10 +185,10 @@ impl Introspector {
} }
list list
} }
Selector::Contains { selector, c } => self Selector::Contains { selector, children_selector } => self
.query(selector) .query(selector)
.iter() .iter()
.flat_map(|children| children.query(c)) .flat_map(|children| children.query(children_selector))
.collect(), .collect(),
// Not supported here. // Not supported here.
Selector::Can(_) | Selector::Regex(_) => EcoVec::new(), Selector::Can(_) | Selector::Regex(_) => EcoVec::new(),