feat(core): Value, reflection, codecs and results, shared by app and client
Crafter.Debug.Core is what the app side and the crafter-debug client both need, without the session: - Value: a JSON value with its own parser and printer, so no consumer interface sees a JSON library. Integers stay integers and whole doubles stay doubles; NaN and the infinities, which JSON cannot spell, are written as strings rather than hidden, because a debugger must not lose a NaN; invalid UTF-8 is replaced so output always parses. Objects keep insertion order, the order an agent reads. - Reflect: aggregate members and names through structured-binding packs and source_location, taken from Crafter.DB, plus scoped-enum names by probing a range of values. Self-tested with static_asserts so a compiler that changes function_name()'s shape fails the build. - Codec<T>: encode, decode and describe for bool, integers, floating point, strings, enums, optional, vector, array, string-keyed maps, Value, Required<T> and every aggregate. Decode reports every problem in a call with its path and, for a typo, the likely intended name, and never throws: a bad call cannot take an app down, which a wrongly typed parameter could in 3DForts' hand-written from_json. - Result, Error and Async: handler results, including ones that take frames to arrive, so callers need no ready:false polling. project.cpp now builds Crafter.Debug.Core as its own library, used by both the app library and the client. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
fb2c06c32e
commit
89a12bcd61
13 changed files with 1951 additions and 11 deletions
118
implementations/Crafter.Debug.Core-Codec.cpp
Normal file
118
implementations/Crafter.Debug.Core-Codec.cpp
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
module Crafter.Debug.Core:Codec_impl;
|
||||
import std;
|
||||
import :Value;
|
||||
import :Codec;
|
||||
|
||||
using namespace Crafter::Debug;
|
||||
|
||||
namespace {
|
||||
// "a string" or "an integer", for a kind as errors name it.
|
||||
std::string WithArticle(ValueKind kind) {
|
||||
const std::string_view name = KindName(kind);
|
||||
if (kind == ValueKind::Null) return std::string(name);
|
||||
const bool vowel = name.starts_with('a') || name.starts_with('i') || name.starts_with('o');
|
||||
return std::format("{} {}", vowel ? "an" : "a", name);
|
||||
}
|
||||
|
||||
std::size_t Distance(std::string_view a, std::string_view b) {
|
||||
std::vector<std::size_t> previous(b.size() + 1);
|
||||
std::vector<std::size_t> current(b.size() + 1);
|
||||
for (std::size_t j = 0; j <= b.size(); ++j) previous[j] = j;
|
||||
for (std::size_t i = 1; i <= a.size(); ++i) {
|
||||
current[0] = i;
|
||||
for (std::size_t j = 1; j <= b.size(); ++j) {
|
||||
const std::size_t substitution = previous[j - 1] + (a[i - 1] == b[j - 1] ? 0 : 1);
|
||||
current[j] = std::min({ previous[j] + 1, current[j - 1] + 1, substitution });
|
||||
}
|
||||
std::swap(previous, current);
|
||||
}
|
||||
return previous[b.size()];
|
||||
}
|
||||
|
||||
std::string Join(std::span<const std::string_view> names) {
|
||||
std::string joined;
|
||||
for (const std::string_view name : names) {
|
||||
if (!joined.empty()) joined += ", ";
|
||||
joined += name;
|
||||
}
|
||||
return joined;
|
||||
}
|
||||
}
|
||||
|
||||
void DecodeContext::Fail(std::string message) {
|
||||
issues.push_back({ Path(), std::move(message) });
|
||||
}
|
||||
|
||||
std::string DecodeContext::Report() const {
|
||||
std::string report;
|
||||
for (const DecodeIssue& issue : issues) {
|
||||
if (!report.empty()) report += "; ";
|
||||
report += issue.path.empty() ? issue.message : std::format("{}: {}", issue.path, issue.message);
|
||||
}
|
||||
return report;
|
||||
}
|
||||
|
||||
DecodeContext::Scope::~Scope() {
|
||||
context.segments.pop_back();
|
||||
}
|
||||
|
||||
DecodeContext::Scope DecodeContext::Field(std::string_view name) {
|
||||
segments.emplace_back(name);
|
||||
return Scope(*this);
|
||||
}
|
||||
|
||||
DecodeContext::Scope DecodeContext::Index(std::size_t index) {
|
||||
segments.push_back(std::format("[{}]", index));
|
||||
return Scope(*this);
|
||||
}
|
||||
|
||||
std::string DecodeContext::Path() const {
|
||||
std::string path;
|
||||
for (const std::string& segment : segments) {
|
||||
if (!path.empty() && !segment.starts_with('[')) path += '.';
|
||||
path += segment;
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
std::optional<std::string_view> Crafter::Debug::Suggest(std::string_view word, std::span<const std::string_view> candidates) {
|
||||
const std::size_t allowed = word.size() <= 4 ? 1 : 2;
|
||||
std::optional<std::string_view> best;
|
||||
std::size_t bestDistance = allowed + 1;
|
||||
for (const std::string_view candidate : candidates) {
|
||||
const std::size_t distance = Distance(word, candidate);
|
||||
if (distance < bestDistance) {
|
||||
best = candidate;
|
||||
bestDistance = distance;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
std::string Crafter::Debug::Detail::Expected(std::string_view wanted, const Value& got) {
|
||||
return std::format("expected {}, got {}", wanted, WithArticle(got.Kind()));
|
||||
}
|
||||
|
||||
Value Crafter::Debug::Detail::Type(std::string_view type) {
|
||||
Value description = Value::MakeObject();
|
||||
description.Set("type", Value(type));
|
||||
return description;
|
||||
}
|
||||
|
||||
std::string Crafter::Debug::Detail::UnknownField(std::string_view key, std::span<const std::string_view> fields) {
|
||||
if (fields.empty()) return "unknown field; this takes no arguments";
|
||||
if (const auto suggestion = Suggest(key, fields)) {
|
||||
return std::format("unknown field; did you mean '{}'? (fields: {})", *suggestion, Join(fields));
|
||||
}
|
||||
return std::format("unknown field (fields: {})", Join(fields));
|
||||
}
|
||||
|
||||
std::string Crafter::Debug::Detail::UnknownName(std::string_view name, std::span<const std::string_view> names) {
|
||||
if (const auto suggestion = Suggest(name, names)) {
|
||||
return std::format("'{}' is not a name here; did you mean '{}'? (one of: {})", name, *suggestion, Join(names));
|
||||
}
|
||||
return std::format("'{}' is not a name here (one of: {})", name, Join(names));
|
||||
}
|
||||
Loading…
Reference in a new issue