2021-10-25 16:53:01 +03:00
# include "experimental-features.hh"
2021-10-26 15:29:48 +03:00
# include "util.hh"
2021-10-25 16:53:01 +03:00
# include "nlohmann/json.hpp"
namespace nix {
std : : map < ExperimentalFeature , std : : string > stringifiedXpFeatures = {
{ Xp : : CaDerivations , " ca-derivations " } ,
2022-03-30 17:31:01 +03:00
{ Xp : : ImpureDerivations , " impure-derivations " } ,
2021-10-25 16:53:01 +03:00
{ Xp : : Flakes , " flakes " } ,
{ Xp : : NixCommand , " nix-command " } ,
{ Xp : : RecursiveNix , " recursive-nix " } ,
{ Xp : : NoUrlLiterals , " no-url-literals " } ,
2022-03-24 22:33:20 +02:00
{ Xp : : FetchClosure , " fetch-closure " } ,
2022-05-20 15:20:00 +03:00
{ Xp : : ReplFlake , " repl-flake " } ,
2021-10-25 16:53:01 +03:00
} ;
const std : : optional < ExperimentalFeature > parseExperimentalFeature ( const std : : string_view & name )
{
using ReverseXpMap = std : : map < std : : string_view , ExperimentalFeature > ;
2021-10-26 15:29:48 +03:00
static auto reverseXpMap = [ ] ( )
{
auto reverseXpMap = std : : make_unique < ReverseXpMap > ( ) ;
2021-10-25 16:53:01 +03:00
for ( auto & [ feature , name ] : stringifiedXpFeatures )
( * reverseXpMap ) [ name ] = feature ;
2021-10-26 15:29:48 +03:00
return reverseXpMap ;
} ( ) ;
2021-10-25 16:53:01 +03:00
2021-10-26 15:29:48 +03:00
if ( auto feature = get ( * reverseXpMap , name ) )
return * feature ;
else
2021-10-25 16:53:01 +03:00
return std : : nullopt ;
}
std : : string_view showExperimentalFeature ( const ExperimentalFeature feature )
{
2022-05-04 08:44:32 +03:00
const auto ret = get ( stringifiedXpFeatures , feature ) ;
assert ( ret ) ;
return * ret ;
2021-10-25 16:53:01 +03:00
}
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 )
2022-09-05 16:42:10 +03:00
: Error ( " experimental Nix feature '%1%' is disabled; pass '--extra-experimental-features %1%' as an argument to enable it " , showExperimentalFeature ( feature ) )
2021-10-25 16:53:01 +03:00
, missingFeature ( feature )
2021-10-26 15:29:48 +03:00
{ }
2021-10-25 16:53:01 +03:00
std : : ostream & operator < < ( std : : ostream & str , const ExperimentalFeature & feature )
{
return str < < showExperimentalFeature ( feature ) ;
}
2022-05-03 15:37:28 +03:00
void to_json ( nlohmann : : json & j , const ExperimentalFeature & feature )
{
2022-04-20 16:41:01 +03:00
j = showExperimentalFeature ( feature ) ;
}
2022-05-03 15:37:28 +03:00
void from_json ( const nlohmann : : json & j , ExperimentalFeature & feature )
{
2022-04-20 16:41:01 +03:00
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 ) ;
}
2021-10-25 16:53:01 +03:00
}