From 5133d398ebc467d8176d09ca81b8e0f42da56993 Mon Sep 17 00:00:00 2001 From: "Luke D. Jones" Date: Sat, 7 Jan 2023 21:20:53 +1300 Subject: [PATCH] Add extra doc comments to config-trait --- Cargo.lock | 3 +-- config-traits/src/lib.rs | 31 +++++++++++++++++++++++++++++++ daemon-user/Cargo.toml | 1 + daemon/Cargo.toml | 2 -- daemon/src/error.rs | 1 + 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 33f1add9..0476910f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -802,10 +802,8 @@ dependencies = [ "rog_dbus", "rog_platform", "rog_profiles", - "ron", "serde", "serde_derive", - "serde_json", "sysfs-class", "systemd-zbus", "tokio", @@ -816,6 +814,7 @@ dependencies = [ name = "daemon-user" version = "4.5.8" dependencies = [ + "config-traits", "dirs", "rog_anime", "rog_aura", diff --git a/config-traits/src/lib.rs b/config-traits/src/lib.rs index 6f8f6115..64708117 100644 --- a/config-traits/src/lib.rs +++ b/config-traits/src/lib.rs @@ -3,6 +3,7 @@ use std::io::{Read, Write}; use std::path::PathBuf; use log::{error, warn}; +pub use ron; use ron::ser::PrettyConfig; use serde::de::DeserializeOwned; use serde::Serialize; @@ -13,12 +14,16 @@ pub trait StdConfig where Self: Serialize + DeserializeOwned, { + /// Taking over the standard `new()` to ensure things can be generic fn new() -> Self; + /// Return the config files names, such as `wibble.cfg` fn file_name() -> &'static str; + /// Return the full path to the directory the config file resides in fn config_dir() -> PathBuf; + /// Return the full path to the config file fn file_path() -> PathBuf { let mut config = Self::config_dir(); if !config.exists() { @@ -29,6 +34,9 @@ where config } + /// Directly open the config file for read and write. If the config file + /// does not exist it is created, including the directories the file + /// resides in. fn file_open() -> File { OpenOptions::new() .read(true) @@ -38,6 +46,7 @@ where .unwrap_or_else(|e| panic!("Could not open {:?} {e}", Self::file_path())) } + /// Open and parse the config file to self from ron format fn read(&mut self) { let mut file = match OpenOptions::new().read(true).open(Self::file_path()) { Ok(data) => data, @@ -60,6 +69,7 @@ where } } + /// Write the config file data to pretty ron format fn write(&self) { let mut file = match File::create(Self::file_path()) { Ok(data) => data, @@ -100,6 +110,13 @@ where } } +/// Base trait for loading/parsing. This can be used to help update configs to +/// new versions ```ignore +/// impl StdConfigLoad1 for FanCurveConfig {} +/// ``` +/// +/// If all of the generics fails to parse, then the old config is renamed and a +/// new one created pub trait StdConfigLoad1 where T: StdConfig + DeserializeOwned + Serialize, @@ -127,6 +144,13 @@ where } } +/// Base trait for loading/parsing. This is intended to be used to help update +/// configs to new versions ```ignore +/// impl StdConfigLoad2 for FanCurveConfig {} +/// ``` +/// +/// If all of the generics fails to parse, then the old config is renamed and a +/// new one created pub trait StdConfigLoad2 where T1: StdConfig + DeserializeOwned + Serialize, @@ -157,6 +181,13 @@ where } } +/// Base trait for loading/parsing. This is intended to be used to help update +/// configs to new versions ```ignore +/// impl StdConfigLoad3 for FanCurveConfig {} +/// ``` +/// +/// If all of the generics fails to parse, then the old config is renamed and a +/// new one created pub trait StdConfigLoad3 where T1: StdConfig + DeserializeOwned + Serialize, diff --git a/daemon-user/Cargo.toml b/daemon-user/Cargo.toml index 0126276c..405f111a 100644 --- a/daemon-user/Cargo.toml +++ b/daemon-user/Cargo.toml @@ -27,5 +27,6 @@ rog_anime = { path = "../rog-anime" } rog_aura = { path = "../rog-aura" } rog_dbus = { path = "../rog-dbus" } rog_platform = { path = "../rog-platform" } +config-traits = { path = "../config-traits" } zbus.workspace = true diff --git a/daemon/Cargo.toml b/daemon/Cargo.toml index f5580976..4bad7dd7 100644 --- a/daemon/Cargo.toml +++ b/daemon/Cargo.toml @@ -38,8 +38,6 @@ logind-zbus.workspace = true # serialisation serde.workspace = true serde_derive.workspace = true -serde_json.workspace = true -ron.workspace = true # Device control sysfs-class.workspace = true # used for backlight control and baord ID diff --git a/daemon/src/error.rs b/daemon/src/error.rs index 068a792d..64ef8a0f 100644 --- a/daemon/src/error.rs +++ b/daemon/src/error.rs @@ -1,6 +1,7 @@ use std::convert::From; use std::fmt; +use config_traits::ron; use rog_anime::error::AnimeError; use rog_platform::error::PlatformError; use rog_profiles::error::ProfileError;