-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
mod.rs
176 lines (146 loc) · 4.79 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
pub(crate) mod path_require_mode;
mod require_mode;
use std::path::Path;
use crate::nodes::Block;
use crate::rules::{
Context, Rule, RuleConfiguration, RuleConfigurationError, RuleProcessResult, RuleProperties,
};
use crate::Parser;
pub use require_mode::BundleRequireMode;
use wax::Pattern;
pub const BUNDLER_RULE_NAME: &str = "bundler";
#[derive(Debug)]
pub(crate) struct BundleOptions {
parser: Parser,
modules_identifier: String,
excludes: Option<wax::Any<'static>>,
}
impl BundleOptions {
fn new<'a>(
parser: Parser,
modules_identifier: impl Into<String>,
excludes: impl Iterator<Item = &'a str>,
) -> Self {
let excludes: Vec<_> = excludes
.filter_map(|exclusion| match wax::Glob::new(exclusion) {
Ok(glob) => Some(glob.into_owned()),
Err(err) => {
log::warn!(
"unable to create exclude matcher from `{}`: {}",
exclusion,
err.to_string()
);
None
}
})
.collect();
Self {
parser,
modules_identifier: modules_identifier.into(),
excludes: if excludes.is_empty() {
None
} else {
let any_pattern = wax::any::<wax::Glob, _>(excludes)
.expect("exclude globs errors should be filtered and only emit a warning");
Some(any_pattern)
},
}
}
fn parser(&self) -> &Parser {
&self.parser
}
fn modules_identifier(&self) -> &str {
&self.modules_identifier
}
fn is_excluded(&self, require: &Path) -> bool {
self.excludes
.as_ref()
.map(|any| any.is_match(require))
.unwrap_or(false)
}
}
/// A rule that inlines required modules
#[derive(Debug)]
pub(crate) struct Bundler {
require_mode: BundleRequireMode,
options: BundleOptions,
}
impl Bundler {
pub(crate) fn new<'a>(
parser: Parser,
require_mode: BundleRequireMode,
excludes: impl Iterator<Item = &'a str>,
) -> Self {
Self {
require_mode,
options: BundleOptions::new(parser, DEFAULT_MODULE_IDENTIFIER, excludes),
}
}
pub(crate) fn with_modules_identifier(mut self, modules_identifier: impl Into<String>) -> Self {
self.options.modules_identifier = modules_identifier.into();
self
}
}
impl Rule for Bundler {
fn process(&self, block: &mut Block, context: &Context) -> RuleProcessResult {
self.require_mode
.process_block(block, context, &self.options)
}
}
impl RuleConfiguration for Bundler {
fn configure(&mut self, _properties: RuleProperties) -> Result<(), RuleConfigurationError> {
Err(RuleConfigurationError::InternalUsageOnly(
self.get_name().to_owned(),
))
}
fn get_name(&self) -> &'static str {
BUNDLER_RULE_NAME
}
fn serialize_to_properties(&self) -> RuleProperties {
RuleProperties::new()
}
}
const DEFAULT_MODULE_IDENTIFIER: &str = "__DARKLUA_BUNDLE_MODULES";
#[cfg(test)]
mod test {
use super::*;
use crate::rules::{require::PathRequireMode, Rule};
use insta::assert_json_snapshot;
fn new_rule() -> Bundler {
Bundler::new(
Parser::default(),
BundleRequireMode::default(),
std::iter::empty(),
)
}
fn new_rule_with_require_mode(mode: impl Into<BundleRequireMode>) -> Bundler {
Bundler::new(Parser::default(), mode.into(), std::iter::empty())
}
// the bundler rule should only be used internally by darklua
// so there is no need for it to serialize properly. The
// implementation exist just make sure it does not panic
#[test]
fn serialize_default_rule() {
let rule: Box<dyn Rule> = Box::new(new_rule());
assert_json_snapshot!("default_bundler", rule);
}
#[test]
fn serialize_path_require_mode_with_custom_module_folder_name() {
let rule: Box<dyn Rule> =
Box::new(new_rule_with_require_mode(PathRequireMode::new("__init__")));
assert_json_snapshot!("default_bundler", rule);
}
#[test]
fn serialize_path_require_mode_with_custom_module_folder_name_and_modules_identifier() {
let rule: Box<dyn Rule> = Box::new(
new_rule_with_require_mode(PathRequireMode::new("__init__"))
.with_modules_identifier("_CUSTOM_VAR"),
);
assert_json_snapshot!("default_bundler", rule);
}
#[test]
fn serialize_with_custom_modules_identifier() {
let rule: Box<dyn Rule> = Box::new(new_rule().with_modules_identifier("_CUSTOM_VAR"));
assert_json_snapshot!("default_bundler", rule);
}
}