use std::fmt;
use crate::generate::GenerateOptions;
use crate::naming::{NamingMode, NamingOptions};
use crate::parser::{ParseMode, ParseOptions, StringDecodeMode, StringEncoding};
use crate::readability::ReadabilityOptions;
use super::debug::DebugOptions;
use super::state::DecompileStage;
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
pub enum DecompileDialect {
#[default]
Lua51,
Lua52,
Lua53,
Lua54,
Lua55,
Luajit,
Luau,
}
impl DecompileDialect {
pub const fn label(self) -> &'static str {
match self {
Self::Lua51 => "lua5.1",
Self::Lua52 => "lua5.2",
Self::Lua53 => "lua5.3",
Self::Lua54 => "lua5.4",
Self::Lua55 => "lua5.5",
Self::Luajit => "luajit",
Self::Luau => "luau",
}
}
pub fn parse(value: &str) -> Option<Self> {
match value {
"lua5.1" | "lua51" => Some(Self::Lua51),
"lua5.2" | "lua52" => Some(Self::Lua52),
"lua5.3" | "lua53" => Some(Self::Lua53),
"lua5.4" | "lua54" => Some(Self::Lua54),
"lua5.5" | "lua55" => Some(Self::Lua55),
"luajit" => Some(Self::Luajit),
"luau" => Some(Self::Luau),
_ => None,
}
}
}
impl fmt::Display for DecompileDialect {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.label())
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct DecompileOptions {
pub dialect: DecompileDialect,
pub parse: ParseOptions,
pub target_stage: DecompileStage,
pub debug: DebugOptions,
pub readability: ReadabilityOptions,
pub naming: NamingOptions,
pub generate: GenerateOptions,
}
impl Default for DecompileOptions {
fn default() -> Self {
Self {
dialect: DecompileDialect::Lua51,
parse: ParseOptions {
mode: ParseMode::Permissive,
string_encoding: StringEncoding::Utf8,
string_decode_mode: StringDecodeMode::Strict,
},
target_stage: DecompileStage::Generate,
debug: DebugOptions::default(),
readability: ReadabilityOptions {
return_inline_max_complexity: 10,
index_inline_max_complexity: 10,
args_inline_max_complexity: 6,
access_base_inline_max_complexity: 5,
},
naming: NamingOptions {
mode: NamingMode::DebugLike,
debug_like_include_function: true,
},
generate: GenerateOptions::default(),
}
}
}
impl DecompileOptions {
pub(crate) fn normalized(mut self) -> Self {
if self.debug.enable && self.debug.output_stages.is_empty() && !self.debug.timing {
self.debug.output_stages.push(self.target_stage);
}
self
}
}