#include "alternative_registry.h" #include namespace alternatives { namespace { const Candidate* automatic_choice(const Group& group) { const Candidate* best = nullptr; for (const auto& candidate : group.candidates) { if (!candidate.enabled) { continue; } if (best == nullptr || candidate.priority > best->priority || (candidate.priority == best->priority && candidate.path < best->path)) { best = &candidate; } } return best; } const Candidate* selected_candidate(const Group& group) { if (group.mode == Mode::automatic) { return automatic_choice(group); } for (const auto& candidate : group.candidates) { if (candidate.path == group.pin) { return candidate.enabled ? &candidate : nullptr; } } return nullptr; } std::string attachment_target(const Group& group, const Candidate* selected, const std::string& slot) { (void)group; if (selected != nullptr) { for (const auto& attachment : selected->attachments) { if (attachment.slot == slot) { return attachment.target; } } } return {}; } } Projection project(const World& world) { Projection result; for (const auto& group : world.groups) { const Candidate* selected = selected_candidate(group); Selection selection; selection.group = group.name; selection.mode = group.mode; selection.pin = group.pin; if (selected != nullptr) { selection.current = selected->path; } result.selections.push_back(std::move(selection)); Binding master; master.link = group.master_link; if (selected != nullptr) { master.target = selected->path; } result.bindings.push_back(std::move(master)); for (const auto& slot : group.slots) { result.bindings.push_back({slot.link, attachment_target(group, selected, slot.name)}); } } std::sort(result.bindings.begin(), result.bindings.end(), [](const Binding& a, const Binding& b) { return a.link < b.link; }); return result; } } // namespace alternatives