forked from lix-project/lix
Yorick
ebf2fd76b1
nix show-config --json was serializing experimental features as ints. nlohmann::json will automatically use these definitions to serialize and deserialize ExperimentalFeatures. Strictly, we don't use the from_json instance yet, it's provided for completeness and hopefully future use.
76 lines
2.2 KiB
C++
76 lines
2.2 KiB
C++
#include "experimental-features.hh"
|
|
#include "util.hh"
|
|
|
|
#include "nlohmann/json.hpp"
|
|
|
|
namespace nix {
|
|
|
|
std::map<ExperimentalFeature, std::string> stringifiedXpFeatures = {
|
|
{ Xp::CaDerivations, "ca-derivations" },
|
|
{ Xp::ImpureDerivations, "impure-derivations" },
|
|
{ Xp::Flakes, "flakes" },
|
|
{ Xp::NixCommand, "nix-command" },
|
|
{ Xp::RecursiveNix, "recursive-nix" },
|
|
{ Xp::NoUrlLiterals, "no-url-literals" },
|
|
{ Xp::FetchClosure, "fetch-closure" },
|
|
};
|
|
|
|
const std::optional<ExperimentalFeature> parseExperimentalFeature(const std::string_view & name)
|
|
{
|
|
using ReverseXpMap = std::map<std::string_view, ExperimentalFeature>;
|
|
|
|
static auto reverseXpMap = []()
|
|
{
|
|
auto reverseXpMap = std::make_unique<ReverseXpMap>();
|
|
for (auto & [feature, name] : stringifiedXpFeatures)
|
|
(*reverseXpMap)[name] = feature;
|
|
return reverseXpMap;
|
|
}();
|
|
|
|
if (auto feature = get(*reverseXpMap, name))
|
|
return *feature;
|
|
else
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::string_view showExperimentalFeature(const ExperimentalFeature feature)
|
|
{
|
|
return stringifiedXpFeatures.at(feature);
|
|
}
|
|
|
|
std::set<ExperimentalFeature> parseFeatures(const std::set<std::string> & rawFeatures)
|
|
{
|
|
std::set<ExperimentalFeature> res;
|
|
for (auto & rawFeature : rawFeatures) {
|
|
if (auto feature = parseExperimentalFeature(rawFeature))
|
|
res.insert(*feature);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
MissingExperimentalFeature::MissingExperimentalFeature(ExperimentalFeature feature)
|
|
: Error("experimental Nix feature '%1%' is disabled; use '--extra-experimental-features %1%' to override", showExperimentalFeature(feature))
|
|
, missingFeature(feature)
|
|
{}
|
|
|
|
std::ostream & operator <<(std::ostream & str, const ExperimentalFeature & feature)
|
|
{
|
|
return str << showExperimentalFeature(feature);
|
|
}
|
|
|
|
void to_json(nlohmann::json& j, const ExperimentalFeature& feature) {
|
|
j = showExperimentalFeature(feature);
|
|
}
|
|
|
|
void from_json(const nlohmann::json& j, ExperimentalFeature& feature) {
|
|
const std::string input = j;
|
|
const auto parsed = parseExperimentalFeature(input);
|
|
|
|
if (parsed.has_value())
|
|
feature = *parsed;
|
|
else
|
|
throw Error("Unknown experimental feature '%s' in JSON input", input);
|
|
}
|
|
|
|
}
|