Files
EchoionCore/src/state/StateSerializer.cpp
T
faumarayandClaude Opus 4.8 23759899bb Use Api::fail() for error returns (241 sites)
Mechanically convert std::unexpected(Api::Error(ErrorCode::X, msg[, src]))
to Api::fail(ErrorCode::X, msg[, src]) across Core. Pure syntax; forwards of
existing errors (std::unexpected(x.error())) are unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-22 22:07:18 +03:00

134 lines
3.6 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)
{
// Quick extraction without full parse
// Look for "version": N pattern
size_t pos = jsonStr.find("\"version\"");
if (pos != std::string::npos) {
pos = jsonStr.find(':', pos);
if (pos != std::string::npos) {
// Skip whitespace
pos++;
while (pos < jsonStr.size() &&
std::isspace(static_cast<unsigned char>(jsonStr[pos]))) {
pos++;
}
// Parse number
if (pos < jsonStr.size() &&
std::isdigit(static_cast<unsigned char>(jsonStr[pos]))) {
try {
return static_cast<uint32_t>(
std::stoul(jsonStr.substr(pos)));
} catch (...) {
return 0;
}
}
}
}
// Default to 0 (unknown/legacy)
return 0;
}
} // namespace Echoion::Core