Crunch
A Message Definition Language for Getting Things Right
Loading...
Searching...
No Matches
crunch_types.hpp
1#pragma once
2
3#include <cstdint>
4#include <string_view>
5
6namespace Crunch {
7
11using FieldId = int32_t;
12
20static constexpr FieldId MaxFieldId = (1 << 29) - 1;
21
25using MessageId = int32_t;
26
30enum class Format : uint8_t {
31 Packed = 0x01,
32 Aligned4 = 0x02,
33 Aligned8 = 0x03,
34 TLV = 0x04,
35};
36
40using CrunchVersionId = uint8_t;
41
46static constexpr std::size_t StandardHeaderSize =
47 sizeof(CrunchVersionId) + sizeof(Format) + sizeof(MessageId);
48
49static constexpr CrunchVersionId CrunchVersion = 0x03;
50
54enum class ErrorCode : uint8_t {
55 UNKNOWN = 0,
65};
66
73struct Error {
75 // cppcheck-suppress unusedStructMember
78 // cppcheck-suppress unusedStructMember
79 std::string_view message{};
80
84 [[nodiscard]] static constexpr Error integrity() noexcept {
85 return {ErrorCode::IntegrityCheckFailed, 0, "integrity check failed"};
86 }
87
94 template <size_t N>
95 [[nodiscard]] static constexpr Error validation(
96 FieldId id, const char (&msg)[N]) noexcept {
97 return {ErrorCode::ValidationFailed, id, std::string_view{msg, N - 1}};
98 }
99
104 [[nodiscard]] static constexpr Error deserialization(
105 std::string_view msg = "deserialization error") noexcept {
106 return {ErrorCode::DeserializationError, 0, msg};
107 }
108
112 [[nodiscard]] static constexpr Error invalid_message_id() noexcept {
113 return {ErrorCode::InvalidMessageId, 0, "invalid message id"};
114 }
115
119 [[nodiscard]] static constexpr Error invalid_format() noexcept {
120 return {ErrorCode::InvalidFormat, 0, "invalid serialization format"};
121 }
122
130 template <size_t N>
131 [[nodiscard]] static constexpr Error capacity_exceeded(
132 FieldId id, const char (&msg)[N]) noexcept {
133 return {ErrorCode::CapacityExceeded, id, std::string_view{msg, N - 1}};
134 }
135
136 [[nodiscard]] constexpr bool operator==(const Error& other) const noexcept =
137 default;
138
142 [[nodiscard]] constexpr bool operator==(ErrorCode c) const noexcept {
143 return code == c;
144 }
145};
146
147} // namespace Crunch
The public API for Crunch.
Definition: crunch_endian.hpp:10
uint8_t CrunchVersionId
Version identifier for the Crunch library.
Definition: crunch_types.hpp:40
int32_t FieldId
Unique identifier for a field within a Crunch message.
Definition: crunch_types.hpp:11
int32_t MessageId
Unique identifier for a message type.
Definition: crunch_types.hpp:25
Format
Serialization format identifier stored in the message header.
Definition: crunch_types.hpp:30
@ TLV
Tag-Length-Value encoding.
@ Aligned4
4-byte alignment padding.
@ Packed
No alignment padding (Alignment = 1).
@ Aligned8
8-byte alignment padding.
ErrorCode
Error codes representing various failure conditions in Crunch.
Definition: crunch_types.hpp:54
@ ValidationFailed
Field or message logical validation failed.
@ InvalidMessageId
Message ID in header does not match expected ID.
@ UNKNOWN
Unknown error.
@ DeserializationError
Error parsing or decoding message data.
Represents an error occurred during Crunch operations.
Definition: crunch_types.hpp:73
static constexpr Error validation(FieldId id, const char(&msg)[N]) noexcept
Creates an error representing a validation failure.
Definition: crunch_types.hpp:95
static constexpr Error deserialization(std::string_view msg="deserialization error") noexcept
Creates an error representing a deserialization failure.
Definition: crunch_types.hpp:104
ErrorCode code
The error code.
Definition: crunch_types.hpp:74
FieldId field_id
Definition: crunch_types.hpp:76
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
std::string_view message
Static error message string.
Definition: crunch_types.hpp:79
constexpr bool operator==(ErrorCode c) const noexcept
Checks if the error matches a specific error code.
Definition: crunch_types.hpp:142
static constexpr Error capacity_exceeded(FieldId id, const char(&msg)[N]) noexcept
Creates an error representing capacity exceeded. Used for strings and aggregated fields.
Definition: crunch_types.hpp:131
static constexpr Error integrity() noexcept
Creates an error representing an integrity check failure.
Definition: crunch_types.hpp:84