2020-10-08 18:36:51 +03:00
|
|
|
#include "realisation.hh"
|
|
|
|
#include "store-api.hh"
|
2020-12-08 22:07:52 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
2020-10-08 18:36:51 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
MakeError(InvalidDerivationOutputId, Error);
|
|
|
|
|
|
|
|
DrvOutput DrvOutput::parse(const std::string &strRep) {
|
2020-12-09 17:56:56 +02:00
|
|
|
size_t n = strRep.find("!");
|
|
|
|
if (n == strRep.npos)
|
2020-10-08 18:36:51 +03:00
|
|
|
throw InvalidDerivationOutputId("Invalid derivation output id %s", strRep);
|
|
|
|
|
|
|
|
return DrvOutput{
|
2020-12-09 17:56:56 +02:00
|
|
|
.drvHash = Hash::parseAnyPrefixed(strRep.substr(0, n)),
|
|
|
|
.outputName = strRep.substr(n+1),
|
2020-10-08 18:36:51 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DrvOutput::to_string() const {
|
2020-12-09 17:56:56 +02:00
|
|
|
return strHash() + "!" + outputName;
|
2020-10-08 18:36:51 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 22:07:52 +02:00
|
|
|
nlohmann::json Realisation::toJSON() const {
|
|
|
|
return nlohmann::json{
|
|
|
|
{"id", id.to_string()},
|
|
|
|
{"outPath", outPath.to_string()},
|
|
|
|
};
|
2020-10-08 18:36:51 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 22:07:52 +02:00
|
|
|
Realisation Realisation::fromJSON(
|
|
|
|
const nlohmann::json& json,
|
|
|
|
const std::string& whence) {
|
|
|
|
auto getField = [&](std::string fieldName) -> std::string {
|
|
|
|
auto fieldIterator = json.find(fieldName);
|
|
|
|
if (fieldIterator == json.end())
|
|
|
|
throw Error(
|
|
|
|
"Drv output info file '%1%' is corrupt, missing field %2%",
|
|
|
|
whence, fieldName);
|
|
|
|
return *fieldIterator;
|
2020-10-08 18:36:51 +03:00
|
|
|
};
|
|
|
|
|
2020-12-08 22:07:52 +02:00
|
|
|
return Realisation{
|
|
|
|
.id = DrvOutput::parse(getField("id")),
|
|
|
|
.outPath = StorePath(getField("outPath")),
|
2020-10-08 18:36:51 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace nix
|