22#ifndef A11_DATA_MSGPACK_H_
23#define A11_DATA_MSGPACK_H_
30#include <absl/base/nullability.h>
31#include <absl/functional/function_ref.h>
32#include <absl/status/status.h>
33#include <absl/status/statusor.h>
34#include <nlohmann/json.hpp>
62 absl::Status
Pack(
const nlohmann::json&
value);
92 absl::FunctionRef<absl::Status(
MsgpackWriter* absl_nonnull)> encode);
95 [[nodiscard]]
const std::string&
bytes()
const {
return *bytes_; }
103 [[nodiscard]] std::string
TakeBytes() {
return std::move(*bytes_); }
107 void Reserve(
size_t extra);
110 std::string* absl_nonnull bytes_;
120 absl::StatusOr<nlohmann::json>
Read();
143 [[nodiscard]]
size_t position()
const {
return position_; }
146 std::string_view bytes_;
147 size_t position_ = 0;
151absl::StatusOr<std::string>
PackStatus(
const absl::Status& status);
153absl::StatusOr<absl::Status>
UnpackStatus(std::string_view bytes);
156nlohmann::json
Binary(std::string_view bytes);
159 std::string_view field_name);
Sequentially decode the fields of one A11 MessagePack wire record.
Definition msgpack.h:114
absl::StatusOr< nlohmann::json > Read()
Decode the next field and advance the byte cursor.
Definition msgpack.cc:441
absl::StatusOr< std::string_view > ReadBinaryView()
Read the next field as MessagePack binary without copying its payload.
Definition msgpack.cc:457
absl::StatusOr< size_t > ReadArrayLength()
Read the next field as a MessagePack array header, returning its length.
Definition msgpack.cc:489
MsgpackReader(std::string_view bytes)
Read from bytes, which must remain alive while this reader is used.
Definition msgpack.h:117
absl::Status EnsureFullyConsumed() const
Return an error if unread trailing bytes remain in the record.
Definition msgpack.cc:516
size_t position() const
Current byte offset, useful when diagnosing malformed peer data.
Definition msgpack.h:143
Incrementally encode the fields of one A11 wire record.
Definition msgpack.h:44
void PackInt(std::int64_t value)
Append a signed integer in the narrowest MessagePack form that fits.
Definition msgpack.cc:352
MsgpackWriter(const MsgpackWriter &)=delete
MsgpackWriter()
Encode into a buffer of this writer's own.
Definition msgpack.h:47
void PackBool(bool value)
Append a MessagePack boolean.
Definition msgpack.cc:330
void PackUint(std::uint64_t value)
Append an unsigned integer in the narrowest MessagePack form that fits.
Definition msgpack.cc:334
void PackString(std::string_view value)
Append a string field, copying value once.
Definition msgpack.cc:376
void PackArrayHeader(size_t length)
Append an array header; length element fields must follow.
Definition msgpack.cc:408
void PackBinary(std::string_view value)
Append a binary field, copying value once.
Definition msgpack.cc:393
void PackNil()
Append a MessagePack nil.
Definition msgpack.cc:326
absl::Status PackRecord(absl::FunctionRef< absl::Status(MsgpackWriter *absl_nonnull)> encode)
Append a nested record as one binary field, encoded by encode.
Definition msgpack.cc:432
MsgpackWriter(std::string *absl_nonnull target)
Encode into a caller-supplied buffer, appending to what is there.
Definition msgpack.h:55
std::string TakeBytes()
Transfer the encoded bytes out of this writer.
Definition msgpack.h:103
absl::Status Pack(const nlohmann::json &value)
Append a JSON-compatible field to the encoded record.
Definition msgpack.cc:313
MsgpackWriter & operator=(const MsgpackWriter &)=delete
const std::string & bytes() const
View the encoded bytes without transferring ownership.
Definition msgpack.h:95
void PackMapHeader(size_t length)
Append a map header; length key/value field pairs must follow.
Definition msgpack.cc:420
std::string value
Definition discover.cc:114
absl::StatusOr< std::string > GetBinary(const nlohmann::json &value, std::string_view field_name)
Read a binary JSON value, naming field_name in any validation error.
Definition msgpack.cc:528
absl::StatusOr< absl::Status > UnpackStatus(std::string_view bytes)
Decode a structured status without losing A11 status details.
Definition msgpack.cc:546
nlohmann::json Binary(std::string_view bytes)
Wrap arbitrary bytes as a MessagePack binary JSON value.
Definition msgpack.cc:523
absl::StatusOr< std::string > PackStatus(const absl::Status &status)
Encode a structured Abseil status for action and transport status fields.
Definition msgpack.cc:538
std::string target
Which editor definition syntax is about; every one when empty, which is the default.
Definition main.cc:74