1use 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 pub call_facts: bool,
20 pub flags: bool,
23 pub component_contracts: bool,
26}
27
28impl ExtractionOptions<'_> {
29 pub const fn gates(&self) -> ExtractionGates {
30 ExtractionGates {
31 mode: self.mode,
32 call_facts: self.call_facts,
33 component_contracts: self.component_contracts,
34 }
35 }
36}
37
38#[derive(Clone, Copy)]
41pub struct ExtractionGates {
42 pub mode: fallow_types::shared_state::ExtractionMode,
43 pub call_facts: bool,
44 pub component_contracts: bool,
45}
46
47#[cfg(test)]
48impl ExtractionGates {
49 pub const STANDARD: Self = Self {
51 mode: fallow_types::shared_state::ExtractionMode::Standard,
52 call_facts: true,
53 component_contracts: true,
54 };
55}
56
57pub fn parse_source_to_module(
67 file_id: FileId,
68 path: &Path,
69 source: &str,
70 content_hash: u64,
71 need_complexity: bool,
72) -> ModuleInfo {
73 parse_source_to_module_with_flags(
74 file_id,
75 path,
76 source,
77 content_hash,
78 need_complexity,
79 &FlagPatterns::default(),
80 )
81}
82
83pub fn parse_source_to_module_with_flags(
89 file_id: FileId,
90 path: &Path,
91 source: &str,
92 content_hash: u64,
93 need_complexity: bool,
94 flag_patterns: &FlagPatterns,
95) -> ModuleInfo {
96 parse_source_without_bom(
97 file_id,
98 path,
99 crate::strip_bom(source),
100 content_hash,
101 need_complexity,
102 &ExtractionOptions {
103 flag_patterns,
104 mode: fallow_types::shared_state::ExtractionMode::Standard,
105 call_facts: true,
106 flags: true,
107 component_contracts: true,
108 },
109 )
110}
111
112pub fn parse_source_without_bom(
119 file_id: FileId,
120 path: &Path,
121 source: &str,
122 content_hash: u64,
123 need_complexity: bool,
124 options: &ExtractionOptions<'_>,
125) -> ModuleInfo {
126 let mut module = parse_source_bounded(
127 file_id,
128 path,
129 source,
130 content_hash,
131 need_complexity,
132 options,
133 );
134 if !options.call_facts {
135 module.function_contracts = None;
138 }
139 if !options.component_contracts {
140 module.component_contracts = None;
142 }
143 module
144}
145
146fn parse_source_bounded(
147 file_id: FileId,
148 path: &Path,
149 source: &str,
150 content_hash: u64,
151 need_complexity: bool,
152 options: &ExtractionOptions<'_>,
153) -> ModuleInfo {
154 let bounded = crate::nesting::parse_file_bounded(path, source, || {
155 let module = parse_source_to_module_unbounded(
156 file_id,
157 path,
158 source,
159 content_hash,
160 need_complexity,
161 options,
162 );
163 (module, crate::css_masked_bytes::take())
166 });
167 match bounded {
168 Some((module, masked_bytes)) => {
169 crate::css_masked_bytes::add(masked_bytes);
170 module
171 }
172 None => crate::nesting::too_deep_module(file_id, source, content_hash),
173 }
174}
175
176#[cfg(test)]
178pub fn parse_source_to_module_with_mode(
179 file_id: FileId,
180 path: &Path,
181 source: &str,
182 content_hash: u64,
183 need_complexity: bool,
184 mode: fallow_types::shared_state::ExtractionMode,
185) -> ModuleInfo {
186 parse_source_without_bom(
187 file_id,
188 path,
189 crate::strip_bom(source),
190 content_hash,
191 need_complexity,
192 &ExtractionOptions {
193 flag_patterns: &FlagPatterns::default(),
194 mode,
195 call_facts: true,
196 flags: true,
197 component_contracts: true,
198 },
199 )
200}