mirror of
https://github.com/RGBCube/superfreq
synced 2025-07-27 17:07:44 +00:00
config: better more enhanched expression
This commit is contained in:
parent
f3813230c5
commit
0de8105432
1 changed files with 87 additions and 21 deletions
108
src/config.rs
108
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")]
|
#[serde(untagged, rename_all = "kebab-case")]
|
||||||
pub enum Condition {
|
pub enum Expression {
|
||||||
ChargeLessThan(u8),
|
#[serde(rename = "$cpu-temperature")]
|
||||||
ChargeMoreThan(u8),
|
CpuTemperature,
|
||||||
|
|
||||||
TemperatureLessThan(u8),
|
#[serde(rename = "%cpu-volatility")]
|
||||||
TemperatureMoreThan(u8),
|
CpuVolatility,
|
||||||
|
|
||||||
UtilizationLessThan(u8),
|
#[serde(rename = "%cpu-utilization")]
|
||||||
UtilizationMoreThan(u8),
|
CpuUtilization,
|
||||||
|
|
||||||
|
#[serde(rename = "%power-supply-charge")]
|
||||||
|
PowerSupplyCharge,
|
||||||
|
|
||||||
|
#[serde(rename = "%power-supply-discharge-rate")]
|
||||||
|
PowerSupplyDischargeRate,
|
||||||
|
|
||||||
|
#[serde(rename = "?charging")]
|
||||||
Charging,
|
Charging,
|
||||||
|
#[serde(rename = "?on-battery")]
|
||||||
OnBattery,
|
OnBattery,
|
||||||
|
|
||||||
|
#[serde(rename = "#false")]
|
||||||
False,
|
False,
|
||||||
|
|
||||||
#[default]
|
#[default]
|
||||||
|
#[serde(rename = "#true")]
|
||||||
True,
|
True,
|
||||||
|
|
||||||
All(Vec<Condition>),
|
Number(f64),
|
||||||
Any(Vec<Condition>),
|
|
||||||
|
|
||||||
Not(Box<Condition>),
|
Plus {
|
||||||
|
value: Box<Expression>,
|
||||||
|
plus: Box<Expression>,
|
||||||
|
},
|
||||||
|
Minus {
|
||||||
|
value: Box<Expression>,
|
||||||
|
minus: Box<Expression>,
|
||||||
|
},
|
||||||
|
Multiply {
|
||||||
|
value: Box<Expression>,
|
||||||
|
multiply: Box<Expression>,
|
||||||
|
},
|
||||||
|
Power {
|
||||||
|
value: Box<Expression>,
|
||||||
|
power: Box<Expression>,
|
||||||
|
},
|
||||||
|
Divide {
|
||||||
|
value: Box<Expression>,
|
||||||
|
divide: Box<Expression>,
|
||||||
|
},
|
||||||
|
|
||||||
|
LessThan {
|
||||||
|
value: Box<Expression>,
|
||||||
|
is_less_than: Box<Expression>,
|
||||||
|
},
|
||||||
|
|
||||||
|
MoreThan {
|
||||||
|
value: Box<Expression>,
|
||||||
|
is_more_than: Box<Expression>,
|
||||||
|
},
|
||||||
|
|
||||||
|
Equal {
|
||||||
|
value: Box<Expression>,
|
||||||
|
is_equal: Box<Expression>,
|
||||||
|
leeway: Box<Expression>,
|
||||||
|
},
|
||||||
|
|
||||||
|
And {
|
||||||
|
value: Box<Expression>,
|
||||||
|
and: Box<Expression>,
|
||||||
|
},
|
||||||
|
All {
|
||||||
|
all: Vec<Expression>,
|
||||||
|
},
|
||||||
|
|
||||||
|
Or {
|
||||||
|
value: Box<Expression>,
|
||||||
|
or: Box<Expression>,
|
||||||
|
},
|
||||||
|
Any {
|
||||||
|
any: Vec<Expression>,
|
||||||
|
},
|
||||||
|
|
||||||
|
Not {
|
||||||
|
not: Box<Expression>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq)]
|
#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
|
||||||
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
|
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
|
||||||
pub struct DaemonConfigLayer {
|
pub struct Rule {
|
||||||
priority: u8,
|
priority: u8,
|
||||||
|
|
||||||
#[serde(default, skip_serializing_if = "is_default")]
|
#[serde(default, skip_serializing_if = "is_default")]
|
||||||
if_: Condition,
|
if_: Expression,
|
||||||
|
|
||||||
#[serde(default, skip_serializing_if = "is_default")]
|
#[serde(default, skip_serializing_if = "is_default")]
|
||||||
cpu: CpuDelta,
|
cpu: CpuDelta,
|
||||||
|
@ -193,10 +258,11 @@ pub struct DaemonConfigLayer {
|
||||||
power: PowerDelta,
|
power: PowerDelta,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq)]
|
#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
|
||||||
#[serde(transparent, default, rename_all = "kebab-case")]
|
#[serde(transparent, default, rename_all = "kebab-case")]
|
||||||
pub struct DaemonConfig {
|
pub struct DaemonConfig {
|
||||||
config: Vec<DaemonConfigLayer>,
|
#[serde(rename = "rule")]
|
||||||
|
rules: Vec<Rule>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DaemonConfig {
|
impl DaemonConfig {
|
||||||
|
@ -208,14 +274,14 @@ impl DaemonConfig {
|
||||||
let config: Self = toml::from_str(&contents).context("failed to parse config file")?;
|
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 {
|
for rule in &config.rules {
|
||||||
if priorities.contains(&layer.priority) {
|
if priorities.contains(&rule.priority) {
|
||||||
bail!("each config layer must have a different priority")
|
bail!("each config rule must have a different priority")
|
||||||
}
|
}
|
||||||
|
|
||||||
priorities.push(layer.priority);
|
priorities.push(rule.priority);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue