-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
mod.rs
257 lines (219 loc) · 7.26 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
mod instance_path;
mod roblox_index_style;
mod roblox_require_mode;
mod rojo_sourcemap;
use serde::{Deserialize, Serialize};
use crate::frontend::DarkluaResult;
use crate::nodes::{Arguments, Block, FunctionCall};
use crate::process::{DefaultVisitor, IdentifierTracker, NodeProcessor, NodeVisitor};
use crate::rules::require::{is_require_call, PathRequireMode};
use crate::rules::{Context, RuleConfiguration, RuleConfigurationError, RuleProperties};
use crate::Resources;
use instance_path::InstancePath;
pub use roblox_index_style::RobloxIndexStyle;
pub use roblox_require_mode::RobloxRequireMode;
use super::{verify_required_properties, Rule, RuleProcessResult};
use std::ffi::OsStr;
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
use std::str::FromStr;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields, rename_all = "kebab-case", tag = "name")]
pub enum RequireMode {
Path(PathRequireMode),
Roblox(RobloxRequireMode),
}
impl RequireMode {
fn find_require(
&self,
call: &FunctionCall,
context: &Context,
) -> DarkluaResult<Option<PathBuf>> {
match self {
RequireMode::Path(path_mode) => path_mode.find_require(call, context),
RequireMode::Roblox(roblox_mode) => roblox_mode.find_require(call, context),
}
}
fn generate_require(
&self,
path: &Path,
current_mode: &Self,
context: &Context,
) -> DarkluaResult<Option<Arguments>> {
match self {
RequireMode::Path(path_mode) => path_mode.generate_require(path, current_mode, context),
RequireMode::Roblox(roblox_mode) => {
roblox_mode.generate_require(path, current_mode, context)
}
}
}
fn is_module_folder_name(&self, path: &Path) -> bool {
match self {
RequireMode::Path(path_mode) => path_mode.is_module_folder_name(path),
RequireMode::Roblox(_roblox_mode) => {
matches!(path.file_stem().and_then(OsStr::to_str), Some("init"))
}
}
}
fn initialize(&mut self, resources: &Resources) -> DarkluaResult<()> {
match self {
RequireMode::Roblox(roblox_mode) => roblox_mode.initialize(resources),
RequireMode::Path(_) => Ok(()),
}
}
}
impl FromStr for RequireMode {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"path" => Self::Path(Default::default()),
"roblox" => Self::Roblox(Default::default()),
_ => return Err(format!("invalid require mode name `{}`", s)),
})
}
}
#[derive(Debug, Clone)]
struct RequireConverter<'a> {
identifier_tracker: IdentifierTracker,
current: RequireMode,
target: RequireMode,
context: &'a Context<'a, 'a, 'a>,
}
impl<'a> Deref for RequireConverter<'a> {
type Target = IdentifierTracker;
fn deref(&self) -> &Self::Target {
&self.identifier_tracker
}
}
impl<'a> DerefMut for RequireConverter<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.identifier_tracker
}
}
impl<'a> RequireConverter<'a> {
fn new(current: RequireMode, target: RequireMode, context: &'a Context) -> Self {
Self {
identifier_tracker: IdentifierTracker::new(),
current,
target,
context,
}
}
fn try_require_conversion(&mut self, call: &mut FunctionCall) -> DarkluaResult<()> {
if let Some(require_path) = self.current.find_require(call, self.context)? {
log::trace!("found require path `{}`", require_path.display());
if let Some(new_arguments) =
self.target
.generate_require(&require_path, &self.current, self.context)?
{
call.set_arguments(new_arguments);
}
}
Ok(())
}
}
impl<'a> NodeProcessor for RequireConverter<'a> {
fn process_function_call(&mut self, call: &mut FunctionCall) {
if is_require_call(call, self) {
match self.try_require_conversion(call) {
Ok(()) => {}
Err(err) => {
log::warn!("{}", err);
}
}
}
}
}
pub const CONVERT_REQUIRE_RULE_NAME: &str = "convert_require";
/// A rule that converts require calls between environments
#[derive(Debug, PartialEq, Eq)]
pub struct ConvertRequire {
current: RequireMode,
target: RequireMode,
}
impl Default for ConvertRequire {
fn default() -> Self {
Self {
current: RequireMode::Path(Default::default()),
target: RequireMode::Roblox(Default::default()),
}
}
}
impl Rule for ConvertRequire {
fn process(&self, block: &mut Block, context: &Context) -> RuleProcessResult {
let mut current_mode = self.current.clone();
current_mode
.initialize(context.resources())
.map_err(|err| err.to_string())?;
let mut target_mode = self.target.clone();
target_mode
.initialize(context.resources())
.map_err(|err| err.to_string())?;
let mut processor = RequireConverter::new(current_mode, target_mode, context);
DefaultVisitor::visit_block(block, &mut processor);
Ok(())
}
}
impl RuleConfiguration for ConvertRequire {
fn configure(&mut self, properties: RuleProperties) -> Result<(), RuleConfigurationError> {
verify_required_properties(&properties, &["current", "target"])?;
for (key, value) in properties {
match key.as_str() {
"current" => {
self.current = value.expect_require_mode(&key)?;
}
"target" => {
self.target = value.expect_require_mode(&key)?;
}
_ => return Err(RuleConfigurationError::UnexpectedProperty(key)),
}
}
Ok(())
}
fn get_name(&self) -> &'static str {
CONVERT_REQUIRE_RULE_NAME
}
fn serialize_to_properties(&self) -> RuleProperties {
RuleProperties::new()
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::rules::Rule;
use insta::assert_json_snapshot;
fn new_rule() -> ConvertRequire {
ConvertRequire::default()
}
#[test]
fn serialize_default_rule() {
let rule: Box<dyn Rule> = Box::new(new_rule());
assert_json_snapshot!("default_convert_require", rule);
}
#[test]
fn configure_with_invalid_require_mode_error() {
let result = json5::from_str::<Box<dyn Rule>>(
r#"{
rule: 'convert_require',
current: 'path',
target: 'rblox',
}"#,
);
pretty_assertions::assert_eq!(
result.unwrap_err().to_string(),
"unexpected value for field 'target': invalid require mode name `rblox`"
);
}
#[test]
fn configure_with_extra_field_error() {
let result = json5::from_str::<Box<dyn Rule>>(
r#"{
rule: 'convert_require',
current: 'path',
target: 'path',
prop: "something",
}"#,
);
pretty_assertions::assert_eq!(result.unwrap_err().to_string(), "unexpected field 'prop'");
}
}