Crunch
A Message Definition Language for Getting Things Right
Loading...
Searching...
No Matches
crunch.hpp
1#pragma once
2
3#include <array>
4#include <concepts>
5#include <crunch/crunch_detail.hpp>
6#include <crunch/fields/crunch_enum.hpp>
7#include <crunch/fields/crunch_string.hpp>
8#include <crunch/integrity/crunch_integrity.hpp>
9#include <crunch/messages/crunch_messages.hpp>
10#include <crunch/serdes/crunch_serdes.hpp>
11#include <crunch/serdes/crunch_static_layout.hpp>
12#include <cstddef>
13#include <cstdint>
14#include <expected>
15#include <optional>
16#include <span>
17
35namespace Crunch {
36
37// Expose Buffer, IsBuffer, and Decoder from detail namespace
38using detail::Buffer;
39using detail::Decoder;
40using detail::IsBuffer;
41
54template <messages::CrunchMessage Message, typename Integrity, typename Serdes>
55 requires IntegrityPolicy<Integrity> && SerdesPolicy<Serdes, Message>
56// cppcheck-suppress unusedFunction
57[[nodiscard]] constexpr auto GetBuffer() noexcept {
58 constexpr std::size_t N =
59 detail::GetBufferSize<Message, Integrity, Serdes>();
61}
62
80template <messages::CrunchMessage Message>
81[[nodiscard]] constexpr auto Validate(const Message& message) noexcept
82 -> std::optional<Error> {
83 return detail::Validate(message);
84}
85
104template <typename BufferType, typename Message>
105 requires IsBuffer<BufferType> && messages::CrunchMessage<Message> &&
106 std::same_as<typename BufferType::MessageType, Message>
107[[nodiscard]] constexpr auto Serialize(BufferType& buffer,
108 const Message& message) noexcept
109 -> std::optional<Error> {
110 using Serdes = typename BufferType::SerdesType;
111 using Integrity = typename BufferType::IntegrityType;
112 auto res = detail::Serialize<Integrity, Serdes>(buffer.data, message);
113 if (!res) {
114 return res.error();
115 }
116 buffer.used_bytes = *res;
117 return std::nullopt;
118}
119
132template <typename BufferType, typename Message>
133 requires IsBuffer<BufferType> && messages::CrunchMessage<Message> &&
134 std::same_as<typename BufferType::MessageType, Message>
135constexpr void SerializeWithoutValidation(BufferType& buffer,
136 const Message& message) noexcept {
137 using Serdes = typename BufferType::SerdesType;
138 using Integrity = typename BufferType::IntegrityType;
139 buffer.used_bytes = detail::SerializeWithoutValidation<Integrity, Serdes>(
140 buffer.data, message);
141}
142
161template <typename BufferType, typename Message>
162 requires IsBuffer<BufferType> && messages::CrunchMessage<Message> &&
163 std::same_as<typename BufferType::MessageType, Message>
164[[nodiscard]] constexpr auto Deserialize(const BufferType& buffer,
165 Message& out_message)
166 -> std::optional<Error> {
167 using Serdes = typename BufferType::SerdesType;
168 using Integrity = typename BufferType::IntegrityType;
169 return detail::Deserialize<Integrity, Serdes>(
170 buffer.serialized_message_span(), out_message);
171}
172
173} // namespace Crunch
constexpr auto Validate(const Message &message) noexcept -> std::optional< Error >
Forward declaration of Validate to enable recursion in ValidateField.
Definition: crunch_detail.hpp:138
The public API for Crunch.
Definition: crunch_endian.hpp:10
constexpr auto Validate(const Message &message) noexcept -> std::optional< Error >
Validates a message (field presence + message-level validation).
Definition: crunch.hpp:81
constexpr auto Deserialize(const BufferType &buffer, Message &out_message) -> std::optional< Error >
Deserializes a message from a buffer.
Definition: crunch.hpp:164
constexpr void SerializeWithoutValidation(BufferType &buffer, const Message &message) noexcept
Serializes a message into the provided buffer without validation.
Definition: crunch.hpp:135
constexpr auto Serialize(BufferType &buffer, const Message &message) noexcept -> std::optional< Error >
Serializes a message into the provided buffer.
Definition: crunch.hpp:107
constexpr auto GetBuffer() noexcept
Creates a correctly sized Buffer for the given configuration.
Definition: crunch.hpp:57
A lightweight wrapper around a std::array for serializing/deserializing messages.
Definition: crunch_detail.hpp:31