Crunch
A Message Definition Language for Getting Things Right
Loading...
Searching...
No Matches
Crunch Namespace Reference

The public API for Crunch. More...

Namespaces

namespace  detail
 Internal implementation details for Crunch's public API.
 
namespace  integrity
 Integrity policies for verifying message correctness.
 

Classes

struct  Around
 Validates that a floating-point value is within a tolerance of a target. More...
 
struct  Buffer
 A lightweight wrapper around a std::array for serializing/deserializing messages. More...
 
struct  EqualTo
 Validates that a value equals a compile-time threshold. More...
 
struct  Error
 Represents an error occurred during Crunch operations. More...
 
struct  Even
 Validates that an integral value is even. More...
 
struct  False
 Validates that a boolean value is false. More...
 
struct  FixedString
 
struct  GreaterThan
 Validates that a value is greater than a compile-time threshold. More...
 
struct  GreaterThanOrEqualTo
 Validates that a value is greater than or equal to a compile-time threshold. More...
 
struct  IsFinite
 Validates that a floating-point value is finite (not NaN or Inf). More...
 
struct  Length
 Validates that a string-like or container value has a specific length. More...
 
struct  LengthAtLeast
 Validates that a container has at least N elements. More...
 
struct  LengthAtMost
 Validates that a container has at most N elements. More...
 
struct  LessThan
 Validates that a value is less than a compile-time threshold. More...
 
struct  LessThanOrEqualTo
 Validates that a value is less than or equal to a compile-time threshold. More...
 
struct  Negative
 Validates that a value is strictly negative (< 0). More...
 
struct  None
 Validates nothing (always succeeds). More...
 
struct  NotEqualTo
 Validates that a value does not equal a compile-time threshold. More...
 
struct  NotZero
 Validates that a value is not zero. More...
 
struct  NullTerminated
 Validates that a string does not contain embedded nulls. More...
 
struct  Odd
 Validates that an integral value is odd. More...
 
struct  OneOf
 Validates that a value is one of a set of compile-time values. More...
 
struct  Optional
 Presence validator allowing an optional field (can be unset). More...
 
struct  Positive
 Validates that a value is non-negative (>= 0). More...
 
struct  Required
 Presence validator enforcing that a field MUST be set. More...
 
struct  StringEquals
 Validates that a string equals a compile-time string. More...
 
struct  StringNotEquals
 Validates that a string does not equal a compile-time string. More...
 
struct  True
 Validates that a boolean value is true. More...
 
struct  Unique
 Validates that a container has unique elements. More...
 

Concepts

concept  IsBuffer
 
concept  IntegrityPolicy
 Concept defining the interface for message integrity policies.
 
concept  SerdesPolicy
 Concept defining a Serialization/Deserialization policy.
 
concept  Validator
 Concept defining a value validator.
 
concept  IsPresenceValidator
 Concept for validators that check field presence semantics.
 

Typedefs

using FieldId = int32_t
 Unique identifier for a field within a Crunch message.
 
using MessageId = int32_t
 Unique identifier for a message type.
 
using CrunchVersionId = uint8_t
 Version identifier for the Crunch library.
 

Enumerations

enum class  Format : uint8_t { Packed = 0x01 , Aligned4 = 0x02 , Aligned8 = 0x03 , TLV = 0x04 }
 Serialization format identifier stored in the message header. More...
 
enum class  ErrorCode : uint8_t {
  UNKNOWN = 0 , IntegrityCheckFailed , DeserializationError , ValidationFailed ,
  InvalidMessageId , InvalidFormat , CapacityExceeded
}
 Error codes representing various failure conditions in Crunch. More...
 

Functions

template<typename T >
requires std::integral<T> || std::is_enum_v<T> || std::floating_point<T>
constexpr T LittleEndian (T value) noexcept
 Converts a value to/from Little Endian byte order.
 
template<messages::CrunchMessage Message, typename Integrity , typename Serdes >
requires IntegrityPolicy<Integrity> && SerdesPolicy<Serdes, Message>
constexpr auto GetBuffer () noexcept
 Creates a correctly sized Buffer for the given configuration.
 
template<messages::CrunchMessage Message>
constexpr auto Validate (const Message &message) noexcept -> std::optional< Error >
 Validates a message (field presence + message-level validation).
 
template<typename BufferType , typename Message >
requires IsBuffer<BufferType> && messages::CrunchMessage<Message> && std::same_as<typename BufferType::MessageType, Message>
constexpr auto Serialize (BufferType &buffer, const Message &message) noexcept -> std::optional< Error >
 Serializes a message into the provided buffer.
 
template<typename BufferType , typename Message >
requires IsBuffer<BufferType> && messages::CrunchMessage<Message> && std::same_as<typename BufferType::MessageType, Message>
constexpr void SerializeWithoutValidation (BufferType &buffer, const Message &message) noexcept
 Serializes a message into the provided buffer without validation.
 
template<typename BufferType , typename Message >
requires IsBuffer<BufferType> && messages::CrunchMessage<Message> && std::same_as<typename BufferType::MessageType, Message>
constexpr auto Deserialize (const BufferType &buffer, Message &out_message) -> std::optional< Error >
 Deserializes a message from a buffer.
 

Detailed Description

The public API for Crunch.

This section contains all the interfaces for serializing and deserializing messages, and transitively provides the necessary types for defining messages and fields.

Top-level APIs

  • GetBuffer: Creates a strongly-typed buffer of the maximum serialized message size for a given Message, Integrity, and Serdes combination.
  • Validate: Validates field presence and message-level constraints.
  • Serialize: Validates and writes a message into a buffer, appending integrity checks.
  • Deserialize: Verifies integrity and reads a message from a buffer.

Typedef Documentation

◆ CrunchVersionId

using Crunch::CrunchVersionId = typedef uint8_t

Version identifier for the Crunch library.

Note
The only valid value is 0x2.

Enumeration Type Documentation

◆ ErrorCode

enum class Crunch::ErrorCode : uint8_t
strong

Error codes representing various failure conditions in Crunch.

Enumerator
UNKNOWN 

Unknown error.

IntegrityCheckFailed 

Message integrity check failed (e.g., CRC mismatch).

DeserializationError 

Error parsing or decoding message data.

ValidationFailed 

Field or message logical validation failed.

InvalidMessageId 

Message ID in header does not match expected ID.

InvalidFormat 

Serialization format in header does not match expected format.

CapacityExceeded 

Data exceeds the capacity of the backing storage.

◆ Format

enum class Crunch::Format : uint8_t
strong

Serialization format identifier stored in the message header.

Enumerator
Packed 

No alignment padding (Alignment = 1).

Aligned4 

4-byte alignment padding.

Aligned8 

8-byte alignment padding.

TLV 

Tag-Length-Value encoding.

Function Documentation

◆ Deserialize()

template<typename BufferType , typename Message >
requires IsBuffer<BufferType> && messages::CrunchMessage<Message> && std::same_as<typename BufferType::MessageType, Message>
constexpr auto Crunch::Deserialize ( const BufferType &  buffer,
Message &  out_message 
) -> std::optional<Error>
constexpr

Deserializes a message from a buffer.

Verifies the integrity of the buffer, then attempts to deserialize the content into a Message object.

Constraints:

  • BufferType must be an instantiation of Crunch::Buffer.
  • Message must satisfy the CrunchMessage concept.
  • BufferType::MessageType must be the same as Message.
Template Parameters
BufferTypeThe Buffer type
MessageThe CrunchMessage type to deserialize into.
Parameters
bufferThe source Buffer to read from.
out_messageOutput parameter for the deserialized message.
Returns
std::optional<Error> std::nullopt on success, or an Error (Integrity/Deserialization).

◆ GetBuffer()

template<messages::CrunchMessage Message, typename Integrity , typename Serdes >
requires IntegrityPolicy<Integrity> && SerdesPolicy<Serdes, Message>
constexpr auto Crunch::GetBuffer ( )
constexprnoexcept

Creates a correctly sized Buffer for the given configuration.

This function calculates the exact size required for the message serialization plus any integrity overhead at compile time.

Template Parameters
MessageThe CrunchMessage type.
IntegrityThe IntegrityPolicy (e.g., integrity::None, integrity::CRC16).
SerdesThe SerdesPolicy (e.g., serdes::PackedLayout).
Returns
A Buffer object ready for use with Serialize/Deserialize.

◆ LittleEndian()

template<typename T >
requires std::integral<T> || std::is_enum_v<T> || std::floating_point<T>
constexpr T Crunch::LittleEndian ( value)
constexprnoexcept

Converts a value to/from Little Endian byte order.

Template Parameters
TThe type of the value to convert. Must be an integral type or enum.
Parameters
valueThe value to convert.
Returns
The converted value.

◆ Serialize()

template<typename BufferType , typename Message >
requires IsBuffer<BufferType> && messages::CrunchMessage<Message> && std::same_as<typename BufferType::MessageType, Message>
constexpr auto Crunch::Serialize ( BufferType &  buffer,
const Message &  message 
) -> std::optional<Error>
constexprnoexcept

Serializes a message into the provided buffer.

Validates the message content, serializes it according to the Serdes policy, and applies the Integrity policy (e.g., checksum).

Constraints:

  • BufferType must be an instantiation of Crunch::Buffer.
  • Message must satisfy the CrunchMessage concept.
  • BufferType::MessageType must be the same as Message.
Template Parameters
BufferTypeThe Buffer type (deduced from buffer parameter).
MessageThe CrunchMessage type to serialize.
Parameters
bufferThe destination Buffer (must match Message type).
messageThe message to serialize.
Returns
std::optional<Error> std::nullopt on success, or an Error if validation fails.

◆ SerializeWithoutValidation()

template<typename BufferType , typename Message >
requires IsBuffer<BufferType> && messages::CrunchMessage<Message> && std::same_as<typename BufferType::MessageType, Message>
constexpr void Crunch::SerializeWithoutValidation ( BufferType &  buffer,
const Message &  message 
)
constexprnoexcept

Serializes a message into the provided buffer without validation.

Does strictly serialization logic (header, payload, checksum) without running any validation checks. Useful for forwarding invalid messages, testing, or performance-critical paths where validation is done elsewhere.

Template Parameters
BufferTypeThe Buffer type
MessageThe CrunchMessage type to serialize.
Parameters
bufferThe destination Buffer (must match Message type).
messageThe message to serialize.

◆ Validate()

template<messages::CrunchMessage Message>
constexpr auto Crunch::Validate ( const Message &  message) -> std::optional<Error>
constexprnoexcept

Validates a message (field presence + message-level validation).

First validates that all required fields are set, then calls the message's custom Validate() method for any cross-field or business logic validation.

Note
This API does not execute field validators. Field validators are executed by the fields on-set.
In the future, this will also validate submessages and run aggregate field validation.
Template Parameters
MessageThe CrunchMessage type to validate.
Parameters
messageThe message to validate.
Returns
std::optional<Error> std::nullopt on success, or an Error if validation fails.