From c586596a9fba2b95a5bba1d72b8039d7c7e8d75e Mon Sep 17 00:00:00 2001 From: Emily Date: Thu, 14 Aug 2025 16:46:46 +0100 Subject: [PATCH] libexpr: format `fromTOML` source Otherwise the next diff becomes very messy. Change-Id: I6a6a6964d96543ade130d491f413ebd9fe2b7ff1 --- lix/libexpr/primops/fromTOML.cc | 124 +++++++++++++++++--------------- 1 file changed, 65 insertions(+), 59 deletions(-) diff --git a/lix/libexpr/primops/fromTOML.cc b/lix/libexpr/primops/fromTOML.cc index 3d6f3ef28..a0f4087cb 100644 --- a/lix/libexpr/primops/fromTOML.cc +++ b/lix/libexpr/primops/fromTOML.cc @@ -6,77 +6,83 @@ namespace nix { -void prim_fromTOML(EvalState & state, Value * * args, Value & val) +void prim_fromTOML(EvalState & state, Value ** args, Value & val) { - auto toml = state.forceStringNoCtx(*args[0], noPos, "while evaluating the argument passed to builtins.fromTOML"); + auto toml = state.forceStringNoCtx( + *args[0], noPos, "while evaluating the argument passed to builtins.fromTOML" + ); std::istringstream tomlStream(std::string{toml}); 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.ctx.buildBindings(size); - auto attrs = state.ctx.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); - - size_t size = array.size(); - v = state.ctx.mem.newList(size); - for (size_t i = 0; i < size; ++i) - visit(*(v.listElems()[i] = state.ctx.mem.allocValue()), array[i]); - } - 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.ctx.buildBindings(2); - attrs.alloc("_type").mkString("timestamp"); - std::ostringstream s; - s << t; - attrs.alloc("value").mkString(s.str()); - v.mkAttrs(attrs); - } else { - // NOLINTNEXTLINE(lix-foreign-exceptions) - 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); + size_t size = array.size(); + v = state.ctx.mem.newList(size); + for (size_t i = 0; i < size; ++i) { + visit(*(v.listElems()[i] = state.ctx.mem.allocValue()), array[i]); + } + } 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.ctx.buildBindings(2); + attrs.alloc("_type").mkString("timestamp"); + std::ostringstream s; + s << t; + attrs.alloc("value").mkString(s.str()); + v.mkAttrs(attrs); + } else { + // NOLINTNEXTLINE(lix-foreign-exceptions) + throw std::runtime_error("Dates and times are not supported"); + } + } break; + ; + case toml::value_t::empty: + v.mkNull(); + break; + ; } };