Process a single rule, collecting matches and updating stats. Returns true if rule had any effect (matches or is a non-selector diagnostic).
(&mut self, rule_idx: usize, stylesheet: &StyleSheet<'_>, css_source: &str)
| 392 | /// Process a single rule, collecting matches and updating stats. |
| 393 | /// Returns true if rule had any effect (matches or is a non-selector diagnostic). |
| 394 | fn process_rule(&mut self, rule_idx: usize, stylesheet: &StyleSheet<'_>, css_source: &str) -> bool { |
| 395 | let (selector, diagnostic_stats, collectors) = { |
| 396 | let rule = &self.collection_rules[rule_idx]; |
| 397 | (rule.selector, rule.diagnostic_stats.clone(), rule.collectors.clone()) |
| 398 | }; |
| 399 | |
| 400 | let Some(selector) = selector else { |
| 401 | let parent_span = self.collection_rules[rule_idx].parent_span; |
| 402 | |
| 403 | let mut match_output = MatchOutput { |
| 404 | span: parent_span, |
| 405 | node_id: css_ast::NodeId::StyleRule, |
| 406 | properties: Default::default(), |
| 407 | size: 0, |
| 408 | stat_snapshot: SmallVec::new(), |
| 409 | }; |
| 410 | |
| 411 | for &stat_bits in &diagnostic_stats { |
| 412 | if let Some((name, &(_, count))) = self.stats.iter().find(|(name, _)| name.as_bits() == stat_bits) { |
| 413 | match_output.stat_snapshot.push((name.as_bits(), count)); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | self.collection_rules[rule_idx].matches.push(match_output); |
| 418 | |
| 419 | for collector_name in &collectors { |
| 420 | if let Some(&mut (stat_type, ref mut count)) = self.stats.get_mut(collector_name) { |
| 421 | match stat_type { |
| 422 | StatType::Counter => { |
| 423 | *count += 1; |
| 424 | } |
| 425 | StatType::Bytes => { |
| 426 | *count += (parent_span.end().0 - parent_span.start().0) as usize; |
| 427 | } |
| 428 | StatType::Lines => { |
| 429 | let text = &css_source[parent_span.start().0 as usize..parent_span.end().0 as usize]; |
| 430 | if !text.is_empty() { |
| 431 | *count += text.lines().count(); |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | return true; |
| 439 | }; |
| 440 | |
| 441 | let matcher = SelectorMatcher::new(selector, self.source, css_source); |
| 442 | let mut had_matches = false; |
| 443 | |
| 444 | for mut match_output in matcher.run(stylesheet) { |
| 445 | had_matches = true; |
| 446 | let span = match_output.span; |
| 447 | |
| 448 | for &stat_bits in &diagnostic_stats { |
| 449 | if let Some((name, &(_, count))) = self.stats.iter().find(|(name, _)| name.as_bits() == stat_bits) { |
| 450 | match_output.stat_snapshot.push((name.as_bits(), count)); |
| 451 | } |