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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//! Message/entry consuming tail-stage codegen.
//!
//! `generate_owner_consuming_stages` emits the type-state tail stages that own
//! sequential access to an owner's (message or entry) tail groups + var-data.
//! `generate_decoder_consuming_stages` / `generate_entry_consuming_stages`
//! resolve message- and entry-level tail components and delegate to it.
use crate::ir::ByteOrder;
use crate::structured_ir::{
MessageGroup, MessageStructure, OwnerTailGroup, OwnerTailVarData, SchemaElements,
decoder_stage_after_ident, get_vardata_info, rust_type,
};
use super::runtime::{to_pascal_case, to_snake_case};
pub(crate) fn generate_owner_consuming_stages(
initial_ident: syn::Ident,
stage_prefix: &str,
header_size: usize,
byte_order: ByteOrder,
groups: &[OwnerTailGroup],
vardata: &[OwnerTailVarData],
enable_dispatch: bool,
// True when the initial owner stage is a message decoder (keeps `offset`,
// exposes `byte_offset()`); false for entry decoders, which also keep `offset`.
initial_has_byte_offset: bool,
) -> proc_macro2::TokenStream {
let total_tail = groups.len() + vardata.len();
if total_tail == 0 {
return proc_macro2::TokenStream::new();
}
let span = proc_macro2::Span::call_site();
let header_size_lit = syn::LitInt::new(&header_size.to_string(), span);
let field_pascals: Vec<String> = groups
.iter()
.map(|g| g.field_pascal.clone())
.chain(vardata.iter().map(|v| v.field_pascal.clone()))
.collect();
let stage_after_ident =
|i: usize| decoder_stage_after_ident(stage_prefix, &field_pascals[i], i, total_tail, span);
let mut ts = proc_macro2::TokenStream::new();
// 1. Stage struct definitions (After + Complete). Identical 5-field layout,
// non-Copy: a stage carries the tail cursor, so consuming it prevents reuse.
for i in 0..total_tail {
let stage = stage_after_ident(i);
ts.extend(quote::quote! {
/// Consuming decoder stage — drop without `into_*` / `finish` skips
/// remaining wire tails.
#[must_use = "decoder stage must be advanced with into_*/finish or tails are skipped"]
pub struct #stage<'a> {
pub(crate) buf: &'a [u8],
pub(crate) offset: usize,
pub(crate) tail_start: usize,
pub(crate) acting_version: u16,
pub(crate) acting_block_length: usize,
}
});
}
// acting_version() / acting_block_length() on every stage.
for i in 0..total_tail {
let stage = stage_after_ident(i);
ts.extend(quote::quote! {
impl<'a> #stage<'a> {
/// Schema version from the message header (or wrap args), not the
/// compiled schema constant. Fields with `sinceVersion` and optional
/// presence depend on this value.
#[inline]
pub const fn acting_version(&self) -> u16 { self.acting_version }
/// Block length from the wire header / wrap args. Tail offsets use
/// this acting length, not only the compiled `BLOCK_LENGTH`.
#[inline]
pub const fn acting_block_length(&self) -> usize { self.acting_block_length }
}
});
}
// The initial stage is the message decoder itself (message tails), which
// keeps `offset` and exposes `byte_offset()`. For entry tails the initial stage
// is the entry decoder, which also keeps `offset` and has no `byte_offset()`.
// Later stages all carry `offset` directly.
let parent_pos_expr = |i: usize| -> syn::Expr {
if i == 0 && initial_has_byte_offset {
syn::parse_str("self.byte_offset()").unwrap()
} else {
syn::parse_str("self.offset").unwrap()
}
};
let start_expr = |i: usize| -> syn::Expr {
if i == 0 && initial_has_byte_offset {
syn::parse_str("self.byte_offset() + self.acting_block_length").unwrap()
} else if i == 0 {
syn::parse_str("self.offset + self.acting_block_length").unwrap()
} else {
syn::parse_str("self.tail_start").unwrap()
}
};
// 2a. Group into_<g>() on the stage that precedes each group.
for (gi, tg) in groups.iter().enumerate() {
let i = gi;
let current_stage = if i == 0 {
initial_ident.clone()
} else {
stage_after_ident(i - 1)
};
let into_ident = syn::Ident::new(&format!("into_{}", tg.accessor_snake), span);
let g_decoder_ident = syn::Ident::new(&tg.group_decoder_ident, span);
let se = start_expr(i);
let pp = parent_pos_expr(i);
ts.extend(quote::quote! {
impl<'a> #current_stage<'a> {
/// Consume this stage and start decoding the next tail group,
/// enforcing wire order. The returned group decoder owns the
/// right to advance to the following stage via `finish()`.
#[inline]
pub fn #into_ident(
self,
) -> Result<#g_decoder_ident<'a, sbe_rt::Attached>, sbe_rt::DecodeError> {
let group_start = #se;
// SAFETY: this stage was reached by consuming the message in
// wire order, so `#pp` and `self.acting_block_length`
// describe the real parent body and `group_start` is this
// group's genuine dimension-header offset. The header,
// block length, and extent are still validated inside.
unsafe {
<#g_decoder_ident<'a, sbe_rt::Attached>>::wrap_with_parent(
self.buf,
group_start,
self.acting_version,
#pp,
self.acting_block_length,
)
}
}
}
});
}
// 2b. Var-data into_<vd>(): read the field and advance.
for (vi, vd) in vardata.iter().enumerate() {
let i = groups.len() + vi;
let current_stage = if i == 0 {
initial_ident.clone()
} else {
stage_after_ident(i - 1)
};
let next_stage = stage_after_ident(i);
let into_ident = syn::Ident::new(&format!("into_{}", vd.accessor_snake), span);
let slice_ident = syn::Ident::new(&format!("{}_slice", vd.accessor_snake), span);
let slice_doc = format!(
"Non-consuming variant: read this var-data field as `&[u8]` without \
advancing or constructing the next stage.\n\n\
Cheaper than [`Self::{into_ident}`] when only the bytes are needed."
);
let slice_doc_tokens = crate::codegen::runtime::doc_lines_tokens(&slice_doc);
let prefix_size_lit = syn::LitInt::new(&vd.prefix_size.to_string(), span);
let len_type_ident = syn::Ident::new(rust_type(vd.len_type), span);
let len_from_endian = syn::Ident::new(
match byte_order {
ByteOrder::LittleEndian => "from_le_bytes",
ByteOrder::BigEndian => "from_be_bytes",
},
span,
);
let vd_name_lit = syn::LitStr::new(&vd.name, span);
let se = start_expr(i);
let pp = parent_pos_expr(i);
// Only the entry decoder carries the `tail_end` one-shot cache, and it
// holds the end of the *last* tail component. So the non-consuming
// slice accessor can reuse it exactly when this var-data is both the
// first and the last tail of a group entry — the common
// "group of records, one string each" shape. Mirrors the flat entry
// accessor in `group_decoder.rs`; without it the two same-signature
// methods on the same type differ only in that this one re-reads and
// re-validates a length header the iterator already resolved.
let slice_cached_tail = if i == 0 && !initial_has_byte_offset && total_tail == 1 {
quote::quote! {
// `Iterator::next` cached the complete validated entry extent,
// including this prefix and payload.
if let Some(end) = self.tail_end.get() {
let data_offset =
self.offset + self.acting_block_length + #prefix_size_lit;
// SAFETY: `tail_end` is only ever set by `encoded_length`
// from `tail_offset_N`, which bounds-checked
// `end <= buf.len()` and `data_offset <= end` before
// caching.
return Ok(unsafe { self.buf.get_unchecked(data_offset..end) });
}
}
} else {
quote::quote! {}
};
let mut max_check = proc_macro2::TokenStream::new();
if let Some(max) = vd.max_length {
let max_lit = syn::LitInt::new(&max.to_string(), span);
max_check.extend(quote::quote! {
if len > #max_lit {
return Err(sbe_rt::DecodeError::InvalidVarDataLength {
field: #vd_name_lit,
length: len,
max_length: #max_lit as u64,
});
}
});
}
ts.extend(quote::quote! {
impl<'a> #current_stage<'a> {
/// Consume this stage, read the next var-data field, and advance
/// to the following stage. Wire order is enforced by consumption.
#[inline]
pub fn #into_ident(self) -> Result<(&'a [u8], #next_stage<'a>), sbe_rt::DecodeError> {
let offset = #se;
if offset + #prefix_size_lit > self.buf.len() {
return Err(sbe_rt::DecodeError::BufferTooShort {
field: #vd_name_lit,
needed: #prefix_size_lit,
available: self.buf.len().saturating_sub(offset),
});
}
// SAFETY: bounds verified by the preceding check
// (offset + prefix_size <= buf.len()).
let bytes: [u8; #prefix_size_lit] = unsafe {
core::ptr::read_unaligned(
self.buf.as_ptr().add(offset) as *const [u8; #prefix_size_lit],
)
};
// Direct integer read — avoids constructing the var-data
// encoding struct while preserving its width and schema byte order.
let len = #len_type_ident::#len_from_endian(bytes) as u64;
#max_check
let (data_start, data_end) = sbe_rt::checked_var_data_bounds(
#vd_name_lit,
offset,
#prefix_size_lit,
len,
self.buf.len(),
)?;
let data = &self.buf[data_start..data_end];
let next = #next_stage {
buf: self.buf,
offset: #pp,
tail_start: data_end,
acting_version: self.acting_version,
acting_block_length: self.acting_block_length,
};
Ok((data, next))
}
#slice_doc_tokens
#[inline]
pub fn #slice_ident(&self) -> Result<&'a [u8], sbe_rt::DecodeError> {
#slice_cached_tail
let offset = #se;
if offset + #prefix_size_lit > self.buf.len() {
return Err(sbe_rt::DecodeError::BufferTooShort {
field: #vd_name_lit,
needed: #prefix_size_lit,
available: self.buf.len().saturating_sub(offset),
});
}
let bytes: [u8; #prefix_size_lit] = unsafe {
core::ptr::read_unaligned(
self.buf.as_ptr().add(offset) as *const [u8; #prefix_size_lit],
)
};
// Direct integer read — avoids constructing the var-data
// encoding struct while preserving its width and schema byte order.
let len = #len_type_ident::#len_from_endian(bytes) as u64;
#max_check
let (data_start, data_end) = sbe_rt::checked_var_data_bounds(
#vd_name_lit,
offset,
#prefix_size_lit,
len,
self.buf.len(),
)?;
Ok(&self.buf[data_start..data_end])
}
}
});
// Text var-data: into_<field>_as_str() for schema-declared characterEncoding.
if let Some(ref enc) = vd.character_encoding {
let is_utf8 = enc.eq_ignore_ascii_case("UTF-8") || enc.eq_ignore_ascii_case("UTF8");
let is_ascii =
enc.eq_ignore_ascii_case("ASCII") || enc.eq_ignore_ascii_case("US-ASCII");
if is_utf8 || is_ascii {
let as_str_ident =
syn::Ident::new(&format!("into_{}_as_str", vd.accessor_snake), span);
let into_ident = syn::Ident::new(&format!("into_{}", vd.accessor_snake), span);
if is_ascii {
ts.extend(quote::quote! {
impl<'a> #current_stage<'a> {
/// Consume this stage, read the next ASCII var-data
/// field as a validated `&str`, and advance.
#[inline]
pub fn #as_str_ident(self) -> Result<(&'a str, #next_stage<'a>), sbe_rt::DecodeError> {
let (bytes, next) = self.#into_ident()?;
if !bytes.is_ascii() {
return Err(sbe_rt::DecodeError::InvalidAscii { field: #vd_name_lit });
}
let s = unsafe { core::str::from_utf8_unchecked(bytes) };
Ok((s, next))
}
}
});
} else {
ts.extend(quote::quote! {
impl<'a> #current_stage<'a> {
/// Consume this stage, read the next UTF-8 var-data
/// field as a validated `&str`, and advance.
#[inline]
pub fn #as_str_ident(self) -> Result<(&'a str, #next_stage<'a>), sbe_rt::DecodeError> {
let (bytes, next) = self.#into_ident()?;
let s = core::str::from_utf8(bytes).map_err(|e| {
sbe_rt::DecodeError::InvalidUtf8 { field: #vd_name_lit, error: e }
})?;
Ok((s, next))
}
}
});
}
let as_str_unchecked = syn::Ident::new(
&format!("into_{}_as_str_unchecked", vd.accessor_snake),
span,
);
ts.extend(quote::quote! {
impl<'a> #current_stage<'a> {
/// Consume this stage, read the next text var-data field as
/// a `&str` without encoding validation, and advance.
///
/// Structural bounds (truncated payload, overflowing length)
/// remain fallible — only character validation is skipped.
///
/// # Safety
/// The wire bytes must be valid for the schema-declared
/// character encoding (UTF-8 or ASCII).
#[inline]
pub unsafe fn #as_str_unchecked(
self,
) -> Result<(&'a str, #next_stage<'a>), sbe_rt::DecodeError> {
let (bytes, next) = self.#into_ident()?;
let s = unsafe { core::str::from_utf8_unchecked(bytes) };
Ok((s, next))
}
}
});
}
}
// Optional crate accessors. Always emitted, cfg-gated on the consumer
// so generator `--all-features` does not change the default compile.
{
let as_compact_ident =
syn::Ident::new(&format!("into_{}_as_compact_str", vd.accessor_snake), span);
let as_smol_ident =
syn::Ident::new(&format!("into_{}_as_smol_str", vd.accessor_snake), span);
let as_bytes_ident =
syn::Ident::new(&format!("into_{}_as_bytes", vd.accessor_snake), span);
let into_ident = syn::Ident::new(&format!("into_{}", vd.accessor_snake), span);
ts.extend(quote::quote! {
impl<'a> #current_stage<'a> {
/// Consume this stage, read the next var-data field as a
/// [`ergo_sbe::compact_str::CompactString`] (≤24 bytes inline), and advance.
#[cfg(feature = "compact_str")]
#[inline]
pub fn #as_compact_ident(self) -> Result<(ergo_sbe::compact_str::CompactString, #next_stage<'a>), sbe_rt::DecodeError> {
let (bytes, next) = self.#into_ident()?;
let s = core::str::from_utf8(bytes).map_err(|e| {
sbe_rt::DecodeError::InvalidUtf8 { field: #vd_name_lit, error: e }
})?;
Ok((ergo_sbe::compact_str::CompactString::new(s), next))
}
/// Consume this stage, read the next var-data field as a
/// [`ergo_sbe::smol_str::SmolStr`] (O(1) clone), and advance.
#[cfg(feature = "smol_str")]
#[inline]
pub fn #as_smol_ident(self) -> Result<(ergo_sbe::smol_str::SmolStr, #next_stage<'a>), sbe_rt::DecodeError> {
let (bytes, next) = self.#into_ident()?;
let s = core::str::from_utf8(bytes).map_err(|e| {
sbe_rt::DecodeError::InvalidUtf8 { field: #vd_name_lit, error: e }
})?;
Ok((ergo_sbe::smol_str::SmolStr::new(s), next))
}
/// Consume this stage, read the next var-data field as
/// [`ergo_sbe::bytes::Bytes`] (one copy from wire, then shared ownership), and advance.
#[cfg(feature = "bytes")]
#[inline]
pub fn #as_bytes_ident(self) -> Result<(ergo_sbe::bytes::Bytes, #next_stage<'a>), sbe_rt::DecodeError> {
let (data, next) = self.#into_ident()?;
Ok((ergo_sbe::bytes::Bytes::copy_from_slice(data), next))
}
}
});
}
// Scoped fallible combinator: try_<data> always available.
let try_data_ident = syn::Ident::new(&format!("try_{}", vd.accessor_snake), span);
ts.extend(quote::quote! {
impl<'a> #current_stage<'a> {
/// Fallible scoped var-data accessor. Calls the closure with
/// the decoded bytes and returns the next stage on success.
#[inline]
pub fn #try_data_ident<E, F>(
self,
f: F,
) -> Result<#next_stage<'a>, E>
where
E: From<sbe_rt::DecodeError>,
F: FnOnce(&[u8]) -> Result<(), E>,
{
let (data, next) = self.#into_ident()?;
f(data)?;
Ok(next)
}
}
});
// Nested-message helpers need AnyMessage/DecodedFrame (dispatch surface).
if enable_dispatch {
let as_msg_ident =
syn::Ident::new(&format!("into_{}_as_message", vd.accessor_snake), span);
let try_data_as_msg_ident =
syn::Ident::new(&format!("try_{}_as_message", vd.accessor_snake), span);
ts.extend(quote::quote! {
impl<'a> #current_stage<'a> {
/// Consume this stage, decode the var-data field as a nested
/// SBE message via `AnyMessage::decode_frame`, and advance
/// to the next stage.
#[inline]
pub fn #as_msg_ident(self) -> Result<(DecodedFrame<'a>, #next_stage<'a>), sbe_rt::DecodeError> {
let (data, next) = self.#into_ident()?;
let frame = AnyMessage::decode_frame(data, 0, data.len())?;
Ok((frame, next))
}
/// Fallible scoped nested-message accessor.
#[inline]
pub fn #try_data_as_msg_ident<E, F>(
self,
f: F,
) -> Result<#next_stage<'a>, E>
where
E: From<sbe_rt::DecodeError>,
F: FnOnce(DecodedFrame<'a>) -> Result<(), E>,
{
let (frame, next) = self.#as_msg_ident()?;
f(frame)?;
Ok(next)
}
}
});
}
}
for (gi, tg) in groups.iter().enumerate() {
let i = gi;
let next_stage = stage_after_ident(i);
let g_decoder_ident = syn::Ident::new(&tg.group_decoder_ident, span);
let entry_decoder_ident = syn::Ident::new(&tg.entry_decoder_ident, span);
let poisoned_finish_guard = if tg.entries_have_tails {
quote::quote! {
if let Some(error) = self.poisoned {
return Err(error);
}
}
} else {
proc_macro2::TokenStream::new()
};
ts.extend(quote::quote! {
impl<'a> #g_decoder_ident<'a, sbe_rt::Attached> {
/// Scan past any unread entries (including nested tails) in wire
/// order and return the next decoder stage.
///
/// Only an *attached* group — one reached through its message's
/// tail — can complete into a message stage. A standalone
/// [`Self::wrap`] has no parent to return to.
#[inline]
pub fn finish(self) -> Result<#next_stage<'a>, sbe_rt::DecodeError> {
// A poisoned group's position came from an entry that
// failed to decode, so the next stage would be built at a
// meaningless offset. Return the stored error instead.
#poisoned_finish_guard
let mut offset = self.offset;
let mut remaining = self.count;
let block_len = self.acting_block_length;
while remaining > 0 {
offset = #entry_decoder_ident::skip(self.buf, offset, block_len, self.acting_version)?;
remaining -= 1;
}
Ok(#next_stage {
buf: self.buf,
offset: self.parent_pos,
tail_start: offset,
acting_version: self.acting_version,
acting_block_length: self.parent_block_length,
})
}
/// Explicit sequential spelling of "advance past the rest of this group".
#[inline]
pub fn skip_remaining(self) -> Result<#next_stage<'a>, sbe_rt::DecodeError> {
self.finish()
}
}
});
}
let complete_ident = stage_after_ident(total_tail - 1);
// Message complete stages: `offset` is body start; header is `header_size`
// bytes before. Entry complete stages pass `header_size == 0`, so the
// header-inclusive view equals the body view.
ts.extend(quote::quote! {
impl<'a> #complete_ident<'a> {
/// Body bytes (excluding the message header; for entries this is the
/// complete entry bytes).
#[must_use = "discarding this value is almost always a mistake"]
#[inline]
pub fn as_body_bytes(&self) -> &'a [u8] {
&self.buf[self.offset..self.tail_start]
}
/// Complete SBE frame (header + body) for message stages.
/// For entry stages (`HEADER_LENGTH == 0`) this equals [`Self::as_body_bytes`].
#[must_use = "discarding this value is almost always a mistake"]
#[inline]
pub fn as_bytes_with_header(&self) -> &'a [u8] {
&self.buf[self.offset - #header_size_lit..self.tail_start]
}
/// Body length (excluding header).
#[must_use = "discarding this value is almost always a mistake"]
#[inline]
pub fn encoded_length(&self) -> usize {
self.tail_start - self.offset
}
/// Total message length including the schema-declared header.
/// Pure arithmetic: body length + `HEADER_LENGTH`.
#[must_use = "discarding this value is almost always a mistake"]
#[inline]
pub fn encoded_length_with_header(&self) -> usize {
self.tail_start - self.offset + #header_size_lit
}
/// Bytes after this message/entry.
#[must_use = "discarding this value is almost always a mistake"]
#[inline]
pub fn remaining(&self) -> &'a [u8] {
&self.buf[self.tail_start..]
}
}
});
ts
}
/// Message-level consuming tail stages: thin wrapper that resolves the
/// message's tail groups + var-data into descriptors and delegates to
/// `generate_owner_consuming_stages`.
pub(crate) fn generate_decoder_consuming_stages(
msg: &MessageStructure,
elements: &SchemaElements,
name: &str,
header_size: usize,
byte_order: ByteOrder,
_multi_message: bool,
group_unique_names: &[String],
enable_dispatch: bool,
) -> proc_macro2::TokenStream {
let span = proc_macro2::Span::call_site();
let stage_prefix = format!("{name}Decoder");
let initial_ident = syn::Ident::new(&stage_prefix, span);
let groups: Vec<OwnerTailGroup> = msg
.groups
.iter()
.enumerate()
.map(|(gi, g)| OwnerTailGroup {
accessor_snake: to_snake_case(&g.name),
field_pascal: to_pascal_case(&g.name),
group_decoder_ident: format!("{}Decoder", group_unique_names[gi]),
entry_decoder_ident: format!("{}EntryDecoder", group_unique_names[gi]),
entries_have_tails: g.has_dynamic_entries(),
})
.collect();
let vardata: Vec<OwnerTailVarData> = msg
.var_data
.iter()
.map(|vd| {
let (type_pascal, prefix_size, len_field, len_type) =
get_vardata_info(elements, &vd.type_name);
OwnerTailVarData {
accessor_snake: to_snake_case(&vd.name),
field_pascal: to_pascal_case(&vd.name),
type_pascal,
prefix_size,
len_field,
len_type,
max_length: vd.max_length,
name: vd.name.clone(),
character_encoding: vd.character_encoding.clone(),
}
})
.collect();
generate_owner_consuming_stages(
initial_ident,
&stage_prefix,
header_size,
byte_order,
&groups,
&vardata,
enable_dispatch,
true,
)
}
/// Entry-level consuming tail stages for a group whose entries have nested
/// groups and/or var-data. `name` is the group's scoped name; nested group
/// decoder names are `{name}{Ng}Decoder`.
pub(crate) fn generate_entry_consuming_stages(
g: &MessageGroup,
elements: &SchemaElements,
name: &str,
byte_order: ByteOrder,
enable_dispatch: bool,
) -> proc_macro2::TokenStream {
let span = proc_macro2::Span::call_site();
let entry_prefix = format!("{name}EntryDecoder");
let initial_ident = syn::Ident::new(&entry_prefix, span);
let groups: Vec<OwnerTailGroup> = g
.groups
.iter()
.map(|ng| {
let ng_pascal = format!("{}{}", name, to_pascal_case(&ng.name));
OwnerTailGroup {
accessor_snake: to_snake_case(&ng.name),
field_pascal: to_pascal_case(&ng.name),
group_decoder_ident: format!("{ng_pascal}Decoder"),
entries_have_tails: ng.has_dynamic_entries(),
entry_decoder_ident: format!("{ng_pascal}EntryDecoder"),
}
})
.collect();
let vardata: Vec<OwnerTailVarData> = g
.var_data
.iter()
.map(|vd| {
let (type_pascal, prefix_size, len_field, len_type) =
get_vardata_info(elements, &vd.type_name);
OwnerTailVarData {
accessor_snake: to_snake_case(&vd.name),
field_pascal: to_pascal_case(&vd.name),
type_pascal,
prefix_size,
len_field,
len_type,
max_length: vd.max_length,
name: vd.name.clone(),
character_encoding: vd.character_encoding.clone(),
}
})
.collect();
generate_owner_consuming_stages(
initial_ident,
&entry_prefix,
0,
byte_order,
&groups,
&vardata,
enable_dispatch,
false,
)
}