inf2ron 0.2.0

Parse Stranded II .inf definition files (items, objects, units, buildings, combinations) and output RON format
Documentation
// inf2ron – parse Stranded II .inf definition files to RON
// Copyright (C) 2025  inf2ron contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Generic data types for .inf file parsing.
//!
//! The parser is completely format-agnostic — any `key=value` lines
//! and any `xxx=start`/`xxx=end` blocks are accepted.
//! No knowledge of specific fields or block names is required.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

/// One entity from an `.inf` file.
///
/// Top-level `key=value` lines grouped by `id=` markers.
/// `id=N` starts a new entry; all subsequent `key=value` lines
/// belong to it until the next `id=` or end of file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InfEntry {
    /// Source line number (not serialized)
    #[serde(skip)]
    pub line: usize,
    /// All `key=value` fields (supports multivalue keys)
    pub fields: HashMap<String, Vec<String>>,
    /// All `xxx=start`…`xxx=end` blocks attached to this entry.
    ///
    /// A block is **any** `keyword=start` / `keyword=end` pair.
    /// Contents are classified automatically (see [`BlockContent`]).
    ///
    /// Multiple blocks with the same name are stored in order
    /// (e.g. `"script" → [Block₁, Block₂]`).
    #[serde(skip_serializing_if = "HashMap::is_empty")]
    pub blocks: HashMap<String, Vec<Block>>,
}

/// A single named block (`xxx=start` … `xxx=end`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Block {
    /// The block name (from `xxx=start`)
    pub name: String,
    pub content: BlockContent,
}

/// Classified content of a block.
///
/// The parser decides the type by inspecting the raw lines *after*
/// extracting nested sub-blocks:
///
/// | Condition | Result |
/// |-----------|--------|
/// | All remaining lines parse as `key=value` | `Structured` |
/// | Any line cannot be parsed as `key=value` | `Text` (preserves everything) |
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum BlockContent {
    /// Free-form text content (s2s code, descriptions, etc.).
    ///
    /// The content includes all raw lines joined with newlines,
    /// preserving original whitespace. Nested sub-blocks inside
    /// a `Text` block are **not** extracted — the whole content
    /// is kept as a single string.
    Text(String),
    /// Key-value structured content (crafting recipes, config blocks, etc.).
    Structured {
        fields: HashMap<String, Vec<String>>,
        #[serde(skip_serializing_if = "HashMap::is_empty")]
        blocks: HashMap<String, Vec<Block>>,
    },
}

/// Result of parsing a single `.inf` file (file-level wrapper).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InfFile {
    pub path: String,
    pub entries: Vec<InfEntry>,
}