Skip to content

Commit e122d89

Browse files
committed
small refactor: abstract common extraction logic
1 parent c841426 commit e122d89

File tree

1 file changed

+12
-34
lines changed
  • crates/oxide/src/scanner

1 file changed

+12
-34
lines changed

crates/oxide/src/scanner/mod.rs

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -429,43 +429,21 @@ fn read_all_files(changed_content: Vec<ChangedContent>) -> Vec<Vec<u8>> {
429429

430430
#[tracing::instrument(skip_all)]
431431
fn extract_css_variables(blobs: Vec<Vec<u8>>) -> Vec<String> {
432-
let mut result: Vec<_> = blobs
433-
.par_iter()
434-
.flat_map(|blob| blob.par_split(|x| *x == b'\n'))
435-
.filter_map(|blob| {
436-
if blob.is_empty() {
437-
return None;
438-
}
439-
440-
let extracted = crate::extractor::Extractor::new(blob).extract_variables_from_css();
441-
if extracted.is_empty() {
442-
return None;
443-
}
444-
445-
Some(FxHashSet::from_iter(extracted.into_iter().map(
446-
|x| match x {
447-
Extracted::CssVariable(bytes) => bytes,
448-
_ => &[],
449-
},
450-
)))
451-
})
452-
.reduce(Default::default, |mut a, b| {
453-
a.extend(b);
454-
a
455-
})
456-
.into_iter()
457-
.map(|s| unsafe { String::from_utf8_unchecked(s.to_vec()) })
458-
.collect();
459-
460-
// SAFETY: Unstable sort is faster and in this scenario it's also safe because we are
461-
// guaranteed to have unique candidates.
462-
result.par_sort_unstable();
463-
464-
result
432+
extract(blobs, |mut extractor| {
433+
extractor.extract_variables_from_css()
434+
})
465435
}
466436

467437
#[tracing::instrument(skip_all)]
468438
fn parse_all_blobs(blobs: Vec<Vec<u8>>) -> Vec<String> {
439+
extract(blobs, |mut extractor| extractor.extract())
440+
}
441+
442+
#[tracing::instrument(skip_all)]
443+
fn extract<H>(blobs: Vec<Vec<u8>>, handle: H) -> Vec<String>
444+
where
445+
H: Fn(Extractor) -> Vec<Extracted> + std::marker::Sync,
446+
{
469447
let mut result: Vec<_> = blobs
470448
.par_iter()
471449
.flat_map(|blob| blob.par_split(|x| *x == b'\n'))
@@ -474,7 +452,7 @@ fn parse_all_blobs(blobs: Vec<Vec<u8>>) -> Vec<String> {
474452
return None;
475453
}
476454

477-
let extracted = crate::extractor::Extractor::new(blob).extract();
455+
let extracted = handle(crate::extractor::Extractor::new(blob));
478456
if extracted.is_empty() {
479457
return None;
480458
}

0 commit comments

Comments
 (0)