1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! Strongly-validated, zero-cost event message type.
//!
//! This module provides [`EventMessage`], a lightweight wrapper around a
//! `&'static str` that enforces strict formatting rules **at compile time**.
//! Messages must not exceed a fixed maximum byte length and must not contain
//! newline characters. Violations detected in a `const` context produce
//! compile-time errors.
//!
//! The accompanying `event_message!` macro guarantees that all validation
//! happens during compilation, even when invoked inside non-const functions.
//! When compiled successfully, an `EventMessage` is represented and used at
//! runtime with **no overhead**, making it as efficient as a bare `&'static str`.
//!
//! This module is intended for log messages, telemetry identifiers, and any
//! system where event message text must be validated, immutable, and fully
//! static while still incurring zero runtime cost.
/// A strongly-validated, zero-cost wrapper around a `&'static str` intended for
/// use in event messages, logs, telemetry, or any system where input strings
/// must meet strict formatting requirements.
///
/// # Overview
///
/// `EventMessage` wraps a `&'static str`, but with **compile-time validation**
/// whenever it is constructed in a `const` context. The validation enforces:
///
/// - The message must not exceed [`EventMessage::MAX_LEN`] bytes.
/// - The message must not contain newline characters (`'\n'` or `'\r'`).
///
/// When constructed using the accompanying `event_message!` macro, all
/// validation is **guaranteed to occur at compile time**, ensuring:
///
/// - **Zero runtime cost** (no loops, no checks, no panics).
/// - **Static enforcement** of message correctness.
/// - **Identical runtime performance** to using a plain `&'static str`.
///
/// # Why use this type?
///
/// This type is useful when you need a runtime message type that is:
///
/// - Guaranteed to be short enough for logging systems or wire protocols.
/// - Guaranteed to be newline-free (e.g., single-line logs).
/// - Immutable and `'static`.
/// - Just as cheap as storing a `&'static str`.
///
/// Once constructed, an `EventMessage` is simply a thin wrapper around a string
/// slice. Accessing the inner string via [`EventMessage::as_str`] is fully
/// inlined and has no measurable overhead.
///
/// # Compile-time vs runtime construction
///
/// - Using `EventMessage::new` inside a `const` context performs validation at
/// compile time.
/// - Using `EventMessage::new` at runtime may incur runtime checking cost.
/// - Using the `event_message!` macro **always** validates at compile time.
///
/// Prefer `event_message!` for maximal performance and strictness.
///
/// # Examples
///
/// ## Compile-time validated constant
///
/// ```rust
/// use limen_core::prelude::event_message::EventMessage;
///
/// const HELLO: EventMessage = EventMessage::new("hello");
/// ```
///
/// ## Preferable usage: enforced via `event_message!`
///
/// ```rust
/// use limen_core::prelude::event_message::EventMessage;
/// use limen_core::event_message;
///
/// let msg: EventMessage = event_message!("system_ready");
/// println!("{}", msg.as_str());
/// ```
///
/// Attempting to use invalid strings results in a **compile-time error**:
///
/// ```rust,compile_fail
/// use limen_core::prelude::event_message::EventMessage;
///
/// const BAD: EventMessage = EventMessage::new("line1\nline2"); // contains newline
/// ```
///
/// ```rust,compile_fail
/// use limen_core::prelude::event_message::EventMessage;
///
/// const TOO_LONG: EventMessage = EventMessage::new(
/// "12345678901234567890123456789012345678901234567890\
///12345678901234567890123456789012345678901234567890\
///123456789012345678901234567890"
/// );
/// ```
;
/// Constructs an [`EventMessage`] with **guaranteed compile-time validation**.
///
/// This macro ensures that:
///
/// - The message is validated in a `const` context.
/// - No runtime checks are ever emitted.
/// - The resulting value is as cheap as a `&'static str`.
///
/// # Why this macro is recommended
///
/// Even inside non-const functions, Rust evaluates the validation inside a
/// compile-time `const` item. This makes the macro strictly superior to calling
/// [`EventMessage::new`] directly at runtime.
///
/// # Examples
///
/// ```rust
/// use limen_core::prelude::event_message::EventMessage;
/// use limen_core::event_message;
///
/// let msg = event_message!("startup_complete");
/// println!("{}", msg.as_str());
/// ```
///
/// Invalid messages cause **compile-time errors**:
///
/// ```rust,compile_fail
/// use limen_core::prelude::event_message::EventMessage;
/// use limen_core::event_message;
///
/// let bad = event_message!("line1\nline2");
/// ```
///
/// ```rust,compile_fail
/// use limen_core::prelude::event_message::EventMessage;
/// use limen_core::event_message;
///
/// let too_long = event_message!(
/// "12345678901234567890123456789012345678901234567890\
///12345678901234567890123456789012345678901234567890\
///123456789012345678901234567890"
/// );
/// ```