Merge branch 'fluke/aura_advanced' into 'main'

Convert repeated code in config-traits to a macro

See merge request asus-linux/asusctl!149
This commit is contained in:
Luke Jones
2023-01-09 10:32:54 +00:00
8 changed files with 172 additions and 199 deletions

View File

@@ -148,197 +148,170 @@ where
} }
} }
/// Base trait for loading/parsing. This can be used to help update configs to #[macro_export]
/// new versions macro_rules! std_config_load {
/// ($trait_name:ident: $($generic:ident),*) => {
/// # Example /// Base trait for loading/parsing. This is intended to be used to help update
/// ```rust /// configs to new versions
/// use std::path::PathBuf; ///
/// use serde::{Deserialize, Serialize}; /// # Example
/// use config_traits::{StdConfig, StdConfigLoad1}; /// ```rust
/// /// use std::path::PathBuf;
/// #[derive(Deserialize, Serialize)] /// use serde::{Deserialize, Serialize};
/// struct FanCurveConfig {} /// use config_traits::{StdConfig, StdConfigLoad2};
/// ///
/// impl StdConfig for FanCurveConfig { /// #[derive(Deserialize, Serialize)]
/// fn new() -> Self { Self {} } /// struct FanCurveConfigOld {}
/// ///
/// fn file_name(&self) -> std::string::String { "test_name.conf".to_owned() } /// #[derive(Deserialize, Serialize)]
/// /// struct FanCurveConfigOlder {}
/// fn config_dir() -> PathBuf { PathBuf::from("/tmp") } ///
/// } /// #[derive(Deserialize, Serialize)]
/// /// struct FanCurveConfig {}
/// impl StdConfigLoad1 for FanCurveConfig {} ///
/// ``` /// impl From<FanCurveConfigOld> for FanCurveConfig {
/// /// fn from(_: FanCurveConfigOld) -> Self { Self {} }
/// If all of the generics fails to parse, then the old config is renamed and a /// }
/// new one created ///
pub trait StdConfigLoad1 /// impl From<FanCurveConfigOlder> for FanCurveConfig {
where /// fn from(_: FanCurveConfigOlder) -> Self { Self {} }
Self: StdConfig + DeserializeOwned + Serialize, /// }
{ ///
fn load(mut self) -> Self { /// impl StdConfig for FanCurveConfig {
let mut file = self.file_open(); /// fn new() -> Self { Self {} }
let mut buf = String::new(); ///
if let Ok(read_len) = file.read_to_string(&mut buf) { /// fn file_name(&self) -> std::string::String { "test_name.conf".to_owned() }
if read_len != 0 { ///
if let Ok(data) = ron::from_str(&buf) { /// fn config_dir() -> PathBuf { PathBuf::from("/tmp") }
self = data; /// }
} else if let Ok(data) = serde_json::from_str(&buf) { ///
self = data; /// impl StdConfigLoad2<FanCurveConfigOld, FanCurveConfigOlder> for FanCurveConfig {}
} else if let Ok(data) = toml::from_str(&buf) { /// ```
self = data; ///
} else { /// If all of the generics fails to parse, then the old config is renamed and a
self.rename_file_old(); /// new one created
self = Self::new(); pub trait $trait_name<$($generic),*>
where
Self: $crate::StdConfig + DeserializeOwned + Serialize,
$($generic: DeserializeOwned + Into<Self>),*
{
fn load(mut self) -> Self {
let mut file = self.file_open();
let mut buf = String::new();
if let Ok(read_len) = file.read_to_string(&mut buf) {
if read_len != 0 {
if let Ok(data) = ron::from_str(&buf) {
self = data;
} else if let Ok(data) = serde_json::from_str(&buf) {
self = data;
} else if let Ok(data) = toml::from_str(&buf) {
self = data;
} $(else if let Ok(data) = serde_json::from_str::<$generic>(&buf) {
self = data.into();
} else if let Ok(data) = toml::from_str::<$generic>(&buf) {
self = data.into();
})* else {
self.rename_file_old();
self = Self::new();
}
} else {
error!("Config file {} zero read length", self.file_name());
}
} }
} else { self.write();
error!("Config file {} zero read length", self.file_name()); self
} }
} }
self.write(); };
self
}
} }
/// Base trait for loading/parsing. This is intended to be used to help update std_config_load!(StdConfigLoad:);
/// configs to new versions std_config_load!(StdConfigLoad1: T1);
/// std_config_load!(StdConfigLoad2: T1, T2);
/// # Example std_config_load!(StdConfigLoad3: T1, T2, T3);
/// ```rust std_config_load!(StdConfigLoad4: T1, T2, T3, T4);
/// use std::path::PathBuf;
/// use serde::{Deserialize, Serialize};
/// use config_traits::{StdConfig, StdConfigLoad2};
///
/// #[derive(Deserialize, Serialize)]
/// struct FanCurveConfigOld {}
///
/// #[derive(Deserialize, Serialize)]
/// struct FanCurveConfig {}
///
/// impl From<FanCurveConfigOld> for FanCurveConfig {
/// fn from(_: FanCurveConfigOld) -> Self { Self {} }
/// }
///
/// impl StdConfig for FanCurveConfig {
/// fn new() -> Self { Self {} }
///
/// fn file_name(&self) -> std::string::String { "test_name.conf".to_owned() }
///
/// fn config_dir() -> PathBuf { PathBuf::from("/tmp") }
/// }
///
/// impl StdConfigLoad2<FanCurveConfigOld> for FanCurveConfig {}
/// ```
///
/// If all of the generics fails to parse, then the old config is renamed and a
/// new one created
pub trait StdConfigLoad2<OldConfig>
where
Self: StdConfig + DeserializeOwned + Serialize,
OldConfig: DeserializeOwned + Into<Self>,
{
fn load(mut self) -> Self {
let mut file = self.file_open();
let mut buf = String::new();
if let Ok(read_len) = file.read_to_string(&mut buf) {
if read_len != 0 {
if let Ok(data) = ron::from_str(&buf) {
self = data;
} else if let Ok(data) = serde_json::from_str(&buf) {
self = data;
} else if let Ok(data) = toml::from_str(&buf) {
self = data;
} else if let Ok(data) = serde_json::from_str::<OldConfig>(&buf) {
self = data.into();
} else if let Ok(data) = toml::from_str::<OldConfig>(&buf) {
self = data.into();
} else {
self.rename_file_old();
self = Self::new();
}
} else {
error!("Config file {} zero read length", self.file_name());
}
}
self.write();
self
}
}
/// Base trait for loading/parsing. This is intended to be used to help update #[cfg(test)]
/// configs to new versions mod tests {
/// use std::path::PathBuf;
/// # Example
/// ```rust #[test]
/// use std::path::PathBuf; fn check_macro_from_1() {
/// use serde::{Deserialize, Serialize}; #[derive(serde::Deserialize, serde::Serialize)]
/// use config_traits::{StdConfig, StdConfigLoad3}; struct Test {}
///
/// #[derive(Deserialize, Serialize)] #[derive(serde::Deserialize, serde::Serialize)]
/// struct FanCurveConfigOld {} struct Old1 {}
///
/// #[derive(Deserialize, Serialize)] impl crate::StdConfig for Test {
/// struct FanCurveConfigOlder {} fn new() -> Self {
/// Self {}
/// #[derive(Deserialize, Serialize)] }
/// struct FanCurveConfig {}
/// fn file_name(&self) -> String {
/// impl From<FanCurveConfigOld> for FanCurveConfig { String::new()
/// fn from(_: FanCurveConfigOld) -> Self { Self {} } }
/// }
/// fn config_dir() -> PathBuf {
/// impl From<FanCurveConfigOlder> for FanCurveConfig { PathBuf::new()
/// fn from(_: FanCurveConfigOlder) -> Self { Self {} }
/// }
///
/// impl StdConfig for FanCurveConfig {
/// fn new() -> Self { Self {} }
///
/// fn file_name(&self) -> std::string::String { "test_name.conf".to_owned() }
///
/// fn config_dir() -> PathBuf { PathBuf::from("/tmp") }
/// }
///
/// impl StdConfigLoad3<FanCurveConfigOld, FanCurveConfigOlder> for FanCurveConfig {}
/// ```
///
/// If all of the generics fails to parse, then the old config is renamed and a
/// new one created
pub trait StdConfigLoad3<OldConfig, OldConfig2>: StdConfig
where
Self: StdConfig + DeserializeOwned + Serialize,
OldConfig: DeserializeOwned + Into<Self>,
OldConfig2: DeserializeOwned + Into<Self>,
{
fn load(mut self) -> Self {
let mut file = self.file_open();
let mut buf = String::new();
if let Ok(read_len) = file.read_to_string(&mut buf) {
if read_len != 0 {
if let Ok(data) = ron::from_str(&buf) {
self = data;
} else if let Ok(data) = serde_json::from_str(&buf) {
self = data;
} else if let Ok(data) = toml::from_str(&buf) {
self = data;
} else if let Ok(data) = serde_json::from_str::<OldConfig>(&buf) {
self = data.into();
} else if let Ok(data) = toml::from_str::<OldConfig>(&buf) {
self = data.into();
} else if let Ok(data) = serde_json::from_str::<OldConfig2>(&buf) {
self = data.into();
} else if let Ok(data) = toml::from_str::<OldConfig2>(&buf) {
self = data.into();
} else {
self.rename_file_old();
self = Self::new();
}
} else {
error!("Config file {} zero read length", self.file_name());
} }
} }
self.write();
self impl From<Old1> for Test {
fn from(_: Old1) -> Self {
Self {}
}
}
impl crate::StdConfigLoad1<Old1> for Test {}
}
#[test]
fn check_macro_from_3() {
#[derive(serde::Deserialize, serde::Serialize)]
struct Test {}
#[derive(serde::Deserialize, serde::Serialize)]
struct Old1 {}
#[derive(serde::Deserialize, serde::Serialize)]
struct Old2 {}
#[derive(serde::Deserialize, serde::Serialize)]
struct Old3 {}
impl crate::StdConfig for Test {
fn new() -> Self {
Self {}
}
fn file_name(&self) -> String {
String::new()
}
fn config_dir() -> PathBuf {
PathBuf::new()
}
}
impl From<Old1> for Test {
fn from(_: Old1) -> Self {
Self {}
}
}
impl From<Old2> for Test {
fn from(_: Old2) -> Self {
Self {}
}
}
impl From<Old3> for Test {
fn from(_: Old3) -> Self {
Self {}
}
}
impl crate::StdConfigLoad3<Old1, Old2, Old3> for Test {}
} }
} }

View File

@@ -1,7 +1,7 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::time::Duration; use std::time::Duration;
use config_traits::{StdConfig, StdConfigLoad1}; use config_traits::{StdConfig, StdConfigLoad};
use rog_anime::{ActionLoader, AnimTime, AnimeType, Fade, Sequences as AnimeSequences, Vec2}; use rog_anime::{ActionLoader, AnimTime, AnimeType, Fade, Sequences as AnimeSequences, Vec2};
use rog_aura::advanced::LedCode; use rog_aura::advanced::LedCode;
use rog_aura::effects::{AdvancedEffects as AuraSequences, Breathe, DoomFlicker, Effect, Static}; use rog_aura::effects::{AdvancedEffects as AuraSequences, Breathe, DoomFlicker, Effect, Static};
@@ -116,7 +116,7 @@ impl StdConfig for ConfigAnime {
} }
} }
impl StdConfigLoad1 for ConfigAnime {} impl StdConfigLoad for ConfigAnime {}
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct ConfigAura { pub struct ConfigAura {
@@ -188,7 +188,7 @@ impl StdConfig for ConfigAura {
} }
} }
impl StdConfigLoad1 for ConfigAura {} impl StdConfigLoad for ConfigAura {}
#[derive(Debug, Default, Deserialize, Serialize)] #[derive(Debug, Default, Deserialize, Serialize)]
#[serde(default)] #[serde(default)]
@@ -216,4 +216,4 @@ impl StdConfig for ConfigBase {
} }
} }
impl StdConfigLoad1 for ConfigBase {} impl StdConfigLoad for ConfigBase {}

View File

@@ -4,7 +4,7 @@ use std::path::PathBuf;
use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicBool;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use config_traits::{StdConfig, StdConfigLoad1}; use config_traits::{StdConfig, StdConfigLoad};
use rog_anime::usb::get_anime_type; use rog_anime::usb::get_anime_type;
use rog_aura::aura_detection::LaptopLedData; use rog_aura::aura_detection::LaptopLedData;
use rog_aura::layouts::KeyLayout; use rog_aura::layouts::KeyLayout;

View File

@@ -1,4 +1,4 @@
use config_traits::{StdConfig, StdConfigLoad3}; use config_traits::{StdConfig, StdConfigLoad2};
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
const CONFIG_FILE: &str = "asusd.ron"; const CONFIG_FILE: &str = "asusd.ron";
@@ -33,7 +33,7 @@ impl StdConfig for Config {
} }
} }
impl StdConfigLoad3<Config455, Config458> for Config {} impl StdConfigLoad2<Config455, Config458> for Config {}
#[derive(Deserialize, Serialize, Default)] #[derive(Deserialize, Serialize, Default)]
#[serde(default)] #[serde(default)]

View File

@@ -1,6 +1,6 @@
use std::time::Duration; use std::time::Duration;
use config_traits::{StdConfig, StdConfigLoad3}; use config_traits::{StdConfig, StdConfigLoad2};
use rog_anime::error::AnimeError; use rog_anime::error::AnimeError;
use rog_anime::{ActionData, ActionLoader, AnimTime, AnimeType, Fade, Vec2}; use rog_anime::{ActionData, ActionLoader, AnimTime, AnimeType, Fade, Vec2};
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
@@ -149,7 +149,7 @@ impl StdConfig for AnimeConfig {
} }
} }
impl StdConfigLoad3<AnimeConfigV341, AnimeConfigV352> for AnimeConfig {} impl StdConfigLoad2<AnimeConfigV341, AnimeConfigV352> for AnimeConfig {}
impl AnimeConfig { impl AnimeConfig {
// fn clamp_config_brightness(mut config: &mut AnimeConfig) { // fn clamp_config_brightness(mut config: &mut AnimeConfig) {

View File

@@ -1,6 +1,6 @@
use std::collections::{BTreeMap, HashSet}; use std::collections::{BTreeMap, HashSet};
use config_traits::{StdConfig, StdConfigLoad1}; use config_traits::{StdConfig, StdConfigLoad};
use rog_aura::aura_detection::{LaptopLedData, ASUS_KEYBOARD_DEVICES}; use rog_aura::aura_detection::{LaptopLedData, ASUS_KEYBOARD_DEVICES};
use rog_aura::usb::{AuraDev1866, AuraDev19b6, AuraDevTuf, AuraDevice, AuraPowerDev}; use rog_aura::usb::{AuraDev1866, AuraDev19b6, AuraDevTuf, AuraDevice, AuraPowerDev};
use rog_aura::{AuraEffect, AuraModeNum, AuraZone, Direction, LedBrightness, Speed, GRADIENT}; use rog_aura::{AuraEffect, AuraModeNum, AuraZone, Direction, LedBrightness, Speed, GRADIENT};
@@ -199,7 +199,7 @@ impl StdConfig for AuraConfig {
} }
} }
impl StdConfigLoad1 for AuraConfig {} impl StdConfigLoad for AuraConfig {}
impl AuraConfig { impl AuraConfig {
fn create_default(support_data: &LaptopLedData) -> Self { fn create_default(support_data: &LaptopLedData) -> Self {

View File

@@ -1,6 +1,6 @@
use std::path::PathBuf; use std::path::PathBuf;
use config_traits::{StdConfig, StdConfigLoad1}; use config_traits::{StdConfig, StdConfigLoad};
use rog_profiles::fan_curve_set::FanCurveSet; use rog_profiles::fan_curve_set::FanCurveSet;
use rog_profiles::{FanCurveProfiles, Profile}; use rog_profiles::{FanCurveProfiles, Profile};
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
@@ -32,7 +32,7 @@ impl StdConfig for ProfileConfig {
} }
} }
impl StdConfigLoad1 for ProfileConfig {} impl StdConfigLoad for ProfileConfig {}
#[derive(Deserialize, Serialize, Debug, Default)] #[derive(Deserialize, Serialize, Debug, Default)]
pub struct FanCurveConfig { pub struct FanCurveConfig {
@@ -81,4 +81,4 @@ impl StdConfig for FanCurveConfig {
} }
} }
impl StdConfigLoad1 for FanCurveConfig {} impl StdConfigLoad for FanCurveConfig {}

View File

@@ -6,7 +6,7 @@ use std::time::Duration;
use ::zbus::export::futures_util::lock::Mutex; use ::zbus::export::futures_util::lock::Mutex;
use ::zbus::Connection; use ::zbus::Connection;
use config_traits::{StdConfig, StdConfigLoad1, StdConfigLoad3}; use config_traits::{StdConfig, StdConfigLoad, StdConfigLoad2};
use daemon::config::Config; use daemon::config::Config;
use daemon::ctrl_anime::config::AnimeConfig; use daemon::ctrl_anime::config::AnimeConfig;
use daemon::ctrl_anime::trait_impls::CtrlAnimeZbus; use daemon::ctrl_anime::trait_impls::CtrlAnimeZbus;