mirror of
https://github.com/NixOS/nix
synced 2025-06-27 08:31:16 +02:00
Make experimental-features a proper type
Rather than having them plain strings scattered through the whole codebase, create an enum containing all the known experimental features. This means that - Nix can now `warn` when an unkwown experimental feature is passed (making it much nicer to spot typos and spot deprecated features) - It’s now easy to remove a feature altogether (once the feature isn’t experimental anymore or is dropped) by just removing the field for the enum and letting the compiler point us to all the now invalid usages of it.
This commit is contained in:
parent
4a2b7cc68c
commit
af99941279
28 changed files with 188 additions and 61 deletions
55
src/libutil/experimental-features.cc
Normal file
55
src/libutil/experimental-features.cc
Normal file
|
@ -0,0 +1,55 @@
|
|||
#include "experimental-features.hh"
|
||||
#include "nlohmann/json.hpp"
|
||||
|
||||
namespace nix {
|
||||
|
||||
std::map<ExperimentalFeature, std::string> stringifiedXpFeatures = {
|
||||
{ Xp::CaDerivations, "ca-derivations" },
|
||||
{ Xp::Flakes, "flakes" },
|
||||
{ Xp::NixCommand, "nix-command" },
|
||||
{ Xp::RecursiveNix, "recursive-nix" },
|
||||
{ Xp::NoUrlLiterals, "no-url-literals" },
|
||||
};
|
||||
|
||||
const std::optional<ExperimentalFeature> parseExperimentalFeature(const std::string_view & name)
|
||||
{
|
||||
using ReverseXpMap = std::map<std::string_view, ExperimentalFeature>;
|
||||
static ReverseXpMap * reverseXpMap;
|
||||
if (!reverseXpMap) {
|
||||
reverseXpMap = new ReverseXpMap{};
|
||||
for (auto & [feature, name] : stringifiedXpFeatures)
|
||||
(*reverseXpMap)[name] = feature;
|
||||
}
|
||||
|
||||
auto featureIter = reverseXpMap->find(name);
|
||||
if (featureIter == reverseXpMap->end())
|
||||
return std::nullopt;
|
||||
return {featureIter->second};
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue