Aurora-Print/Core/config/mod.rs

57 lines
1.6 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

mod default;
use default::default_config;
use std::{fs, io, path::{Path, PathBuf}};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChromeCfg {
pub path: String, // kann leer sein => auto-download
pub args: Vec<String>, // SSL-Flags etc.
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerCfg {
pub port: u16,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
pub server: ServerCfg,
pub chrome: ChromeCfg,
}
const CONF_NAME: &str = "server.conf";
pub fn read_config(dir: &Path) -> io::Result<Config> {
let path = dir.join(CONF_NAME);
if !path.exists() {
let def = default_config();
let toml_str = toml::to_string_pretty(&def)
.expect("Default Config serialisieren fehlgeschlagen");
fs::write(&path, toml_str)?;
Ok(def)
} else {
let content = fs::read_to_string(&path)?;
let cfg: Config = toml::from_str(&content)
.unwrap_or_else(|_| {
eprintln!("[Aurora] WARNUNG: Config beschädigt lade Default.");
default_config()
});
Ok(cfg)
}
}
pub fn save_config(dir: &Path, cfg: &Config) -> io::Result<()> {
let path = dir.join(CONF_NAME);
let toml_str = toml::to_string_pretty(cfg)
.expect("Config serialisieren fehlgeschlagen");
fs::write(path, toml_str)
}
/// Hilfsfunktion: macht aus einem (ggf. relativen) Pfad einen absoluten Pfad relativ zur EXE.
pub fn resolve_path(exe_dir: &Path, p: &str) -> PathBuf {
let pb = PathBuf::from(p);
if pb.is_absolute() { pb } else { exe_dir.join(pb) }
}