fallow_extract/
module_parse.rs1use std::path::Path;
5
6use fallow_types::discover::FileId;
7use fallow_types::extract::ModuleInfo;
8
9use crate::parse::parse_source_to_module_unbounded;
10use fallow_types::extract::FlagPatterns;
11
12pub struct ExtractionOptions<'a> {
14 pub flag_patterns: &'a FlagPatterns,
15 pub mode: fallow_types::shared_state::ExtractionMode,
16}
17
18pub fn parse_source_to_module(
28 file_id: FileId,
29 path: &Path,
30 source: &str,
31 content_hash: u64,
32 need_complexity: bool,
33) -> ModuleInfo {
34 parse_source_to_module_with_flags(
35 file_id,
36 path,
37 source,
38 content_hash,
39 need_complexity,
40 &FlagPatterns::default(),
41 )
42}
43
44pub fn parse_source_to_module_with_flags(
50 file_id: FileId,
51 path: &Path,
52 source: &str,
53 content_hash: u64,
54 need_complexity: bool,
55 flag_patterns: &FlagPatterns,
56) -> ModuleInfo {
57 parse_source_without_bom(
58 file_id,
59 path,
60 crate::strip_bom(source),
61 content_hash,
62 need_complexity,
63 &ExtractionOptions {
64 flag_patterns,
65 mode: fallow_types::shared_state::ExtractionMode::Standard,
66 },
67 )
68}
69
70pub fn parse_source_without_bom(
77 file_id: FileId,
78 path: &Path,
79 source: &str,
80 content_hash: u64,
81 need_complexity: bool,
82 options: &ExtractionOptions<'_>,
83) -> ModuleInfo {
84 let bounded = crate::nesting::parse_file_bounded(path, source, || {
85 let module = parse_source_to_module_unbounded(
86 file_id,
87 path,
88 source,
89 content_hash,
90 need_complexity,
91 options,
92 );
93 (module, crate::css_masked_bytes::take())
96 });
97 match bounded {
98 Some((module, masked_bytes)) => {
99 crate::css_masked_bytes::add(masked_bytes);
100 module
101 }
102 None => crate::nesting::too_deep_module(file_id, source, content_hash),
103 }
104}
105
106#[cfg(test)]
108pub fn parse_source_to_module_with_mode(
109 file_id: FileId,
110 path: &Path,
111 source: &str,
112 content_hash: u64,
113 need_complexity: bool,
114 mode: fallow_types::shared_state::ExtractionMode,
115) -> ModuleInfo {
116 parse_source_without_bom(
117 file_id,
118 path,
119 crate::strip_bom(source),
120 content_hash,
121 need_complexity,
122 &ExtractionOptions {
123 flag_patterns: &FlagPatterns::default(),
124 mode,
125 },
126 )
127}