From 6a60e29fd707951cea82461d9d0534d9fdf8cc10 Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Wed, 11 Mar 2026 23:49:08 +0100 Subject: [PATCH] add save function to root_config --- src/types/game.rs | 4 ++-- src/types/mod_config.rs | 11 +++++++++-- src/types/root_config.rs | 26 ++++++++++++++++++-------- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/types/game.rs b/src/types/game.rs index b10f1d6..0ce1675 100644 --- a/src/types/game.rs +++ b/src/types/game.rs @@ -3,12 +3,12 @@ use std::{ path::{Path, PathBuf}, }; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use crate::{types::link::Link, utils::walk_all_files}; /// Available game -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct Game { path: PathBuf, } diff --git a/src/types/mod_config.rs b/src/types/mod_config.rs index 230dc72..1f1ba9e 100644 --- a/src/types/mod_config.rs +++ b/src/types/mod_config.rs @@ -1,11 +1,12 @@ use std::path::{Path, PathBuf}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; /// Config for an available mod -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct ModConfig { /// ID of the mod + #[serde(skip)] id: String, /// Relative to the mod_location from root config @@ -13,10 +14,12 @@ pub struct ModConfig { /// If the files should be included on the root #[serde(default)] + #[serde(skip_serializing_if = "is_false")] root_mod: bool, /// Globs of what files to ignore #[serde(default)] + #[serde(skip_serializing_if = "Vec::is_empty")] ignore: Vec, } @@ -47,3 +50,7 @@ impl ModConfig { &self.ignore } } + +fn is_false(b: &bool) -> bool { + !b +} diff --git a/src/types/root_config.rs b/src/types/root_config.rs index 430c35c..845d775 100644 --- a/src/types/root_config.rs +++ b/src/types/root_config.rs @@ -1,21 +1,20 @@ use std::{ collections::HashMap, fs::{self, read_to_string}, + io::Write, path::{Path, PathBuf}, }; use log::debug; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use crate::types::{ConfigReadWriteError, ModConfig, game::Game, modded_instance::ModdedInstance}; -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct RootConfig { - /// Available games #[serde(default)] games: HashMap, - /// Where all mods are stored mod_location: PathBuf, download_location: Option, @@ -27,7 +26,7 @@ pub struct RootConfig { /// All available mods #[serde(default)] - mods: Vec, + mods: HashMap, #[serde(skip)] self_path: PathBuf, @@ -75,12 +74,23 @@ impl RootConfig { Ok(config) } + pub fn save_to_file(&self) -> Result<(), ConfigReadWriteError> { + let content = toml::to_string_pretty(self)?; + let mut file = fs::File::create(&self.self_path)?; + write!(file, "{}", content)?; + Ok(()) + } + pub fn game_by_id(&self, id: &str) -> Option<&Game> { self.games.get(id) } - pub fn mod_by_id(&self, id: &str) -> Option<&ModConfig> { - self.mods.iter().find(|e| e.id() == id) + pub fn mod_by_id(&self, id: &str) -> Option { + self.mods.get(id).map(|e| e.clone().add_id(id)) + } + + pub fn add_mod(&mut self, new_mod: &ModConfig) { + self.mods.insert(new_mod.id().to_owned(), new_mod.clone()); } pub fn load_instance_by_id(&self, id: &str) -> Result { @@ -109,7 +119,7 @@ impl RootConfig { } } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] struct InstancePointer { path: PathBuf, }