Config files use generic traits

This commit is contained in:
Luke D. Jones
2023-01-06 12:03:12 +13:00
parent 54273cfb60
commit e4f79a3e6f
18 changed files with 314 additions and 333 deletions

View File

@@ -1,13 +1,12 @@
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::fs::File;
use std::io::Write;
use std::time::Duration;
use log::{error, info, warn};
use rog_anime::error::AnimeError;
use rog_anime::{ActionData, ActionLoader, AnimTime, AnimeType, Fade, Vec2};
use serde_derive::{Deserialize, Serialize};
use crate::{config_file_open, VERSION};
use crate::config_traits::{StdConfig, StdConfigLoad3};
pub static CONFIG_FILE: &str = "anime.conf";
pub static ANIME_CACHE_PATH: &str = "/etc/asusd/anime-cache.conf";
@@ -20,25 +19,25 @@ pub struct AnimeConfigV341 {
pub shutdown: Option<ActionLoader>,
}
impl AnimeConfigV341 {
pub(crate) fn into_current(self) -> AnimeConfig {
impl From<AnimeConfigV341> for AnimeConfig {
fn from(c: AnimeConfigV341) -> AnimeConfig {
AnimeConfig {
system: if let Some(ani) = self.system {
system: if let Some(ani) = c.system {
vec![ani]
} else {
vec![]
},
boot: if let Some(ani) = self.boot {
boot: if let Some(ani) = c.boot {
vec![ani]
} else {
vec![]
},
wake: if let Some(ani) = self.suspend {
wake: if let Some(ani) = c.suspend {
vec![ani]
} else {
vec![]
},
shutdown: if let Some(ani) = self.shutdown {
shutdown: if let Some(ani) = c.shutdown {
vec![ani]
} else {
vec![]
@@ -59,13 +58,13 @@ pub struct AnimeConfigV352 {
pub brightness: f32,
}
impl AnimeConfigV352 {
pub(crate) fn into_current(self) -> AnimeConfig {
impl From<AnimeConfigV352> for AnimeConfig {
fn from(c: AnimeConfigV352) -> AnimeConfig {
AnimeConfig {
system: self.system,
boot: self.boot,
wake: self.wake,
shutdown: self.shutdown,
system: c.system,
boot: c.boot,
wake: c.wake,
shutdown: c.shutdown,
brightness: 1.0,
awake_enabled: true,
boot_anim_enabled: true,
@@ -140,56 +139,28 @@ impl Default for AnimeConfig {
}
}
impl AnimeConfig {
/// `load` will attempt to read the config, and panic if the dir is missing
pub fn load() -> Self {
let mut file = config_file_open(CONFIG_FILE);
let mut buf = String::new();
if let Ok(read_len) = file.read_to_string(&mut buf) {
if read_len == 0 {
return AnimeConfig::create_default(&mut file);
} else {
if let Ok(mut data) = serde_json::from_str(&buf) {
Self::clamp_config_brightness(&mut data);
return data;
} else if let Ok(data) = serde_json::from_str::<AnimeConfigV341>(&buf) {
let mut config = data.into_current();
config.write();
info!("Updated config version to: {}", VERSION);
Self::clamp_config_brightness(&mut config);
return config;
} else if let Ok(data) = serde_json::from_str::<AnimeConfigV352>(&buf) {
let mut config = data.into_current();
config.write();
info!("Updated config version to: {}", VERSION);
Self::clamp_config_brightness(&mut config);
return config;
}
warn!(
"Could not deserialise {}.\nWill rename to {}-old and recreate config",
CONFIG_FILE, CONFIG_FILE
);
let cfg_old = CONFIG_FILE.to_string() + "-old";
std::fs::rename(CONFIG_FILE, cfg_old).unwrap_or_else(|err| {
panic!(
"Could not rename. Please remove {} then restart service: Error {}",
CONFIG_FILE, err
)
});
}
}
AnimeConfig::create_default(&mut file)
impl StdConfig for AnimeConfig {
fn new() -> Self {
Self::create_default(&mut Self::file_open())
}
fn clamp_config_brightness(mut config: &mut AnimeConfig) {
if config.brightness < 0.0 || config.brightness > 1.0 {
warn!(
"Clamped brightness to [0.0 ; 1.0], was {}",
config.brightness
);
config.brightness = f32::max(0.0, f32::min(1.0, config.brightness));
}
fn file_name() -> &'static str {
CONFIG_FILE
}
}
impl StdConfigLoad3<AnimeConfig, AnimeConfigV341, AnimeConfigV352> for AnimeConfig {}
impl AnimeConfig {
// fn clamp_config_brightness(mut config: &mut AnimeConfig) {
// if config.brightness < 0.0 || config.brightness > 1.0 {
// warn!(
// "Clamped brightness to [0.0 ; 1.0], was {}",
// config.brightness
// );
// config.brightness = f32::max(0.0, f32::min(1.0, config.brightness));
// }
// }
fn create_default(file: &mut File) -> Self {
// create a default config here
@@ -237,28 +208,4 @@ impl AnimeConfig {
.unwrap_or_else(|_| panic!("Could not write {}", CONFIG_FILE));
config
}
pub fn read(&mut self) {
let mut file = OpenOptions::new()
.read(true)
.open(CONFIG_FILE)
.unwrap_or_else(|err| panic!("Error reading {}: {}", CONFIG_FILE, err));
let mut buf = String::new();
if let Ok(l) = file.read_to_string(&mut buf) {
if l == 0 {
warn!("File is empty {}", CONFIG_FILE);
} else {
let x: AnimeConfig = serde_json::from_str(&buf)
.unwrap_or_else(|_| panic!("Could not deserialise {}", CONFIG_FILE));
*self = x;
}
}
}
pub fn write(&self) {
let mut file = File::create(CONFIG_FILE).expect("Couldn't overwrite config");
let json = serde_json::to_string_pretty(self).expect("Parse config to JSON failed");
file.write_all(json.as_bytes())
.unwrap_or_else(|err| error!("Could not write config: {}", err));
}
}

View File

@@ -9,6 +9,7 @@ use zbus::export::futures_util::lock::{Mutex, MutexGuard};
use zbus::{dbus_interface, Connection, SignalContext};
use super::CtrlAnime;
use crate::config_traits::StdConfig;
use crate::error::RogError;
pub(super) const ZBUS_PATH: &str = "/org/asuslinux/Anime";