Skip to content

Commit 132b91f

Browse files
committed
refacto: pass whole compiler to Scanner::new
This avoids the "too many arguments" lint on Scanner::new, and will scale better.
1 parent f6524d4 commit 132b91f

File tree

2 files changed

+27
-41
lines changed

2 files changed

+27
-41
lines changed

boreal/src/compiler/mod.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ use crate::{statistics, Scanner};
3030
#[derive(Debug)]
3131
pub struct Compiler {
3232
/// List of compiled rules.
33-
rules: Vec<rule::Rule>,
33+
pub(crate) rules: Vec<rule::Rule>,
3434

3535
/// List of compiled, global rules.
36-
global_rules: Vec<rule::Rule>,
36+
pub(crate) global_rules: Vec<rule::Rule>,
3737

3838
/// List of compiled variables.
39-
variables: Vec<variable::Variable>,
39+
pub(crate) variables: Vec<variable::Variable>,
4040

4141
/// Number of variables used by global rules.
4242
nb_global_rules_variables: usize,
@@ -46,7 +46,7 @@ pub struct Compiler {
4646
/// This list always contains at least one namespace: the default one,
4747
/// at index 0. Other namespaces are added when rules are added in the
4848
/// non default namespace.
49-
namespaces: Vec<Namespace>,
49+
pub(crate) namespaces: Vec<Namespace>,
5050

5151
/// Map from the namespace name to its index in the `namespaces` list.
5252
namespaces_indexes: HashMap<String, usize>,
@@ -57,15 +57,15 @@ pub struct Compiler {
5757
available_modules: HashMap<&'static str, AvailableModule>,
5858

5959
/// List of imported modules, passed to the scanner.
60-
imported_modules: Vec<Box<dyn crate::module::Module>>,
60+
pub(crate) imported_modules: Vec<Box<dyn crate::module::Module>>,
6161

6262
/// Externally defined symbols.
63-
external_symbols: Vec<external_symbol::ExternalSymbol>,
63+
pub(crate) external_symbols: Vec<external_symbol::ExternalSymbol>,
6464

6565
/// Bytes intern pool.
6666
///
6767
/// This is used to reduce memory footprint and share byte strings.
68-
bytes_pool: BytesPoolBuilder,
68+
pub(crate) bytes_pool: BytesPoolBuilder,
6969

7070
/// Compilation parameters
7171
params: CompilerParams,
@@ -484,17 +484,7 @@ impl Compiler {
484484
/// Can fail if generating a set of all rules variables is not possible.
485485
#[must_use]
486486
pub fn into_scanner(self) -> Scanner {
487-
let namespaces = self.namespaces.into_iter().map(|v| v.name).collect();
488-
489-
Scanner::new(
490-
self.rules,
491-
self.global_rules,
492-
self.variables,
493-
self.imported_modules,
494-
self.external_symbols,
495-
namespaces,
496-
self.bytes_pool.into_pool(),
497-
)
487+
Scanner::new(self)
498488
}
499489
}
500490

@@ -505,9 +495,9 @@ impl Compiler {
505495
/// - new rules can reference already existing rules
506496
/// - new rules can either import new modules, or directly use already imported modules
507497
#[derive(Debug, Default)]
508-
struct Namespace {
498+
pub(crate) struct Namespace {
509499
/// Name of the namespace, `None` if default.
510-
name: Option<String>,
500+
pub(crate) name: Option<String>,
511501

512502
/// Map of a rule name to its index in the `rules` vector in [`Compiler`].
513503
///

boreal/src/scanner/mod.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::evaluator::{self, evaluate_rule, EvalError};
1111
use crate::memory::{FragmentedMemory, Memory, Region};
1212
use crate::module::{Module, ModuleData, ModuleUserData};
1313
use crate::timeout::TimeoutChecker;
14-
use crate::{statistics, Metadata};
14+
use crate::{statistics, Compiler, Metadata};
1515

1616
pub use crate::evaluator::variable::StringMatch;
1717

@@ -101,15 +101,19 @@ pub struct Scanner {
101101
}
102102

103103
impl Scanner {
104-
pub(crate) fn new(
105-
rules: Vec<Rule>,
106-
global_rules: Vec<Rule>,
107-
variables: Vec<Variable>,
108-
modules: Vec<Box<dyn Module>>,
109-
external_symbols: Vec<ExternalSymbol>,
110-
namespaces: Vec<Option<String>>,
111-
bytes_pool: BytesPool,
112-
) -> Self {
104+
pub(crate) fn new(compiler: Compiler) -> Self {
105+
let Compiler {
106+
rules,
107+
global_rules,
108+
variables,
109+
namespaces,
110+
imported_modules,
111+
external_symbols,
112+
bytes_pool,
113+
..
114+
} = compiler;
115+
let namespaces = namespaces.into_iter().map(|v| v.name).collect();
116+
113117
let ac_scan = ac_scan::AcScan::new(&variables);
114118

115119
let mut external_symbols_values = Vec::new();
@@ -129,10 +133,10 @@ impl Scanner {
129133
global_rules,
130134
variables,
131135
ac_scan,
132-
modules,
136+
modules: imported_modules,
133137
external_symbols_map,
134138
namespaces,
135-
bytes_pool,
139+
bytes_pool: bytes_pool.into_pool(),
136140
}),
137141
scan_params: ScanParams::default(),
138142
external_symbols_values,
@@ -1503,15 +1507,7 @@ mod tests {
15031507

15041508
#[test]
15051509
fn test_types_traits() {
1506-
test_type_traits(Scanner::new(
1507-
Vec::new(),
1508-
Vec::new(),
1509-
Vec::new(),
1510-
Vec::new(),
1511-
Vec::new(),
1512-
Vec::new(),
1513-
BytesPool::default(),
1514-
));
1510+
test_type_traits(Scanner::new(Compiler::default()));
15151511
test_type_traits_non_clonable(ScanResult {
15161512
matched_rules: Vec::new(),
15171513
module_values: Vec::new(),

0 commit comments

Comments
 (0)