use crate::ast::AstDialectVersion;
use crate::hir::{HirProtoRef, ProtoLineRange, ProtoSignature};
use std::str::FromStr;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GeneratedChunk {
pub dialect: AstDialectVersion,
pub source: String,
pub warnings: Vec<String>,
}
impl Default for GeneratedChunk {
fn default() -> Self {
Self {
dialect: AstDialectVersion::Lua51,
source: String::new(),
warnings: Vec::new(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GenerateOptions {
pub mode: GenerateMode,
pub indent_width: usize,
pub max_line_length: usize,
pub quote_style: QuoteStyle,
pub table_style: TableStyle,
pub conservative_output: bool,
pub comment: bool,
}
impl Default for GenerateOptions {
fn default() -> Self {
Self {
mode: GenerateMode::Strict,
indent_width: 4,
max_line_length: 100,
quote_style: QuoteStyle::MinEscape,
table_style: TableStyle::Balanced,
conservative_output: true,
comment: true,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GenerateCommentMetadata {
pub chunk: GenerateChunkCommentMetadata,
pub functions: Vec<GenerateFunctionCommentMetadata>,
}
impl GenerateCommentMetadata {
pub fn function(&self, function: HirProtoRef) -> Option<&GenerateFunctionCommentMetadata> {
self.functions.get(function.index())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GenerateChunkCommentMetadata {
pub file_name: Option<String>,
pub encoding: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GenerateFunctionCommentMetadata {
pub function: HirProtoRef,
pub source: Option<String>,
pub line_range: ProtoLineRange,
pub signature: ProtoSignature,
pub local_count: usize,
pub upvalue_count: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum GenerateMode {
#[default]
Strict,
BestEffort,
Permissive,
}
impl GenerateMode {
pub const fn as_str(self) -> &'static str {
match self {
Self::Strict => "strict",
Self::BestEffort => "best-effort",
Self::Permissive => "permissive",
}
}
}
impl FromStr for GenerateMode {
type Err = ();
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"strict" => Ok(Self::Strict),
"best-effort" | "best_effort" | "besteffort" => Ok(Self::BestEffort),
"permissive" => Ok(Self::Permissive),
_ => Err(()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum QuoteStyle {
PreferDouble,
PreferSingle,
#[default]
MinEscape,
}
impl QuoteStyle {
pub const fn as_str(self) -> &'static str {
match self {
Self::PreferDouble => "prefer-double",
Self::PreferSingle => "prefer-single",
Self::MinEscape => "min-escape",
}
}
}
impl FromStr for QuoteStyle {
type Err = ();
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"prefer-double" => Ok(Self::PreferDouble),
"prefer-single" => Ok(Self::PreferSingle),
"min-escape" => Ok(Self::MinEscape),
_ => Err(()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TableStyle {
Compact,
#[default]
Balanced,
Expanded,
}
impl TableStyle {
pub const fn as_str(self) -> &'static str {
match self {
Self::Compact => "compact",
Self::Balanced => "balanced",
Self::Expanded => "expanded",
}
}
}
impl FromStr for TableStyle {
type Err = ();
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"compact" => Ok(Self::Compact),
"balanced" => Ok(Self::Balanced),
"expanded" => Ok(Self::Expanded),
_ => Err(()),
}
}
}