diff --git a/src/libexpr/primops/fromTOML.cc b/src/libexpr/primops/fromTOML.cc index b4f1df7a8..0b6c21f99 100644 --- a/src/libexpr/primops/fromTOML.cc +++ b/src/libexpr/primops/fromTOML.cc @@ -7,7 +7,7 @@ namespace nix { -static void prim_fromTOML(EvalState & state, const PosIdx pos, Value * * args, Value & val) +static void prim_fromTOML(EvalState & state, const PosIdx pos, Value ** args, Value & val) { auto toml = state.forceStringNoCtx(*args[0], pos, "while evaluating the argument passed to builtins.fromTOML"); @@ -16,67 +16,69 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value * * args, V std::function visit; visit = [&](Value & v, toml::value t) { + switch (t.type()) { + case toml::value_t::table: { + auto table = toml::get(t); - switch(t.type()) - { - case toml::value_t::table: - { - auto table = toml::get(t); + size_t size = 0; + for (auto & i : table) { + (void) i; + size++; + } - size_t size = 0; - for (auto & i : table) { (void) i; size++; } + auto attrs = state.buildBindings(size); - auto attrs = state.buildBindings(size); + for (auto & elem : table) + visit(attrs.alloc(elem.first), elem.second); - for(auto & elem : table) - visit(attrs.alloc(elem.first), elem.second); - - v.mkAttrs(attrs); - } - break;; - case toml::value_t::array: - { - auto array = toml::get>(t); - - auto list = state.buildList(array.size()); - for (const auto & [n, v] : enumerate(list)) - visit(*(v = state.allocValue()), array[n]); - v.mkList(list); - } - break;; - case toml::value_t::boolean: - v.mkBool(toml::get(t)); - break;; - case toml::value_t::integer: - v.mkInt(toml::get(t)); - break;; - case toml::value_t::floating: - v.mkFloat(toml::get(t)); - break;; - case toml::value_t::string: - v.mkString(toml::get(t)); - break;; - case toml::value_t::local_datetime: - case toml::value_t::offset_datetime: - case toml::value_t::local_date: - case toml::value_t::local_time: - { - if (experimentalFeatureSettings.isEnabled(Xp::ParseTomlTimestamps)) { - auto attrs = state.buildBindings(2); - attrs.alloc("_type").mkString("timestamp"); - std::ostringstream s; - s << t; - attrs.alloc("value").mkString(s.str()); - v.mkAttrs(attrs); - } else { - throw std::runtime_error("Dates and times are not supported"); - } - } - break;; - case toml::value_t::empty: - v.mkNull(); - break;; + v.mkAttrs(attrs); + } break; + ; + case toml::value_t::array: { + auto array = toml::get>(t); + auto list = state.buildList(array.size()); + for (const auto & [n, v] : enumerate(list)) + visit(*(v = state.allocValue()), array[n]); + v.mkList(list); + } break; + ; + case toml::value_t::boolean: + v.mkBool(toml::get(t)); + break; + ; + case toml::value_t::integer: + v.mkInt(toml::get(t)); + break; + ; + case toml::value_t::floating: + v.mkFloat(toml::get(t)); + break; + ; + case toml::value_t::string: + v.mkString(toml::get(t)); + break; + ; + case toml::value_t::local_datetime: + case toml::value_t::offset_datetime: + case toml::value_t::local_date: + case toml::value_t::local_time: { + if (experimentalFeatureSettings.isEnabled(Xp::ParseTomlTimestamps)) { + auto attrs = state.buildBindings(2); + attrs.alloc("_type").mkString("timestamp"); + std::ostringstream s; + s << t; + attrs.alloc("value").mkString(s.str()); + v.mkAttrs(attrs); + } else { + throw std::runtime_error("Dates and times are not supported"); + } + } break; + ; + case toml::value_t::empty: + v.mkNull(); + break; + ; } }; @@ -87,10 +89,10 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value * * args, V } } -static RegisterPrimOp primop_fromTOML({ - .name = "fromTOML", - .args = {"e"}, - .doc = R"( +static RegisterPrimOp primop_fromTOML( + {.name = "fromTOML", + .args = {"e"}, + .doc = R"( Convert a TOML string to a Nix value. For example, ```nix @@ -104,7 +106,6 @@ static RegisterPrimOp primop_fromTOML({ returns the value `{ s = "a"; table = { y = 2; }; x = 1; }`. )", - .fun = prim_fromTOML -}); + .fun = prim_fromTOML}); }