From 0de81054328c93e0acd97ffcc69d4e1323026d7b Mon Sep 17 00:00:00 2001 From: RGBCube Date: Mon, 19 May 2025 22:22:26 +0300 Subject: [PATCH] config: better more enhanched expression --- src/config.rs | 108 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 87 insertions(+), 21 deletions(-) diff --git a/src/config.rs b/src/config.rs index 5d528b8..f8fb584 100644 --- a/src/config.rs +++ b/src/config.rs @@ -154,38 +154,103 @@ impl PowerDelta { } } -#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)] #[serde(untagged, rename_all = "kebab-case")] -pub enum Condition { - ChargeLessThan(u8), - ChargeMoreThan(u8), +pub enum Expression { + #[serde(rename = "$cpu-temperature")] + CpuTemperature, - TemperatureLessThan(u8), - TemperatureMoreThan(u8), + #[serde(rename = "%cpu-volatility")] + CpuVolatility, - UtilizationLessThan(u8), - UtilizationMoreThan(u8), + #[serde(rename = "%cpu-utilization")] + CpuUtilization, + #[serde(rename = "%power-supply-charge")] + PowerSupplyCharge, + + #[serde(rename = "%power-supply-discharge-rate")] + PowerSupplyDischargeRate, + + #[serde(rename = "?charging")] Charging, + #[serde(rename = "?on-battery")] OnBattery, + #[serde(rename = "#false")] False, + #[default] + #[serde(rename = "#true")] True, - All(Vec), - Any(Vec), + Number(f64), - Not(Box), + Plus { + value: Box, + plus: Box, + }, + Minus { + value: Box, + minus: Box, + }, + Multiply { + value: Box, + multiply: Box, + }, + Power { + value: Box, + power: Box, + }, + Divide { + value: Box, + divide: Box, + }, + + LessThan { + value: Box, + is_less_than: Box, + }, + + MoreThan { + value: Box, + is_more_than: Box, + }, + + Equal { + value: Box, + is_equal: Box, + leeway: Box, + }, + + And { + value: Box, + and: Box, + }, + All { + all: Vec, + }, + + Or { + value: Box, + or: Box, + }, + Any { + any: Vec, + }, + + Not { + not: Box, + }, } -#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq)] +#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)] #[serde(deny_unknown_fields, rename_all = "kebab-case")] -pub struct DaemonConfigLayer { +pub struct Rule { priority: u8, #[serde(default, skip_serializing_if = "is_default")] - if_: Condition, + if_: Expression, #[serde(default, skip_serializing_if = "is_default")] cpu: CpuDelta, @@ -193,10 +258,11 @@ pub struct DaemonConfigLayer { power: PowerDelta, } -#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq)] +#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)] #[serde(transparent, default, rename_all = "kebab-case")] pub struct DaemonConfig { - config: Vec, + #[serde(rename = "rule")] + rules: Vec, } impl DaemonConfig { @@ -208,14 +274,14 @@ impl DaemonConfig { let config: Self = toml::from_str(&contents).context("failed to parse config file")?; { - let mut priorities = Vec::with_capacity(config.config.len()); + let mut priorities = Vec::with_capacity(config.rules.len()); - for layer in &config.config { - if priorities.contains(&layer.priority) { - bail!("each config layer must have a different priority") + for rule in &config.rules { + if priorities.contains(&rule.priority) { + bail!("each config rule must have a different priority") } - priorities.push(layer.priority); + priorities.push(rule.priority); } }