mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-21 08:01:39 +01:00
refactor(config): push_simple_rule, adding a simple rule once per kind and id
Moves KDE's push_simple_rule (and its test) to komorebi_config::matching: add a simple rule that matches id as a kind with the legacy strategy unless a simple rule of that kind and id is there already; a rule of another kind with the same id and a composite rule do not stop it. The KDE daemon and, next, the macOS daemon use it for the ignore and manage rules. Nothing calls it yet. Verified: native cargo test -p komorebi-config 5 + 16 + 9 + 1 passed.
This commit is contained in:
1 parent
2ba338347c
commit
f3b9d926fb
1 file changed
+39
@@ -94,6 +94,23 @@ impl From<IdWithIdentifierAndComment> for IdWithIdentifier {
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a simple rule that matches `id` as a `kind` (with the legacy matching strategy), unless a
|
||||
/// simple rule of that kind and id is there already. A rule of another kind with the same id, or a
|
||||
/// composite rule, does not stop it.
|
||||
pub fn push_simple_rule(rules: &mut Vec<MatchingRule>, kind: ApplicationIdentifier, id: &str) {
|
||||
let exists = rules
|
||||
.iter()
|
||||
.any(|r| matches!(r, MatchingRule::Simple(r) if r.kind == kind && r.id.eq(id)));
|
||||
|
||||
if !exists {
|
||||
rules.push(MatchingRule::Simple(IdWithIdentifier {
|
||||
kind,
|
||||
id: id.to_string(),
|
||||
matching_strategy: Option::from(MatchingStrategy::Legacy),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -158,4 +175,26 @@ mod tests {
|
||||
assert_eq!(plain.id, "/usr/bin/kcalc");
|
||||
assert_eq!(plain.matching_strategy, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn push_simple_rule_keeps_rules_of_another_kind_with_the_same_id() {
|
||||
let mut rules = vec![];
|
||||
|
||||
push_simple_rule(&mut rules, ApplicationIdentifier::Class, "foo");
|
||||
push_simple_rule(&mut rules, ApplicationIdentifier::Exe, "foo");
|
||||
assert_eq!(rules.len(), 2);
|
||||
|
||||
push_simple_rule(&mut rules, ApplicationIdentifier::Exe, "foo");
|
||||
push_simple_rule(&mut rules, ApplicationIdentifier::Class, "foo");
|
||||
assert_eq!(rules.len(), 2);
|
||||
|
||||
rules.clear();
|
||||
rules.push(MatchingRule::Composite(vec![IdWithIdentifier {
|
||||
kind: ApplicationIdentifier::Exe,
|
||||
id: "foo".to_string(),
|
||||
matching_strategy: None,
|
||||
}]));
|
||||
push_simple_rule(&mut rules, ApplicationIdentifier::Exe, "foo");
|
||||
assert_eq!(rules.len(), 2);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user