Skip to content

Commit 4ff21a3

Browse files
committed
passthrough the extension to the Extractor itself
1 parent 601f369 commit 4ff21a3

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

crates/oxide/src/extractor/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,17 @@ impl fmt::Display for Extracted<'_> {
5656
#[derive(Debug)]
5757
pub struct Extractor<'a> {
5858
cursor: cursor::Cursor<'a>,
59+
extension: Option<&'a str>,
5960

6061
css_variable_machine: CssVariableMachine,
6162
candidate_machine: CandidateMachine,
6263
}
6364

6465
impl<'a> Extractor<'a> {
65-
pub fn new(input: &'a [u8]) -> Self {
66+
pub fn new(input: &'a [u8], extension: Option<&'a str>) -> Self {
6667
Self {
6768
cursor: cursor::Cursor::new(input),
69+
extension,
6870

6971
css_variable_machine: Default::default(),
7072
candidate_machine: Default::default(),
@@ -208,7 +210,7 @@ mod tests {
208210
}
209211

210212
fn extract_sorted_candidates(input: &str) -> Vec<&str> {
211-
let mut machine = Extractor::new(input.as_bytes());
213+
let mut machine = Extractor::new(input.as_bytes(), None);
212214
let mut actual = machine
213215
.extract()
214216
.iter()
@@ -222,7 +224,7 @@ mod tests {
222224
}
223225

224226
fn extract_sorted_css_variables(input: &str) -> Vec<&str> {
225-
let mut machine = Extractor::new(input.as_bytes());
227+
let mut machine = Extractor::new(input.as_bytes(), None);
226228
let mut actual = machine
227229
.extract()
228230
.iter()
@@ -287,12 +289,12 @@ mod tests {
287289
let input = include_bytes!("../fixtures/example.html");
288290

289291
let throughput = Throughput::compute(iterations, input.len(), || {
290-
let mut extractor = Extractor::new(input);
292+
let mut extractor = Extractor::new(input, None);
291293
_ = black_box(extractor.extract());
292294
});
293295
eprintln!("Extractor throughput: {:}", throughput);
294296

295-
let mut extractor = Extractor::new(input);
297+
let mut extractor = Extractor::new(input, None);
296298
let start = std::time::Instant::now();
297299
_ = black_box(extractor.extract().len());
298300
let end = start.elapsed();

crates/oxide/src/extractor/pre_processors/pre_processor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub trait PreProcessor: Sized + Default {
3333
let processor = Self::default();
3434
let transformed = processor.process(input);
3535

36-
let extracted = Extractor::new(&transformed).extract();
36+
let extracted = Extractor::new(&transformed, None).extract();
3737

3838
// Extract all candidates and css variables.
3939
let candidates = extracted

crates/oxide/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use tailwindcss_oxide::extractor::{Extracted, Extractor};
55
use tailwindcss_oxide::throughput::Throughput;
66

77
fn run_full_extractor(input: &[u8]) -> Vec<&[u8]> {
8-
Extractor::new(input)
8+
Extractor::new(input, None)
99
.extract()
1010
.into_iter()
1111
.map(|x| match x {

crates/oxide/src/scanner/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ impl Scanner {
318318
&mut self,
319319
changed_content: ChangedContent,
320320
) -> Vec<(String, usize)> {
321-
let content = read_changed_content(changed_content).unwrap_or_default();
321+
let (content, extension) = read_changed_content(changed_content).unwrap_or_default();
322322
let original_content = &content;
323323

324324
// Workaround for legacy upgrades:
@@ -328,7 +328,7 @@ impl Scanner {
328328
let content = content.replace("-[]", "XYZ");
329329
let offset = content.as_ptr() as usize;
330330

331-
let mut extractor = Extractor::new(&content[..]);
331+
let mut extractor = Extractor::new(&content[..], Some(&extension));
332332

333333
extractor
334334
.extract()
@@ -355,7 +355,7 @@ impl Scanner {
355355
}
356356
}
357357

358-
fn read_changed_content(c: ChangedContent) -> Option<Vec<u8>> {
358+
fn read_changed_content(c: ChangedContent) -> Option<(Vec<u8>, String)> {
359359
let (content, extension) = match c {
360360
ChangedContent::File(file, extension) => match std::fs::read(&file) {
361361
Ok(content) => (content, extension),
@@ -368,7 +368,7 @@ fn read_changed_content(c: ChangedContent) -> Option<Vec<u8>> {
368368
ChangedContent::Content(contents, extension) => (contents.into_bytes(), extension),
369369
};
370370

371-
Some(pre_process_input(&content, &extension))
371+
Some((pre_process_input(&content, &extension), extension))
372372
}
373373

374374
pub fn pre_process_input(content: &[u8], extension: &str) -> Vec<u8> {
@@ -389,7 +389,7 @@ pub fn pre_process_input(content: &[u8], extension: &str) -> Vec<u8> {
389389
}
390390

391391
#[tracing::instrument(skip_all)]
392-
fn read_all_files(changed_content: Vec<ChangedContent>) -> Vec<Vec<u8>> {
392+
fn read_all_files(changed_content: Vec<ChangedContent>) -> Vec<(Vec<u8>, String)> {
393393
event!(
394394
tracing::Level::INFO,
395395
"Reading {:?} file(s)",
@@ -403,16 +403,16 @@ fn read_all_files(changed_content: Vec<ChangedContent>) -> Vec<Vec<u8>> {
403403
}
404404

405405
#[tracing::instrument(skip_all)]
406-
fn parse_all_blobs(blobs: Vec<Vec<u8>>) -> Vec<String> {
406+
fn parse_all_blobs(blobs: Vec<(Vec<u8>, String)>) -> Vec<String> {
407407
let mut result: Vec<_> = blobs
408408
.par_iter()
409-
.flat_map(|blob| blob.par_split(|x| *x == b'\n'))
410-
.filter_map(|blob| {
409+
.flat_map(|(blob, extension)| blob.par_split(|x| *x == b'\n').map(move |x| (x, extension)))
410+
.filter_map(|(blob, extension)| {
411411
if blob.is_empty() {
412412
return None;
413413
}
414414

415-
let extracted = crate::extractor::Extractor::new(blob).extract();
415+
let extracted = crate::extractor::Extractor::new(blob, Some(extension)).extract();
416416
if extracted.is_empty() {
417417
return None;
418418
}

0 commit comments

Comments
 (0)