Reintroduce some encapulation

As much as I like keeping things as simple as possible it's probably best to be consistent with other library related stuff

Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
TheKodeToad 2025-08-04 15:41:36 +01:00
parent 3ba9483011
commit aaa1a74875
No known key found for this signature in database
GPG key ID: 5E39D70B4C93C38E
2 changed files with 23 additions and 21 deletions

View file

@ -44,13 +44,13 @@ Rule Rule::fromJson(const QJsonObject& object)
Rule result;
if (object["action"] == "allow")
result.action = Allow;
result.m_action = Allow;
else if (object["action"] == "disallow")
result.action = Disallow;
result.m_action = Disallow;
if (auto os = object["os"]; os.isObject()) {
if (auto name = os["name"].toString(); !name.isNull()) {
result.os = OS{
result.m_os = OS{
name,
os["version"].toString(),
};
@ -64,20 +64,20 @@ QJsonObject Rule::toJson()
{
QJsonObject result;
if (action == Allow)
if (m_action == Allow)
result["action"] = "allow";
else if (action == Disallow)
else if (m_action == Disallow)
result["action"] = "disallow";
if (os.has_value()) {
QJsonObject osResult;
if (m_os.has_value()) {
QJsonObject os;
osResult["name"] = os->name;
os["name"] = m_os->name;
if (!os->version.isEmpty())
osResult["version"] = os->version;
if (!m_os->version.isEmpty())
os["version"] = m_os->version;
result["os"] = osResult;
result["os"] = os;
}
return result;
@ -85,8 +85,8 @@ QJsonObject Rule::toJson()
Rule::Action Rule::apply(const RuntimeContext& runtimeContext)
{
if (!runtimeContext.classifierMatches(os->name))
if (!runtimeContext.classifierMatches(m_os->name))
return Defer;
return action;
return m_action;
}

View file

@ -43,9 +43,16 @@
class Library;
struct Rule {
class Rule {
public:
enum Action { Allow, Disallow, Defer };
static Rule fromJson(const QJsonObject& json);
QJsonObject toJson();
Action apply(const RuntimeContext& runtimeContext);
private:
struct OS {
QString name;
// FIXME: unsupported
@ -53,11 +60,6 @@ struct Rule {
QString version;
};
Action action = Defer;
std::optional<OS> os;
static Rule fromJson(const QJsonObject& json);
QJsonObject toJson();
Action apply(const RuntimeContext& runtimeContext);
Action m_action = Defer;
std::optional<OS> m_os;
};