5#include <crunch/core/crunch_types.hpp>
6#include <crunch/validators/crunch_validators.hpp>
13namespace Crunch::fields {
21template <std::size_t MaxSize,
typename... Validators>
22 requires((Validator<Validators, std::string_view> && ...) &&
23 (
sizeof...(Validators) > 0))
26 using ValueType = std::string_view;
28 static constexpr std::size_t max_size = MaxSize;
30 constexpr String() =
default;
31 constexpr explicit String(std::string_view sv) {
32 static_cast<void>(set(sv));
35 [[nodiscard]]
constexpr std::string_view get()
const noexcept {
36 return std::string_view{buffer_.data(), current_len_};
39 constexpr std::optional<Error> set(std::string_view sv)
noexcept {
40 if (sv.size() > MaxSize) {
49 const std::span<char, MaxSize> buf{buffer_};
52 std::ranges::copy(sv, buf.begin());
53 std::ranges::fill(buf.subspan(sv.size()),
'\0');
55 current_len_ = sv.size();
59 constexpr void clear()
noexcept {
61 std::ranges::fill(buffer_,
'\0');
64 [[nodiscard]]
constexpr bool operator==(
65 const String& other)
const noexcept {
66 return get() == other.get();
70 -> std::optional<Error> {
74 [[nodiscard]]
static constexpr auto Validate(std::string_view v,
76 -> std::optional<Error> {
77 for (
const auto& result : {Validators::Check(v,
id)...}) {
78 if (result.has_value()) {
85 std::array<char, MaxSize> buffer_{};
86 std::size_t current_len_{0};
92template <std::size_t MaxSize,
typename... V>
Fixed-size string field backed by std::array.
Definition: crunch_string.hpp:24
constexpr auto Validate(const Message &message) noexcept -> std::optional< Error >
Validates a message (field presence + message-level validation).
Definition: crunch.hpp:123
int32_t FieldId
Unique identifier for a field within a Crunch message.
Definition: crunch_types.hpp:11
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:133
Definition: crunch_string.hpp:90