s2mod-packer 0.1.0

Pack/unpack .s2mod Content Pack archives for OpenStranded
Documentation
// s2mod-packer – pack/unpack .s2mod Content Pack archives for OpenStranded
// Copyright (C) 2025  s2mod-packer 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/>.

//! Content Pack manifest types.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

/// Content Pack manifest — describes what a .s2mod provides and requires.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContentPackManifest {
    pub pack: PackInfo,
    #[serde(default)]
    pub registry: Registry,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dependencies: Option<Dependencies>,
}

/// Pack metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PackInfo {
    pub id: String,
    pub name: String,
    pub version: String,
    #[serde(default)]
    pub author: String,
    #[serde(default)]
    pub description: String,
}

/// Registry listing which .ron files contain definitions for each domain.
///
/// Domain name is derived from the .ron filename: the first token before `_`,
/// or the full stem if no `_` (e.g. `items_edible.ron` → `"items"`,
/// `game.ron` → `"game"`).
///
/// Serialises as a flat TOML table under `[registry]` — every key is a domain
/// name, every value is an array of .ron file paths (relative to pack root).
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Registry {
    /// Domain → .ron file paths.
    #[serde(flatten)]
    pub domains: HashMap<String, Vec<String>>,
}

/// Pack dependencies
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Dependencies {
    #[serde(default)]
    pub mods: Vec<String>,
}

impl ContentPackManifest {
    /// Create a minimal manifest with autofilled defaults
    pub fn new(id: &str, name: &str) -> Self {
        Self {
            pack: PackInfo {
                id: id.to_string(),
                name: name.to_string(),
                version: "1.0".to_string(),
                author: String::new(),
                description: String::new(),
            },
            registry: Registry::default(),
            dependencies: None,
        }
    }

    /// Deserialize from TOML string
    pub fn from_toml(input: &str) -> Result<Self, toml::de::Error> {
        toml::from_str(input)
    }

    /// Serialize to TOML string
    pub fn to_toml(&self) -> Result<String, toml::ser::Error> {
        toml::to_string_pretty(self)
    }
}