14#include <commons/json/reason.hpp>
15#include <commons/reason.hpp>
38 using storage_t = comms::ReasonPtr;
40 static constexpr std::string_view kind_id =
"reason";
50 return std::make_shared<ReasonCell>(std::move(v));
53 [[nodiscard]] std::string_view
kind()
const override {
61 [[nodiscard]] std::string
to_string()
const override {
62 return value ? std::string{
value->kind()} : std::string{
"null"};
74 auto c = std::make_shared<ReasonCell>(
value ?
value->clone() : comms::ReasonPtr{});
75 c->display_info_ = display_info_;
79 [[nodiscard]] std::partial_ordering
compare(
ICell const& other)
const override {
80 if (
const auto k_cmp = this->
kind() <=> other.
kind(); k_cmp != 0) {
83 const auto* o =
dynamic_cast<ReasonCell const*
>(&other);
85 return std::partial_ordering::unordered;
88 : std::partial_ordering::unordered;
95 const auto it_kind = j.find(
KEY_KIND);
96 if (it_kind == j.end() || !it_kind->is_string()) {
97 throw InvalidJsonException(
"Missing or invalid 'k' field in ICell JSON",
98 std::string(kind_id));
100 if (
const auto k = it_kind->get<std::string_view>(); k != kind_id) {
101 throw KindMismatchException(
"Unexpected kind in ICell JSON: expected '" +
102 std::string(kind_id) +
"', got '" + std::string(k) +
104 std::string(kind_id));
107 if (it_value == j.end()) {
108 throw InvalidJsonException(
"Missing 'v' field in ICell JSON", std::string(kind_id));
111 auto cell = std::make_shared<ReasonCell>(it_value->get<comms::ReasonPtr>());
113 cell->set_display_info(it_d->get<DisplayInfo>());
119 static const auto d = std::make_shared<SimpleCellTypeDescriptor<ReasonCell>>(
DisplayInfo{
121 .description =
"Polymorphic \"why\" envelope ({\"kind\", …fields} on the wire)"});
127 display_info_ = std::move(m);
131 std::optional<DisplayInfo> display_info_;
145 using storage_t = comms::FailureReasonPtr;
147 static constexpr std::string_view kind_id =
"failure_reason";
157 return std::make_shared<FailureReasonCell>(std::move(v));
160 [[nodiscard]] std::string_view
kind()
const override {
165 return display_info_;
169 return value ? std::string{
value->kind()} : std::string{
"null"};
183 comms::FailureReasonPtr copy;
185 comms::IReason* raw =
value->clone().release();
187 copy = comms::FailureReasonPtr{
static_cast<comms::IFailureReason*
>(raw)};
189 auto c = std::make_shared<FailureReasonCell>(std::move(copy));
190 c->display_info_ = display_info_;
194 [[nodiscard]] std::partial_ordering
compare(
ICell const& other)
const override {
195 if (
const auto k_cmp = this->
kind() <=> other.
kind(); k_cmp != 0) {
200 return std::partial_ordering::unordered;
203 : std::partial_ordering::unordered;
207 if (!j.is_object()) {
210 const auto it_kind = j.find(
KEY_KIND);
211 if (it_kind == j.end() || !it_kind->is_string()) {
212 throw InvalidJsonException(
"Missing or invalid 'k' field in ICell JSON",
213 std::string(kind_id));
215 if (
const auto k = it_kind->get<std::string_view>(); k != kind_id) {
216 throw KindMismatchException(
"Unexpected kind in ICell JSON: expected '" +
217 std::string(kind_id) +
"', got '" + std::string(k) +
219 std::string(kind_id));
222 if (it_value == j.end()) {
223 throw InvalidJsonException(
"Missing 'v' field in ICell JSON", std::string(kind_id));
227 auto cell = std::make_shared<FailureReasonCell>(it_value->get<comms::FailureReasonPtr>());
229 cell->set_display_info(it_d->get<DisplayInfo>());
235 static const auto d =
236 std::make_shared<SimpleCellTypeDescriptor<FailureReasonCell>>(
DisplayInfo{
237 .name =
"FailureReason",
239 "Polymorphic developer-controlled failure ({\"kind\", …fields} on the wire)"});
245 display_info_ = std::move(m);
249 std::optional<DisplayInfo> display_info_;
Core ICell interface, cell_t handle, BaseCell CRTP base, and CellLike concept.
std::shared_ptr< ICellTypeDescriptor > cell_type_descriptor_t
Shared handle to a runtime cell-type descriptor.
Definition cell.h:63
std::shared_ptr< ICell > cell_t
Shared handle to any ICell-derived value — the canonical cell pointer.
Definition cell.h:68
Cell wrapping comms::FailureReasonPtr (a std::unique_ptr<comms::IFailureReason>).
Definition reason.h:143
std::partial_ordering compare(ICell const &other) const override
Three-way compare against another cell.
Definition reason.h:194
std::optional< DisplayInfo > const & overridden_display_info() const override
Read-only access to this cell's optional display info.
Definition reason.h:164
std::string_view kind() const override
Wire-stable kind identifier for this cell.
Definition reason.h:160
std::string to_string() const override
Render the cell's value as a compact human-readable string.
Definition reason.h:168
cell_t clone() const override
Deep-copy this cell.
Definition reason.h:180
comms::FailureReasonPtr value
Held failure reason (may be null).
Definition reason.h:150
json_t to_json() const override
Serialize this cell to its canonical JSON representation.
Definition reason.h:172
static cell_t of(comms::FailureReasonPtr v)
Construct a shared_ptr<FailureReasonCell> owning v.
Definition reason.h:156
void set_display_info(std::optional< DisplayInfo > m) override
Replace this cell's display info in place.
Definition reason.h:244
JSON shape was wrong (missing/non-string k, missing v, etc.).
Definition error.h:158
Runtime catalog of cell-type descriptors, keyed by wire kind id.
Definition registry.h:115
Cell wrapping comms::ReasonPtr (a std::unique_ptr<comms::IReason>).
Definition reason.h:36
std::string to_string() const override
Render the cell's value as a compact human-readable string.
Definition reason.h:61
std::optional< DisplayInfo > const & overridden_display_info() const override
Read-only access to this cell's optional display info.
Definition reason.h:57
json_t to_json() const override
Serialize this cell to its canonical JSON representation.
Definition reason.h:65
void set_display_info(std::optional< DisplayInfo > m) override
Replace this cell's display info in place.
Definition reason.h:126
std::string_view kind() const override
Wire-stable kind identifier for this cell.
Definition reason.h:53
cell_t clone() const override
Deep-copy this cell.
Definition reason.h:73
std::partial_ordering compare(ICell const &other) const override
Three-way compare against another cell.
Definition reason.h:79
comms::ReasonPtr value
Held reason (may be null).
Definition reason.h:43
static cell_t of(comms::ReasonPtr v)
Construct a shared_ptr<ReasonCell> owning v.
Definition reason.h:49
comms::DisplayInfo DisplayInfo
Display info attached to a cell or descriptor — re-exported from comms::DisplayInfo.
Definition common.h:75
The default_cell_for<T> trait that drives FieldsBuilder field-type inference.
#define PARCEL_DEFAULT_CELL(CellT)
Register a cell type as the default wrapper for its payload type.
Definition defaults.h:269
auto cell(T &&v)
Wrap a raw value into its default cell, returning a shared_ptr to the cell.
Definition defaults.h:192
Runtime cell-type descriptors and the schema-graph mix-ins (IHasFields, ISubTypes).
nlohmann::json typedef shared across cell types.
nlohmann::json json_t
Project-wide alias for nlohmann::json.
Definition json.h:19
Polymorphic root of every parcel cell.
Definition cell.h:84
static constexpr std::string_view KEY_DESCRIPTION
JSON key for the optional display info block ("d").
Definition cell.h:90
static constexpr std::string_view KEY_VALUE
JSON key for the value payload ("v").
Definition cell.h:88
virtual std::string_view kind() const =0
Wire-stable kind identifier for this cell.
static constexpr std::string_view KEY_KIND
JSON key for the kind id ("k").
Definition cell.h:86