1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-11 13:06:01 +01:00

libexpr: Format primops/fromTOML.cc

This commit is contained in:
Sergei Zimmerman 2025-08-13 22:37:03 +03:00
parent d52210fae3
commit 79b932277a
No known key found for this signature in database

View file

@ -16,15 +16,15 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value * * args, V
std::function<void(Value &, toml::value)> visit;
visit = [&](Value & v, toml::value t) {
switch(t.type())
{
case toml::value_t::table:
{
switch (t.type()) {
case toml::value_t::table: {
auto table = toml::get<toml::table>(t);
size_t size = 0;
for (auto & i : table) { (void) i; size++; }
for (auto & i : table) {
(void) i;
size++;
}
auto attrs = state.buildBindings(size);
@ -32,35 +32,37 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value * * args, V
visit(attrs.alloc(elem.first), elem.second);
v.mkAttrs(attrs);
}
break;;
case toml::value_t::array:
{
} break;
;
case toml::value_t::array: {
auto array = toml::get<std::vector<toml::value>>(t);
auto list = state.buildList(array.size());
for (const auto & [n, v] : enumerate(list))
visit(*(v = state.allocValue()), array[n]);
v.mkList(list);
}
break;;
} break;
;
case toml::value_t::boolean:
v.mkBool(toml::get<bool>(t));
break;;
break;
;
case toml::value_t::integer:
v.mkInt(toml::get<int64_t>(t));
break;;
break;
;
case toml::value_t::floating:
v.mkFloat(toml::get<NixFloat>(t));
break;;
break;
;
case toml::value_t::string:
v.mkString(toml::get<std::string>(t));
break;;
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:
{
case toml::value_t::local_time: {
if (experimentalFeatureSettings.isEnabled(Xp::ParseTomlTimestamps)) {
auto attrs = state.buildBindings(2);
attrs.alloc("_type").mkString("timestamp");
@ -71,12 +73,12 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value * * args, V
} else {
throw std::runtime_error("Dates and times are not supported");
}
}
break;;
} break;
;
case toml::value_t::empty:
v.mkNull();
break;;
break;
;
}
};
@ -87,8 +89,8 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value * * args, V
}
}
static RegisterPrimOp primop_fromTOML({
.name = "fromTOML",
static RegisterPrimOp primop_fromTOML(
{.name = "fromTOML",
.args = {"e"},
.doc = R"(
Convert a TOML string to a Nix value. For example,
@ -104,7 +106,6 @@ static RegisterPrimOp primop_fromTOML({
returns the value `{ s = "a"; table = { y = 2; }; x = 1; }`.
)",
.fun = prim_fromTOML
});
.fun = prim_fromTOML});
}