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},
};
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,
}

View File

@@ -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<String>,
}
@@ -47,3 +50,7 @@ impl ModConfig {
&self.ignore
}
}
fn is_false(b: &bool) -> bool {
!b
}

View File

@@ -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<String, Game>,
/// Where all mods are stored
mod_location: PathBuf,
download_location: Option<PathBuf>,
@@ -27,7 +26,7 @@ pub struct RootConfig {
/// All available mods
#[serde(default)]
mods: Vec<ModConfig>,
mods: HashMap<String, ModConfig>,
#[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<ModConfig> {
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> {
@@ -109,7 +119,7 @@ impl RootConfig {
}
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
struct InstancePointer {
path: PathBuf,
}