openstranded_s2mod_tool/registry.rs
1// openstranded-s2mod-tool — convert Stranded II mods to .s2mod format
2// Copyright (C) 2025 openstranded-s2mod-tool contributors
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17//! Registry key derivation and registration for `.ron` files in the manifest.
18
19use std::collections::HashMap;
20use std::path::Path;
21
22/// Derive the registry key from a .ron filename.
23///
24/// items_edible.ron → "items"
25/// combinations_basic.ron → "combinations"
26/// game.ron → "game"
27/// vehicles_flying.ron → "vehicles"
28/// strings.ron → "strings"
29pub fn registry_key_from_filename(stem: &str) -> String {
30 // Use the first token before '_' as the key; if there's no '_', use the whole stem.
31 stem.split('_').next().unwrap_or(stem).to_string()
32}
33
34/// Register a .ron file path in the registry under the key derived from its filename.
35pub fn register_ron(ron_rel: &Path, registry: &mut HashMap<String, Vec<String>>) {
36 let rel_str = ron_rel.to_string_lossy().to_string();
37 let stem = ron_rel
38 .file_stem()
39 .and_then(|s| s.to_str())
40 .unwrap_or("unknown");
41 let key = registry_key_from_filename(stem);
42 registry.entry(key).or_default().push(rel_str);
43}