#include "alternative_registry.h" #include namespace alternatives { namespace { bool event_fields(const Event& event, std::int32_t& priority) { if (!valid_id(event.group)) { return false; } switch (event.op) { case Operation::install: return valid_path(event.path) && parse_priority(event.priority, priority) == Error::none; case Operation::remove: case Operation::select: return valid_path(event.path) && event.priority.empty() && !event.enabled && event.attachments.empty(); case Operation::automatic: return event.path.empty() && event.priority.empty() && !event.enabled && event.attachments.empty(); case Operation::enable: return valid_path(event.path) && event.priority.empty() && event.attachments.empty(); } return false; } bool apply(Group& group, const Event& event, std::int32_t priority) { auto found = std::find_if(group.candidates.begin(), group.candidates.end(), [&](const Candidate& candidate) { return candidate.path == event.path; }); switch (event.op) { case Operation::install: { Candidate candidate{event.path, priority, event.enabled, event.attachments}; if (found == group.candidates.end()) { group.candidates.push_back(std::move(candidate)); } else { *found = std::move(candidate); } return true; } case Operation::remove: if (found != group.candidates.end()) { group.candidates.erase(found); if (group.mode == Mode::manual && group.pin == event.path) { group.mode = Mode::automatic; group.pin.clear(); } } return true; case Operation::select: if (found == group.candidates.end()) { return false; } group.mode = Mode::manual; group.pin = event.path; return true; case Operation::automatic: group.mode = Mode::automatic; group.pin.clear(); return true; case Operation::enable: if (found == group.candidates.end()) { return false; } found->enabled = event.enabled; return true; } return false; } std::vector changes(const Projection& before, const Projection& after) { std::vector result; for (std::size_t i = 0; i < before.bindings.size(); ++i) { if (before.bindings[i].target != after.bindings[i].target) { result.push_back({before.bindings[i].link, before.bindings[i].target, after.bindings[i].target}); } } return result; } } Error replay(const World& initial, const std::vector& events, Transcript& output) { World working = initial; output.final_world.groups.clear(); output.frames.clear(); if (events.size() > 128) { return Error::invalid; } Transcript local; for (const auto& event : events) { std::int32_t priority = 0; if (!event_fields(event, priority)) { return Error::invalid; } auto group = std::find_if(working.groups.begin(), working.groups.end(), [&](const Group& item) { return item.name == event.group; }); if (group == working.groups.end()) { return Error::invalid; } const Projection previous = project(working); if (!apply(*group, event, priority)) { return Error::invalid; } World canonical; if (compile_world(working.groups, canonical) != Error::none) { return Error::invalid; } Projection projected = project(canonical); Frame frame; frame.world = canonical; frame.projection = projected; frame.changes = changes(previous, projected); local.frames.push_back(std::move(frame)); working = std::move(canonical); } local.final_world = std::move(working); output = std::move(local); return Error::none; } } // namespace alternatives