factorio-ir 0.1.2

Intermediate representation for factorio-rs Rust-to-Lua transpilation
Documentation
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LocaleEntry {
    /// Factorio category name (e.g. `mod-setting-name`), if any.
    pub category: Option<String>,
    /// Locale key (e.g. `msr-casual-mode`).
    pub key: String,
    /// Translated / English template string.
    pub value: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LocaleFile {
    /// Language code (`en`, `de`, ...).
    pub lang: String,
    /// File stem written under `locale/<lang>/` (without `.cfg`).
    pub file: String,
    pub entries: Vec<LocaleEntry>,
}

impl LocaleFile {
    /// Serialize to Factorio's `.cfg` format.
    #[must_use]
    pub fn to_cfg(&self) -> String {
        let mut output = String::new();
        let mut current_category: Option<&str> = None;
        let mut first_section = true;

        for entry in &self.entries {
            let category = entry.category.as_deref();
            if category != current_category {
                if !first_section {
                    output.push('\n');
                }
                if let Some(name) = category {
                    output.push('[');
                    output.push_str(name);
                    output.push_str("]\n");
                }
                current_category = category;
                first_section = false;
            }

            output.push_str(&entry.key);
            output.push('=');
            output.push_str(&entry.value);
            output.push('\n');
        }

        output
    }
}