Skip to content

Commit 3fa7d71

Browse files
Add readable format
1 parent bc9e99c commit 3fa7d71

File tree

217 files changed

+3724
-2249
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

217 files changed

+3724
-2249
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "darklua"
3-
version = "0.3.6"
3+
version = "0.4.0"
44
authors = ["jeparlefrancais <jeparlefrancais21@gmail.com>"]
55
edition = "2018"
66
readme = "README.md"

bin/test-commands.lua

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ local projects = {
191191
local testSuite = {
192192
DarkluaTest.new('minify', 'minify $input $output'),
193193
DarkluaTest.new('default-process', 'process $input $output'),
194+
DarkluaTest.new('default-process-readable', 'process $input $output --format readable'),
194195
DarkluaTest.new('rename', 'process $input $output', {
195196
process = {{
196197
rule = 'rename_variables',

src/cli/minify.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::cli::utils::{
77
FileProcessing,
88
};
99

10-
use darklua_core::{LuaGenerator, ToLua, Parser};
10+
use darklua_core::{generator::{DenseLuaGenerator, LuaGenerator}, Parser};
1111
use std::path::PathBuf;
1212
use std::fs;
1313
use std::process;
@@ -46,8 +46,8 @@ fn process(file: &FileProcessing, options: &Options, global: &GlobalOptions) ->
4646
let block = parser.parse(&input)
4747
.map_err(|parser_error| CliError::Parser(source.clone(), parser_error))?;
4848

49-
let mut generator = LuaGenerator::new(config.column_span);
50-
block.to_lua(&mut generator);
49+
let mut generator = DenseLuaGenerator::new(config.column_span);
50+
generator.write_block(&block);
5151
let minified = generator.into_string();
5252

5353
write_file(&output, &minified)

src/cli/process.rs

+49-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,55 @@ use crate::cli::utils::{
77
FileProcessing,
88
};
99

10-
use darklua_core::{LuaGenerator, ToLua, Parser};
10+
use darklua_core::{
11+
nodes::Block,
12+
generator::{LuaGenerator, DenseLuaGenerator, ReadableLuaGenerator},
13+
Parser,
14+
};
1115
use std::path::PathBuf;
1216
use std::fs;
1317
use std::process;
18+
use std::str::FromStr;
1419
use structopt::StructOpt;
1520

21+
#[derive(Debug, Copy, Clone)]
22+
pub enum LuaFormat {
23+
Dense,
24+
Readable,
25+
}
26+
27+
impl LuaFormat {
28+
pub fn generate(&self, config: &Config, block: &Block) -> String {
29+
match self {
30+
Self::Dense => {
31+
let mut generator = DenseLuaGenerator::new(config.column_span);
32+
generator.write_block(block);
33+
generator.into_string()
34+
}
35+
Self::Readable => {
36+
let mut generator = ReadableLuaGenerator::new(config.column_span);
37+
generator.write_block(block);
38+
generator.into_string()
39+
}
40+
}
41+
}
42+
}
43+
44+
impl FromStr for LuaFormat {
45+
type Err = String;
46+
47+
fn from_str(format: &str) -> Result<Self, Self::Err> {
48+
match format {
49+
"dense" => Ok(Self::Dense),
50+
"readable" => Ok(Self::Readable),
51+
_ => Err(format!(
52+
"format '{}' does not exist! (possible options are: 'dense' or 'readable'",
53+
format
54+
)),
55+
}
56+
}
57+
}
58+
1659
#[derive(Debug, StructOpt)]
1760
pub struct Options {
1861
/// Path to the lua file to minify.
@@ -24,6 +67,9 @@ pub struct Options {
2467
/// Choose a specific configuration file.
2568
#[structopt(long, short)]
2669
pub config_path: Option<PathBuf>,
70+
/// Choose how Lua code is formatted ('dense' or 'readable').
71+
#[structopt(long, default_value = "dense")]
72+
pub format: LuaFormat,
2773
}
2874

2975
fn process(file: &FileProcessing, options: &Options, global: &GlobalOptions) -> Result<(), CliError> {
@@ -46,11 +92,9 @@ fn process(file: &FileProcessing, options: &Options, global: &GlobalOptions) ->
4692

4793
config.process.iter().for_each(|rule| rule.process(&mut block));
4894

49-
let mut generator = LuaGenerator::new(config.column_span);
50-
block.to_lua(&mut generator);
51-
let minified = generator.into_string();
95+
let lua_code = options.format.generate(&config, &block);
5296

53-
write_file(&output, &minified)
97+
write_file(&output, &lua_code)
5498
.map_err(|io_error| CliError::OutputFile(output.clone(), format!("{}", io_error)))?;
5599

56100
if global.verbose > 0 {

0 commit comments

Comments
 (0)