Phosphor
Qt6 / Wayland library suite for window-management tools
Loading...
Searching...
No Matches
MatchExpression.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include <QJsonObject>
7#include <QList>
8#include <QRegularExpression>
9#include <QSet>
10#include <QString>
11#include <QVariant>
12
13#include <memory>
14#include <optional>
15
16#include "MatchTypes.h"
17#include "WindowQuery.h"
18#include "phosphorrules_export.h"
19
20namespace PhosphorRules {
21
49class PHOSPHORRULES_EXPORT MatchExpression
50{
51public:
53 enum class Kind {
54 Leaf,
55 All,
56 Any,
57 None,
58 };
59
61 struct Predicate
62 {
65 QVariant value;
66
67 bool operator==(const Predicate& other) const
68 {
69 return field == other.field && op == other.op && value == other.value;
70 }
71 };
72
75 : m_kind(Kind::All)
76 {
77 }
78
81 static MatchExpression makeLeaf(Field field, Operator op, const QVariant& value);
82
84 static MatchExpression makeAll(const QList<MatchExpression>& children);
85 static MatchExpression makeAny(const QList<MatchExpression>& children);
86 static MatchExpression makeNone(const QList<MatchExpression>& children);
87
88 Kind kind() const
89 {
90 return m_kind;
91 }
92 bool isLeaf() const
93 {
94 return m_kind == Kind::Leaf;
95 }
96
98 const Predicate& predicate() const
99 {
100 return m_predicate;
101 }
102
104 const QList<MatchExpression>& children() const
105 {
106 return m_children;
107 }
108
110 bool isCatchAll() const
111 {
112 return m_kind == Kind::All && m_children.isEmpty();
113 }
114
118 bool isContextOnly() const;
119
128 bool referencesAnyField(const QSet<Field>& fields) const;
129
142 bool negatesAnyField(const QSet<Field>& fields) const;
143
154 bool evaluate(const WindowQuery& query) const;
155
159 bool isValid() const;
160
161 QJsonObject toJson() const;
162
170 static std::optional<MatchExpression> fromJson(const QJsonObject& obj);
171
175 static constexpr int kMaxParseDepth = 32;
176
177 bool operator==(const MatchExpression& other) const;
178 bool operator!=(const MatchExpression& other) const
179 {
180 return !(*this == other);
181 }
182
183private:
184 Kind m_kind = Kind::All;
185 Predicate m_predicate;
186 QList<MatchExpression> m_children;
187
188 // Eagerly compiled regex for a `Regex` leaf — built once by ensureRegex()
189 // at construction / load time, never mutated by evaluate(). `shared_ptr`
190 // so value-copies of the expression share the compiled program rather
191 // than recompiling.
192 std::shared_ptr<QRegularExpression> m_compiledRegex;
193
194 bool evaluateLeaf(const WindowQuery& query) const;
195
196 // Compiles the `Regex` leaf's pattern into m_compiledRegex if this is a
197 // Regex leaf and the program is not yet built. Idempotent; called from
198 // every construction path so evaluate() never has to compile.
199 void ensureRegex();
200
204 static std::optional<MatchExpression> fromJsonAtDepth(const QJsonObject& obj, int depth);
205};
206
207} // namespace PhosphorRules
Kind
Tree node kind.
Definition MatchExpression.h:53
@ All
AND — every child must match.
Definition MatchExpression.h:55
@ Leaf
a single field/op/value predicate
Definition MatchExpression.h:54
static MatchExpression makeLeaf(const Predicate &predicate)
Constructs a leaf predicate expression.
static MatchExpression makeLeaf(Field field, Operator op, const QVariant &value)
bool negatesAnyField(const QSet< Field > &fields) const
True if a leaf predicate on one of fields appears anywhere INSIDE a none{} negation (at any nesting d...
static MatchExpression makeNone(const QList< MatchExpression > &children)
bool isContextOnly() const
True if this expression references only context fields (ScreenId / VirtualDesktop / Activity / Mode /...
MatchExpression()
Default-constructs an empty All{} — the always-true catch-all.
Definition MatchExpression.h:74
static std::optional< MatchExpression > fromJson(const QJsonObject &obj)
Strict loader — returns nullopt on a malformed / invalid expression.
Kind kind() const
Definition MatchExpression.h:88
bool operator!=(const MatchExpression &other) const
Definition MatchExpression.h:178
const QList< MatchExpression > & children() const
Composite accessor — empty for a leaf.
Definition MatchExpression.h:104
const Predicate & predicate() const
Leaf accessor — only meaningful when isLeaf().
Definition MatchExpression.h:98
static MatchExpression makeAny(const QList< MatchExpression > &children)
bool operator==(const MatchExpression &other) const
bool isCatchAll() const
True if this is the empty catch-all All{} — matches every query.
Definition MatchExpression.h:110
bool evaluate(const WindowQuery &query) const
Evaluate this expression against query.
static MatchExpression makeAll(const QList< MatchExpression > &children)
Constructs a composite expression.
bool referencesAnyField(const QSet< Field > &fields) const
True if any leaf predicate anywhere in the tree (including inside none{} negations) tests one of fiel...
static constexpr int kMaxParseDepth
Hard cap on the JSON parser's recursion depth.
Definition MatchExpression.h:175
QJsonObject toJson() const
bool isLeaf() const
Definition MatchExpression.h:92
bool isValid() const
True if this expression is structurally well-formed: every leaf has a field/operator combination that...
Definition ActionParams.h:25
Field
Match fields.
Definition MatchTypes.h:23
@ AppId
Definition MatchTypes.h:25
Operator
Match operators.
Definition MatchTypes.h:441
@ Equals
case-insensitive for strings, numeric for ints, bool for flags
Definition MatchTypes.h:442
Leaf predicate payload.
Definition MatchExpression.h:62
Operator op
Definition MatchExpression.h:64
QVariant value
Definition MatchExpression.h:65
bool operator==(const Predicate &other) const
Definition MatchExpression.h:67
Field field
Definition MatchExpression.h:63
The evaluation input — an attribute bag matched against a MatchExpression.
Definition WindowQuery.h:37