Skip to content

Commit 2054514

Browse files
Refactor ParserError into opaque struct (#71)
1 parent 6b7652a commit 2054514

File tree

9 files changed

+43
-22
lines changed

9 files changed

+43
-22
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
* refactor ParserError into an opaque struct (instead of an enum) ([#71](https://github.com/seaofvoices/darklua/pull/71))
56
* refactor darklua frontend ([#69](https://github.com/seaofvoices/darklua/pull/69)):
67
* the `--config-path` argument of the `minify` command was removed
78
* the configuration file does not accept the `column_span` field anymore (use the [`generator` field](https://darklua.com/docs/generators/) instead)

src/frontend/error.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ pub type DarkluaResult<T> = Result<T, DarkluaError>;
5353

5454
#[derive(Debug, Clone)]
5555
pub struct DarkluaError {
56-
kind: ErrorKind,
56+
kind: Box<ErrorKind>,
5757
context: Option<Cow<'static, str>>,
5858
}
5959

6060
impl DarkluaError {
6161
fn new(kind: ErrorKind) -> Self {
6262
Self {
63-
kind,
63+
kind: kind.into(),
6464
context: None,
6565
}
6666
}
@@ -184,7 +184,7 @@ impl From<ResourceError> for DarkluaError {
184184

185185
impl Display for DarkluaError {
186186
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
187-
match &self.kind {
187+
match &*self.kind {
188188
ErrorKind::Parser { path, error } => {
189189
write!(f, "unable to parse `{}`: {}", path.display(), error)?;
190190
}

src/frontend/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ fn private_process(
8383
)
8484
}
8585
} else {
86+
let input = options.input().to_path_buf();
8687
worker.process(
8788
resources
88-
.collect_work(options.input().to_path_buf())
89+
.collect_work(input)
8990
.map(|source| Ok(WorkItem::new_in_place(source))),
9091
options,
9192
)

src/generator/token_based.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ impl<'a> TokenBasedLuaGenerator<'a> {
724724
end: Token::from_content("end"),
725725
parameter_commas: intersect_with_token(
726726
comma_token(),
727-
function.parameters_count() + if function.is_variadic() { 1 } else { 0 },
727+
function.parameters_count() + usize::from(function.is_variadic()),
728728
),
729729
variable_arguments: if function.is_variadic() {
730730
Some(Token::from_content("..."))
@@ -776,7 +776,7 @@ impl<'a> TokenBasedLuaGenerator<'a> {
776776
end: Token::from_content("end"),
777777
parameter_commas: intersect_with_token(
778778
comma_token(),
779-
function.parameters_count() + if function.is_variadic() { 1 } else { 0 },
779+
function.parameters_count() + usize::from(function.is_variadic()),
780780
),
781781
variable_arguments: if function.is_variadic() {
782782
Some(Token::from_content("..."))
@@ -820,7 +820,7 @@ impl<'a> TokenBasedLuaGenerator<'a> {
820820
end: Token::from_content("end"),
821821
parameter_commas: intersect_with_token(
822822
comma_token(),
823-
function.parameters_count() + if function.is_variadic() { 1 } else { 0 },
823+
function.parameters_count() + usize::from(function.is_variadic()),
824824
),
825825
variable_arguments: if function.is_variadic() {
826826
Some(Token::from_content("..."))

src/generator/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ pub fn write_string(string: &StringExpression) -> String {
266266
}
267267

268268
fn write_long_bracket(value: &str) -> String {
269-
let mut i = if value.ends_with(']') { 1 } else { 0 };
269+
let mut i: usize = value.ends_with(']').into();
270270
let mut equals = "=".repeat(i);
271271
loop {
272272
if !value.contains(&format!("]{}]", equals)) {

src/nodes/expressions/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ fn read_number(
246246
let mut amount = first_digit
247247
.map(|char| char.to_digit(radix).unwrap())
248248
.unwrap_or(0);
249-
let mut iteration_count = if first_digit.is_some() { 1 } else { 0 };
249+
let mut iteration_count: usize = first_digit.is_some().into();
250250

251251
while let Some(next_digit) = chars.peek().cloned().filter(filter) {
252252
chars.next();

src/parser.rs

+26-7
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ pub struct Parser {
8383
impl Parser {
8484
pub fn parse(&self, code: &str) -> Result<Block, ParserError> {
8585
full_moon::parse(code)
86-
.map_err(ParserError::Parsing)
87-
.and_then(|ast| self.convert_ast(ast).map_err(ParserError::Converting))
86+
.map_err(ParserError::parsing)
87+
.and_then(|ast| self.convert_ast(ast).map_err(ParserError::converting))
8888
}
8989

9090
pub fn preserve_tokens(mut self) -> Self {
@@ -1048,7 +1048,7 @@ impl Parser {
10481048
}
10491049

10501050
#[derive(Clone, Debug)]
1051-
pub enum ConvertError {
1051+
enum ConvertError {
10521052
Statement {
10531053
statement: String,
10541054
},
@@ -1134,16 +1134,35 @@ impl fmt::Display for ConvertError {
11341134
}
11351135

11361136
#[derive(Clone, Debug)]
1137-
pub enum ParserError {
1137+
enum ParserErrorKind {
11381138
Parsing(full_moon::Error),
11391139
Converting(ConvertError),
11401140
}
11411141

1142+
#[derive(Clone, Debug)]
1143+
pub struct ParserError {
1144+
kind: Box<ParserErrorKind>,
1145+
}
1146+
1147+
impl ParserError {
1148+
fn parsing(err: full_moon::Error) -> Self {
1149+
Self {
1150+
kind: ParserErrorKind::Parsing(err).into(),
1151+
}
1152+
}
1153+
1154+
fn converting(err: ConvertError) -> Self {
1155+
Self {
1156+
kind: ParserErrorKind::Converting(err).into(),
1157+
}
1158+
}
1159+
}
1160+
11421161
impl fmt::Display for ParserError {
11431162
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1144-
match self {
1145-
Self::Parsing(err) => write!(f, "{}", err),
1146-
Self::Converting(err) => write!(f, "{}", err),
1163+
match &*self.kind {
1164+
ParserErrorKind::Parsing(err) => write!(f, "{}", err),
1165+
ParserErrorKind::Converting(err) => write!(f, "{}", err),
11471166
}
11481167
}
11491168
}

src/rules/filter_early_return.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::verify_no_rule_properties;
1010
struct Processor {}
1111

1212
impl Processor {
13-
fn search_remove_after(&self, block: &Block) -> Option<usize> {
13+
fn search_remove_after(block: &Block) -> Option<usize> {
1414
block
1515
.iter_statements()
1616
.enumerate()
@@ -24,7 +24,7 @@ impl Processor {
2424
LastStatement::Return(_) => Some(i),
2525
}
2626
} else {
27-
self.search_remove_after(inner_block).map(|_| i)
27+
Self::search_remove_after(inner_block).map(|_| i)
2828
}
2929
}
3030
Statement::Assign(_)
@@ -44,7 +44,7 @@ impl Processor {
4444

4545
impl NodeProcessor for Processor {
4646
fn process_block(&mut self, block: &mut Block) {
47-
if let Some(remove_after) = self.search_remove_after(block) {
47+
if let Some(remove_after) = Self::search_remove_after(block) {
4848
block.take_last_statement();
4949
block.truncate(remove_after + 1);
5050
}

tests/cli.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl Context {
4444
pub fn expect_file<P: AsRef<Path>>(&self, file_path: P) -> &Self {
4545
let file_path = file_path.as_ref();
4646
if !file_path.exists() || !file_path.is_file() {
47-
self.debug_working_directory(self.working_directory.path());
47+
Self::debug_working_directory(self.working_directory.path());
4848
}
4949
assert!(
5050
file_path.exists(),
@@ -138,12 +138,12 @@ impl Context {
138138
string
139139
}
140140

141-
fn debug_working_directory(&self, root: &Path) {
141+
fn debug_working_directory(root: &Path) {
142142
eprintln!("{}", root.display());
143143
for entry in root.read_dir().unwrap() {
144144
let entry = entry.unwrap().path();
145145
if entry.is_dir() {
146-
self.debug_working_directory(&entry);
146+
Self::debug_working_directory(&entry);
147147
} else {
148148
eprintln!("{}", entry.display());
149149
}

0 commit comments

Comments
 (0)