/* * This file is part of laurelin/client * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. * See LICENSE for licensing information. */ // TODO: We should encode the configs as base64 use bevy::prelude::*; pub fn load serde::Deserialize<'a>>(target_dir: &str, file_name: &str) -> T { let file_path: String = format!("{}/{}", target_dir, file_name); match std::path::Path::new(&file_path).exists() { true => { info!("sl::load found '{}'", file_path); let json: Vec = std::fs::read(file_path.clone()).unwrap(); serde_json::from_str(std::str::from_utf8(&json).unwrap()).unwrap_or_else(|_| { panic!( "sl::load couldn't deserialize the config at '{}'", file_path ) }) } false => { warn!("sl::load couldn't find '{}', using defaults", file_path); T::default() } } } pub fn save(target_dir: &str, file_name: &str, value: &T) -> bool where T: ?Sized + serde::Serialize, { let file_path: String = format!("{}/{}", target_dir, file_name); let json: String = serde_json::to_string(&value).unwrap(); match std::path::Path::new(target_dir).exists() { true => {} false => { info!("sl::save creating target dir '{}'", target_dir); match std::fs::create_dir_all(target_dir) { Ok(_) => {} Err(_) => { warn!("sl::save couldn't create target dir '{}'", target_dir); warn!("sl::save nothing will be saved!"); return false; } } } } match std::fs::write(&file_path, json) { Ok(_) => { info!("sl::save wrote file '{}'", file_path); true } Err(_) => { info!("sl::save couldn't write file '{}'", file_path); false } } }