datex-core 0.0.14

The DATEX Core Rust implementation
Documentation
use datex_macros_internal::Datex;

#[derive(Datex, Debug, Clone, Default)]
#[datex(structural)]
pub struct DecompileOptions {
    pub formatting_options: FormattingOptions,
    /// display slots with generated variable names
    pub resolve_slots: bool,
}

impl DecompileOptions {
    pub fn json_compat() -> Self {
        DecompileOptions {
            formatting_options: FormattingOptions::json_compat(),
            ..DecompileOptions::default()
        }
    }

    /// Formats and colorizes the output
    pub fn colorized() -> Self {
        DecompileOptions {
            formatting_options: FormattingOptions::colorized(),
            ..DecompileOptions::default()
        }
    }

    /// Formats the output with indentation and newlines
    pub fn pretty() -> Self {
        DecompileOptions {
            formatting_options: FormattingOptions::pretty(),
            ..DecompileOptions::default()
        }
    }

    /// Formats and colorizes the output with indentation and newlines
    pub fn colorized_pretty() -> Self {
        DecompileOptions {
            formatting_options: FormattingOptions {
                colorized: true,
                mode: FormattingMode::pretty(),
                ..FormattingOptions::default()
            },
            ..DecompileOptions::default()
        }
    }

    /// No extra spaces or newlines, no colorization
    pub fn compact() -> Self {
        DecompileOptions {
            formatting_options: FormattingOptions::compact(),
            ..DecompileOptions::default()
        }
    }
}

#[derive(Datex, Clone, Debug, Copy, Default)]
#[datex(structural)]
pub enum IndentType {
    #[default]
    Spaces,
    Tabs,
}

#[derive(Datex, Debug, Clone, Default)]
#[datex(structural)]
pub enum FormattingMode {
    /// compact formatting, no unnecessary spaces or newlines
    #[default]
    Compact,
    /// pretty formatting with indentation and newlines
    Pretty {
        indent: usize,
        indent_type: IndentType,
    },
}

impl FormattingMode {
    pub fn pretty() -> Self {
        FormattingMode::Pretty {
            indent: 4,
            indent_type: IndentType::Spaces,
        }
    }

    pub fn compact() -> Self {
        FormattingMode::Compact
    }

    pub fn pretty_with_indent(indent: usize, indent_type: IndentType) -> Self {
        FormattingMode::Pretty {
            indent,
            indent_type,
        }
    }
}

#[derive(Datex, Debug, Clone, Default)]
#[datex(structural)]
pub struct FormattingOptions {
    pub mode: FormattingMode,
    pub json_compat: bool,
    pub colorized: bool,
}

impl FormattingOptions {
    pub fn colorized() -> Self {
        FormattingOptions {
            colorized: true,
            ..FormattingOptions::default()
        }
    }

    pub fn json_compat() -> Self {
        FormattingOptions {
            json_compat: true,
            ..FormattingOptions::default()
        }
    }

    pub fn compact() -> Self {
        FormattingOptions {
            mode: FormattingMode::Compact,
            ..FormattingOptions::default()
        }
    }

    pub fn pretty() -> Self {
        FormattingOptions {
            mode: FormattingMode::pretty(),
            ..FormattingOptions::default()
        }
    }

    pub fn pretty_with_indent(indent: usize, indent_type: IndentType) -> Self {
        FormattingOptions {
            mode: FormattingMode::pretty_with_indent(indent, indent_type),
            ..FormattingOptions::default()
        }
    }
}