3#include <crunch/core/crunch_endian.hpp>
4#include <crunch/core/crunch_types.hpp>
31[[nodiscard]]
constexpr std::expected<CrunchHeader, Error>
GetHeader(
32 std::span<const std::byte> input)
noexcept {
33 if (input.size() < StandardHeaderSize) {
34 return std::unexpected(
61template <
typename Message,
typename Serdes>
63 std::span<std::byte> output)
noexcept {
64 std::size_t offset = 0;
67 std::memcpy(output.data() + offset, &version,
sizeof(version));
68 offset +=
sizeof(version);
70 const Format format = Serdes::GetFormat();
71 std::memcpy(output.data() + offset, &format,
sizeof(format));
72 offset +=
sizeof(format);
74 const MessageId msg_id = Message::message_id;
76 std::memcpy(output.data() + offset, &le_msg_id,
sizeof(le_msg_id));
77 offset +=
sizeof(le_msg_id);
96template <
typename Message,
typename Serdes>
98 std::span<const std::byte> input)
noexcept {
100 if (!header_result) {
101 return std::unexpected(header_result.error());
106 if (header.version != CrunchVersion) {
107 return std::unexpected(
111 if (header.format != Serdes::GetFormat()) {
115 if (header.message_id != Message::message_id) {
119 return StandardHeaderSize;
The public API for Crunch.
Definition: crunch_endian.hpp:10
uint8_t CrunchVersionId
Version identifier for the Crunch library.
Definition: crunch_types.hpp:40
constexpr std::size_t WriteHeader(std::span< std::byte > output) noexcept
Writes the standard header to the output buffer.
Definition: crunch_header.hpp:62
constexpr T LittleEndian(T value) noexcept
Converts a value to/from Little Endian byte order.
Definition: crunch_endian.hpp:21
int32_t MessageId
Unique identifier for a message type.
Definition: crunch_types.hpp:25
constexpr std::expected< CrunchHeader, Error > GetHeader(std::span< const std::byte > input) noexcept
Parses and returns a copy of the header from the input buffer.
Definition: crunch_header.hpp:31
Format
Serialization format identifier stored in the message header.
Definition: crunch_types.hpp:30
constexpr std::expected< std::size_t, Error > ValidateHeader(std::span< const std::byte > input) noexcept
Validates the header and returns offset after header on success.
Definition: crunch_header.hpp:97
static constexpr Error deserialization(std::string_view msg="deserialization error") noexcept
Creates an error representing a deserialization failure.
Definition: crunch_types.hpp:104
static constexpr Error invalid_message_id() noexcept
Creates an error representing an invalid message ID.
Definition: crunch_types.hpp:112
static constexpr Error invalid_format() noexcept
Creates an error representing an invalid serialization format.
Definition: crunch_types.hpp:119