add save function to root_config

This commit is contained in:
2026-03-11 23:49:08 +01:00
parent 96dda41c46
commit 6a60e29fd7
3 changed files with 29 additions and 12 deletions

View File

@@ -3,12 +3,12 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use serde::Deserialize; use serde::{Deserialize, Serialize};
use crate::{types::link::Link, utils::walk_all_files}; use crate::{types::link::Link, utils::walk_all_files};
/// Available game /// Available game
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Game { pub struct Game {
path: PathBuf, path: PathBuf,
} }

View File

@@ -1,11 +1,12 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use serde::Deserialize; use serde::{Deserialize, Serialize};
/// Config for an available mod /// Config for an available mod
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ModConfig { pub struct ModConfig {
/// ID of the mod /// ID of the mod
#[serde(skip)]
id: String, id: String,
/// Relative to the mod_location from root config /// Relative to the mod_location from root config
@@ -13,10 +14,12 @@ pub struct ModConfig {
/// If the files should be included on the root /// If the files should be included on the root
#[serde(default)] #[serde(default)]
#[serde(skip_serializing_if = "is_false")]
root_mod: bool, root_mod: bool,
/// Globs of what files to ignore /// Globs of what files to ignore
#[serde(default)] #[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
ignore: Vec<String>, ignore: Vec<String>,
} }
@@ -47,3 +50,7 @@ impl ModConfig {
&self.ignore &self.ignore
} }
} }
fn is_false(b: &bool) -> bool {
!b
}

View File

@@ -1,21 +1,20 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
fs::{self, read_to_string}, fs::{self, read_to_string},
io::Write,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use log::debug; use log::debug;
use serde::Deserialize; use serde::{Deserialize, Serialize};
use crate::types::{ConfigReadWriteError, ModConfig, game::Game, modded_instance::ModdedInstance}; use crate::types::{ConfigReadWriteError, ModConfig, game::Game, modded_instance::ModdedInstance};
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RootConfig { pub struct RootConfig {
/// Available games
#[serde(default)] #[serde(default)]
games: HashMap<String, Game>, games: HashMap<String, Game>,
/// Where all mods are stored
mod_location: PathBuf, mod_location: PathBuf,
download_location: Option<PathBuf>, download_location: Option<PathBuf>,
@@ -27,7 +26,7 @@ pub struct RootConfig {
/// All available mods /// All available mods
#[serde(default)] #[serde(default)]
mods: Vec<ModConfig>, mods: HashMap<String, ModConfig>,
#[serde(skip)] #[serde(skip)]
self_path: PathBuf, self_path: PathBuf,
@@ -75,12 +74,23 @@ impl RootConfig {
Ok(config) 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> { pub fn game_by_id(&self, id: &str) -> Option<&Game> {
self.games.get(id) self.games.get(id)
} }
pub fn mod_by_id(&self, id: &str) -> Option<&ModConfig> { pub fn mod_by_id(&self, id: &str) -> Option<ModConfig> {
self.mods.iter().find(|e| e.id() == id) 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<ModdedInstance, ConfigReadWriteError> { pub fn load_instance_by_id(&self, id: &str) -> Result<ModdedInstance, ConfigReadWriteError> {
@@ -109,7 +119,7 @@ impl RootConfig {
} }
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize, Serialize)]
struct InstancePointer { struct InstancePointer {
path: PathBuf, path: PathBuf,
} }