2018-07-03 19:15:59 +03:00
|
|
|
#include "primops.hh"
|
|
|
|
#include "eval-inline.hh"
|
|
|
|
|
2021-12-17 23:03:48 +02:00
|
|
|
#include "../../toml11/toml.hpp"
|
2018-07-03 19:15:59 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2021-12-17 23:03:48 +02:00
|
|
|
static void prim_fromTOML(EvalState & state, const Pos & pos, Value * * args, Value & val)
|
2018-07-03 19:15:59 +03:00
|
|
|
{
|
|
|
|
auto toml = state.forceStringNoCtx(*args[0], pos);
|
|
|
|
|
|
|
|
std::istringstream tomlStream(toml);
|
|
|
|
|
2021-12-17 23:03:48 +02:00
|
|
|
std::function<void(Value &, toml::value)> visit;
|
2018-07-03 19:15:59 +03:00
|
|
|
|
2021-12-17 23:03:48 +02:00
|
|
|
visit = [&](Value & v, toml::value t) {
|
2018-07-03 19:15:59 +03:00
|
|
|
|
2021-12-17 23:03:48 +02:00
|
|
|
switch(t.type())
|
|
|
|
{
|
|
|
|
case toml::value_t::table:
|
|
|
|
{
|
|
|
|
auto table = toml::get<toml::table>(t);
|
2018-07-03 19:15:59 +03:00
|
|
|
|
2021-12-17 23:03:48 +02:00
|
|
|
size_t size = 0;
|
|
|
|
for (auto & i : table) { (void) i; size++; }
|
2018-07-03 19:15:59 +03:00
|
|
|
|
2021-12-17 23:03:48 +02:00
|
|
|
state.mkAttrs(v, size);
|
2018-07-03 19:15:59 +03:00
|
|
|
|
2021-12-17 23:03:48 +02:00
|
|
|
for(auto & elem: table) {
|
2019-06-24 18:09:09 +03:00
|
|
|
|
2021-12-17 23:03:48 +02:00
|
|
|
auto & v2 = *state.allocAttr(v, state.symbols.create(elem.first));
|
|
|
|
visit(v2, elem.second);
|
|
|
|
}
|
2021-12-28 17:10:58 +02:00
|
|
|
|
|
|
|
v.attrs->sort();
|
2021-12-17 23:03:48 +02:00
|
|
|
}
|
|
|
|
break;;
|
|
|
|
case toml::value_t::array:
|
|
|
|
{
|
|
|
|
auto array = toml::get<std::vector<toml::value>>(t);
|
|
|
|
|
|
|
|
size_t size = array.size();
|
|
|
|
state.mkList(v, size);
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
visit(*(v.listElems()[i] = state.allocValue()), array[i]);
|
|
|
|
}
|
|
|
|
break;;
|
|
|
|
case toml::value_t::boolean:
|
|
|
|
mkBool(v, toml::get<bool>(t));
|
|
|
|
break;;
|
|
|
|
case toml::value_t::integer:
|
2021-12-23 15:12:48 +02:00
|
|
|
mkInt(v, toml::get<int64_t>(t));
|
2021-12-17 23:03:48 +02:00
|
|
|
break;;
|
|
|
|
case toml::value_t::floating:
|
2021-12-23 15:12:48 +02:00
|
|
|
mkFloat(v, toml::get<NixFloat>(t));
|
2021-12-17 23:03:48 +02:00
|
|
|
break;;
|
|
|
|
case toml::value_t::string:
|
|
|
|
mkString(v, toml::get<std::string>(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:
|
2021-12-20 22:39:37 +02:00
|
|
|
// We fail since Nix doesn't have date and time types
|
2021-12-17 23:03:48 +02:00
|
|
|
throw std::runtime_error("Dates and times are not supported");
|
|
|
|
break;;
|
|
|
|
case toml::value_t::empty:
|
|
|
|
mkNull(v);
|
|
|
|
break;;
|
2019-06-24 18:09:09 +03:00
|
|
|
|
2018-07-03 19:15:59 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2021-12-20 22:39:37 +02:00
|
|
|
visit(val, toml::parse(tomlStream, "fromTOML" /* the "filename" */));
|
2021-12-17 23:03:48 +02:00
|
|
|
} catch (std::exception & e) { // TODO: toml::syntax_error
|
2020-06-15 15:06:58 +03:00
|
|
|
throw EvalError({
|
2021-01-21 01:27:36 +02:00
|
|
|
.msg = hintfmt("while parsing a TOML string: %s", e.what()),
|
2020-06-24 00:30:13 +03:00
|
|
|
.errPos = pos
|
2020-06-15 15:06:58 +03:00
|
|
|
});
|
2018-07-03 19:15:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 14:36:55 +03:00
|
|
|
static RegisterPrimOp primop_fromTOML("fromTOML", 1, prim_fromTOML);
|
2018-07-03 19:15:59 +03:00
|
|
|
|
|
|
|
}
|