Fix loading & saving non-persistent configs, hopefully

This commit is contained in:
NGnius (Graham) 2022-10-04 20:49:02 -04:00
parent 6878897784
commit c13f35a4a1
5 changed files with 19 additions and 15 deletions

2
backend/Cargo.lock generated
View file

@ -567,7 +567,7 @@ dependencies = [
[[package]] [[package]]
name = "powertools-rs" name = "powertools-rs"
version = "1.0.4" version = "1.0.5"
dependencies = [ dependencies = [
"log", "log",
"serde", "serde",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "powertools-rs" name = "powertools-rs"
version = "1.0.4" version = "1.0.5"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -31,11 +31,16 @@ impl Default for SettingsJson {
impl SettingsJson { impl SettingsJson {
pub fn save<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), JsonError> { pub fn save<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), JsonError> {
let path = path.as_ref(); let path = path.as_ref();
if let Some(parent) = path.parent() { if !self.persistent && path.exists() {
std::fs::create_dir_all(parent).map_err(JsonError::Io)?; // remove settings file when persistence is turned off, to prevent it from be loaded next time.
std::fs::remove_file(path).map_err(JsonError::Io)
} else {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(JsonError::Io)?;
}
let mut file = std::fs::File::create(path).map_err(JsonError::Io)?;
serde_json::to_writer_pretty(&mut file, &self).map_err(JsonError::Serde)
} }
let mut file = std::fs::File::create(path).map_err(JsonError::Io)?;
serde_json::to_writer_pretty(&mut file, &self).map_err(JsonError::Serde)
} }
pub fn open<P: AsRef<std::path::Path>>(path: P) -> Result<Self, JsonError> { pub fn open<P: AsRef<std::path::Path>>(path: P) -> Result<Self, JsonError> {

View file

@ -37,13 +37,6 @@ pub struct General {
impl OnSet for General { impl OnSet for General {
fn on_set(&mut self) -> Result<(), SettingError> { fn on_set(&mut self) -> Result<(), SettingError> {
// remove settings file when persistence is turned off, to prevent it from be loaded next time.
if !self.persistent && self.path.exists() {
std::fs::remove_file(&self.path).map_err(|e| SettingError {
msg: format!("Failed to delete `{}`: {}", self.path.display(), e),
setting: SettingVariant::General,
})?;
}
Ok(()) Ok(())
} }
} }
@ -143,6 +136,12 @@ impl Settings {
msg: e.to_string(), msg: e.to_string(),
setting: SettingVariant::General, setting: SettingVariant::General,
})?; })?;
if !settings_json.persistent {
log::warn!("Loaded persistent config `{}` with persistent=false", json_path.display());
general_lock.persistent = false;
general_lock.name = settings_json.name;
return Ok(false);
}
let new_cpus = Self::convert_cpus(settings_json.cpus, settings_json.version); let new_cpus = Self::convert_cpus(settings_json.cpus, settings_json.version);
let new_gpu = Gpu::from_json(settings_json.gpu, settings_json.version); let new_gpu = Gpu::from_json(settings_json.gpu, settings_json.version);
let new_battery = Battery::from_json(settings_json.battery, settings_json.version); let new_battery = Battery::from_json(settings_json.battery, settings_json.version);

View file

@ -1,6 +1,6 @@
{ {
"name": "PowerTools", "name": "PowerTools",
"version": "1.0.4", "version": "1.0.5",
"description": "Power tweaks for power users", "description": "Power tweaks for power users",
"scripts": { "scripts": {
"build": "shx rm -rf dist && rollup -c", "build": "shx rm -rf dist && rollup -c",
@ -39,7 +39,7 @@
"typescript": "^4.6.4" "typescript": "^4.6.4"
}, },
"dependencies": { "dependencies": {
"decky-frontend-lib": "*", "decky-frontend-lib": "3.*",
"react-icons": "^4.4.0", "react-icons": "^4.4.0",
"usdpl-front": "file:./src/usdpl_front" "usdpl-front": "file:./src/usdpl_front"
} }