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

Internal implementation details for Crunch's public API. More...

Classes

struct  is_buffer
 
struct  is_buffer< Buffer< Message, Integrity, Serdes, N > >
 

Functions

template<messages::CrunchMessage Message, typename Integrity , typename Serdes >
requires IntegrityPolicy<Integrity> && SerdesPolicy<Serdes, Message>
consteval auto GetBufferSize () noexcept -> std::size_t
 Compile-time calculation of the size of a buffer for a given message, integrity, and serdes combination.
 
template<typename Message >
requires messages::CrunchMessage<Message>
constexpr auto Validate (const Message &message) noexcept -> std::optional< Error >
 Forward declaration of Validate to enable recursion in ValidateField.
 
template<typename T >
constexpr std::optional< ErrorValidateField (const T &field)
 Helper to validate a single field.
 
template<typename Integrity , typename Serdes , messages::CrunchMessage Message, std::size_t N>
requires IntegrityPolicy<Integrity> && SerdesPolicy<Serdes, Message>
std::size_t SerializeWithoutValidation (std::array< std::byte, N > &buffer, const Message &message) noexcept
 Serializes the message without any validation checks.
 
template<typename Integrity , typename Serdes , messages::CrunchMessage Message, std::size_t N>
requires IntegrityPolicy<Integrity> && SerdesPolicy<Serdes, Message>
auto Serialize (std::array< std::byte, N > &buffer, const Message &message) noexcept -> std::expected< std::size_t, Error >
 implementation of Serialize.
 
template<typename Integrity , typename Serdes , typename Message >
requires IntegrityPolicy<Integrity> && SerdesPolicy<Serdes, Message> && messages::CrunchMessage<Message>
auto Deserialize (std::span< const std::byte > buffer, Message &message) noexcept -> std::optional< Error >
 implementation of Deserialize.
 

Detailed Description

Internal implementation details for Crunch's public API.

Function Documentation

◆ Deserialize()

template<typename Integrity , typename Serdes , typename Message >
requires IntegrityPolicy<Integrity> && SerdesPolicy<Serdes, Message> && messages::CrunchMessage<Message>
auto Crunch::detail::Deserialize ( std::span< const std::byte >  buffer,
Message &  message 
) -> std::optional<Error>
noexcept

implementation of Deserialize.

First, the integrity policy is executed to validate the buffer. Then, the header is deserialized. Finally, the payload is delegated to the Serdes policy for deserialization.

Template Parameters
IntegrityThe integrity policy to use.
SerdesThe serialization policy to use.
MessageThe message type to deserialize into.
Parameters
bufferThe buffer to deserialize from.
messageThe message object to populate.
Returns
std::nullopt on success, or an Error if integrity or deserialization fails.

◆ GetBufferSize()

template<messages::CrunchMessage Message, typename Integrity , typename Serdes >
requires IntegrityPolicy<Integrity> && SerdesPolicy<Serdes, Message>
consteval auto Crunch::detail::GetBufferSize ( ) -> std::size_t
noexcept

Compile-time calculation of the size of a buffer for a given message, integrity, and serdes combination.

Template Parameters
MessageThe message type to calculate the buffer size for.
IntegrityThe integrity policy to use.
SerdesThe serialization policy to use.
Returns
The size of the buffer in bytes.

◆ Serialize()

template<typename Integrity , typename Serdes , messages::CrunchMessage Message, std::size_t N>
requires IntegrityPolicy<Integrity> && SerdesPolicy<Serdes, Message>
auto Crunch::detail::Serialize ( std::array< std::byte, N > &  buffer,
const Message &  message 
) -> std::expected<std::size_t, Error>
noexcept

implementation of Serialize.

First field presence and message-level validation. Then, the header is serialized. Then the payload is delegated to the Serdes policy for serialization. Finally, the integrity policy is executed and appended.

Template Parameters
IntegrityThe integrity policy to use.
SerdesThe serialization policy to use.
MessageThe message type to serialize.
NThe size of the buffer.
Parameters
bufferThe buffer to serialize into.
messageThe message to serialize.
Returns
std::nullopt on success, or an Error if validation fails.

◆ SerializeWithoutValidation()

template<typename Integrity , typename Serdes , messages::CrunchMessage Message, std::size_t N>
requires IntegrityPolicy<Integrity> && SerdesPolicy<Serdes, Message>
std::size_t Crunch::detail::SerializeWithoutValidation ( std::array< std::byte, N > &  buffer,
const Message &  message 
)
noexcept

Serializes the message without any validation checks.

Template Parameters
IntegrityThe integrity policy to use.
SerdesThe serialization policy to use.
MessageThe message type to serialize.
NThe size of the buffer.
Parameters
bufferThe buffer to serialize into.
messageThe message to serialize.

◆ Validate()

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

Forward declaration of Validate to enable recursion in ValidateField.

Validates a message (fields presence, values, submessages, and message-level logic).

ValidateField calls Validate for submessages, and Validate calls ValidateField for all fields. This forward declaration resolves the circular dependency.

Template Parameters
MessageThe message type to validate.
Parameters
messageThe message to validate.
Returns
std::nullopt on success, or an Error.
Template Parameters
MessageThe message type to validate.
Parameters
messageThe message to validate.
Returns
std::optional<Error> std::nullopt on success, or an Error.

◆ ValidateField()

template<typename T >
constexpr std::optional< Error > Crunch::detail::ValidateField ( const T &  field)
constexpr

Helper to validate a single field.

Template Parameters
TThe type of the field to validate.
Parameters
fieldThe field instance to validate.
Returns
std::optional<Error> An error if validation fails, otherwise std::nullopt.