From f0891b440f3ef0ce2f4951c179c8f9f1eb98bef7 Mon Sep 17 00:00:00 2001 From: skye Date: Sun, 1 Mar 2026 20:07:39 -0500 Subject: [PATCH] libexpr/primops/fromTOML: Migrate `visit` lambda to return a value Necessary step to replace BindingsBuilder::alloc uses with insert Arguably part of #1136 Change-Id: I72acdd1676cf2dda69bce39e9b7feb656a6a6964 --- lix/libexpr/primops/fromTOML.cc | 37 +++++++++++++-------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/lix/libexpr/primops/fromTOML.cc b/lix/libexpr/primops/fromTOML.cc index b59c807ea..bce233277 100644 --- a/lix/libexpr/primops/fromTOML.cc +++ b/lix/libexpr/primops/fromTOML.cc @@ -15,62 +15,53 @@ void prim_fromTOML(EvalState & state, Value ** args, Value & val) std::istringstream tomlStream(std::string{toml}); - auto visit = [&](this const auto & self, Value & v, toml::value t) -> void { + auto visit = [&](this const auto & self, toml::value t) -> Value { switch (t.type()) { case toml::value_t::table: { auto table = toml::get(t); auto attrs = state.ctx.buildBindings(table.size()); for (auto & elem : table) { - self(attrs.alloc(elem.first), elem.second); + attrs.alloc(elem.first) = self(elem.second); } - v = {NewValueAs::attrs, attrs}; - } break; + return {NewValueAs::attrs, attrs}; + } case toml::value_t::array: { auto array = toml::get>(t); size_t size = array.size(); auto list = state.ctx.mem.newList(size); - v = {NewValueAs::list, list}; for (size_t i = 0; i < size; ++i) { - self(list->elems[i], array[i]); + list->elems[i] = self(array[i]); } - } break; + return {NewValueAs::list, list}; + } case toml::value_t::boolean: - v = {NewValueAs::boolean, toml::get(t)}; - break; + return {NewValueAs::boolean, toml::get(t)}; case toml::value_t::integer: - v = {NewValueAs::integer, toml::get(t)}; - break; + return {NewValueAs::integer, toml::get(t)}; case toml::value_t::floating: - v = {NewValueAs::floating, toml::get(t)}; - break; + return {NewValueAs::floating, toml::get(t)}; case toml::value_t::string: - v = {NewValueAs::string, toml::get(t)}; - break; + return {NewValueAs::string, toml::get(t)}; case toml::value_t::local_datetime: case toml::value_t::offset_datetime: case toml::value_t::local_date: case toml::value_t::local_time: // NOLINTNEXTLINE(lix-foreign-exceptions) throw std::runtime_error("Dates and times are not supported"); - break; case toml::value_t::empty: - v = Value::VNULL; - break; + return Value::VNULL; } }; try { - visit( - val, + val = visit( toml::parse( tomlStream, "fromTOML", /* the "filename" */ - toml::spec::v( - 1, 0, 0 - ) // Be explicit that we are parsing TOML 1.0.0 without extensions + toml::spec::v(1, 0, 0) // Be explicit that we are parsing TOML 1.0.0 without extensions ) ); } catch (std::exception & e) { // NOLINT(lix-foreign-exceptions) // TODO: toml::syntax_error