Files
EchoionCore/src/state/StateSerializer.cpp
T
faumaray 39aff68d8d Update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-15 19:49:57 +03:00

113 lines
3.3 KiB
C++

// #include "echoion/api/base/StateBlob.hpp"
#include "echoion/core/state/StateMigration.hpp"
#include "echoion/core/state/StateSerializer.hpp"
#include <Void/Data/Json.hpp>
#include <Void/Data/Reflect.hpp>
#include <Void/Time/Format.hpp>
#include <format>
namespace Echoion::Core
{
Api::StateBlob StateSerializer::serialize(const StateV1& state)
{
// Serialize to JSON using Void::Json reflection
std::string jsonStr =
Void::Json::serialize(Void::Json::to_json(state));
// Convert to binary blob
std::vector<uint8_t> data(jsonStr.begin(), jsonStr.end());
return Api::StateBlob{.data = std::move(data),
.version = CURRENT_VERSION,
.timestamp = Void::Time::utc_now_iso8601()};
}
Api::Result<StateV1>
StateSerializer::deserialize(const Api::StateBlob& blob)
{
if (blob.data.empty()) {
return Api::fail(Api::ErrorCode::InvalidArgument,
"Empty state blob");
}
// Convert blob to string
std::string jsonStr(blob.data.begin(), blob.data.end());
// Extract version
uint32_t dataVersion = extractVersion(jsonStr);
// Check if version is supported
if (!isVersionSupported(dataVersion)) {
if (dataVersion > CURRENT_VERSION) {
return Api::fail(
Api::ErrorCode::IncompatibleVersion,
std::format("State version {} is from a newer Core. "
"Please update/restart to load this state.",
dataVersion));
} else {
return Api::fail(
Api::ErrorCode::IncompatibleVersion,
std::format("State version {} is too old. "
"Maximum supported age is {} versions.",
dataVersion,
Api::MAX_SUPPORTED_STATE_VERSION_AGE));
}
}
// If current version, parse directly
if (dataVersion == CURRENT_VERSION) {
auto parsed = Void::Json::parse(jsonStr);
if (!parsed) {
return Api::fail(
Api::ErrorCode::ParseError,
std::format("Failed to parse state ({}:{}): {}",
parsed.error().line, parsed.error().col,
parsed.error().message));
}
auto state = Void::Json::from_json<StateV1>(*parsed);
if (!state) {
return Api::fail(
Api::ErrorCode::ParseError,
std::format("Failed to decode state: field '{}': {}",
state.error().field, state.error().message));
}
return *state;
}
// Otherwise, migrate
return StateMigration::migrate(dataVersion, jsonStr);
}
bool StateSerializer::isVersionSupported(uint32_t version)
{
if (version > CURRENT_VERSION) {
return false; // Future version
}
uint32_t minVersion = getMinimumSupportedVersion();
return version >= minVersion && version <= CURRENT_VERSION;
}
uint32_t StateSerializer::getMinimumSupportedVersion()
{
// Support 10 versions back, but not below 1
return (CURRENT_VERSION > Api::MAX_SUPPORTED_STATE_VERSION_AGE)
? CURRENT_VERSION - Api::MAX_SUPPORTED_STATE_VERSION_AGE
: 1;
}
uint32_t StateSerializer::getCurrentVersion() { return CURRENT_VERSION; }
uint32_t StateSerializer::extractVersion(const std::string& jsonStr)
{
// Read only the root field. A textual search can accidentally select a
// nested schema version (for example an extension plugin's state).
auto parsed = Void::Json::parse(jsonStr);
if (!parsed || !parsed->is_object()) return 0;
const auto version = parsed->get_num("version", 0);
return version > 0 ? static_cast<uint32_t>(version) : 0;
}
} // namespace Echoion::Core