netbat 0.9.0

Thin sync-first server/network boundary exposure layer for syncbat.
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
//! NETBAT/2 subscription streaming line frames.
//!
//! One newline-terminated ASCII line per frame. Cursor bytes are opaque on the
//! wire: runtime producers map existing core ordering surfaces
//! (`global_sequence`, HLC/frontier, `ProjectionFrontier`, source-specific
//! resume tokens) into bytes without netbat interpreting or minting cursors.
//!
//! Packet B is encode/decode only — no TCP streaming service, no async API, and
//! no subscription serving claim.

use super::error::NetbatError;
use super::hex::{decode_hex, encode_hex_into};
use super::limits::{
    Limits, STREAM_PROTOCOL_VERSION, SUBSCRIBE_VERB, SUB_ACK_VERB, SUB_CANCEL_VERB, SUB_END_VERB,
    SUB_ERR_VERB, SUB_EVENT_VERB, SUB_WATERMARK_VERB,
};

/// Maximum bytes accepted for a [`SubscriptionToken`] (matches hostbat grammar).
const MAX_SUBSCRIPTION_ID_BYTES: usize = 128;
/// Maximum bytes accepted for a payload schema ref token.
const MAX_PAYLOAD_SCHEMA_REF_BYTES: usize = 256;
/// Maximum bytes accepted for a stream reason/code token.
const MAX_STREAM_REASON_CODE_BYTES: usize = 128;

/// Globally unique subscription id (`orders.open.v1` grammar).
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SubscriptionToken(String);

impl SubscriptionToken {
    /// Construct a subscription token from wire text.
    ///
    /// # Errors
    /// [`NetbatError::SubscriptionIdTooLong`] or [`NetbatError::MalformedStreamFrame`].
    pub fn new(id: impl Into<String>, limits: &Limits) -> Result<Self, NetbatError> {
        let id = id.into();
        if id.len() > limits.max_subscription_id_bytes {
            return Err(NetbatError::SubscriptionIdTooLong {
                max: limits.max_subscription_id_bytes,
            });
        }
        validate_subscription_id(&id)
            .map_err(|reason| NetbatError::MalformedStreamFrame { reason })?;
        Ok(Self(id))
    }

    /// Token as a string slice.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

/// Opaque cursor bytes carried on the wire as lowercase hex.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CursorBytes(Vec<u8>);

impl CursorBytes {
    /// Wrap already-bounded opaque cursor bytes.
    #[must_use]
    pub fn new(bytes: impl Into<Vec<u8>>) -> Self {
        Self(bytes.into())
    }

    /// Borrow the opaque bytes.
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }

    /// Consume into owned bytes.
    #[must_use]
    pub fn into_bytes(self) -> Vec<u8> {
        self.0
    }
}

/// Optional cursor field encoded as `-` when absent.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum MaybeCursor {
    /// No cursor value on this field.
    Absent,
    /// Opaque cursor bytes.
    Present(CursorBytes),
}

impl MaybeCursor {
    fn encode_token(&self, out: &mut Vec<u8>) {
        match self {
            Self::Absent => out.push(b'-'),
            Self::Present(cursor) => encode_hex_into(cursor.as_bytes(), out),
        }
    }
}

/// Monotonic per-subscription delivery index (nonzero).
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct DeliveryIndex(u64);

impl DeliveryIndex {
    /// Construct a nonzero delivery index.
    ///
    /// # Errors
    /// [`NetbatError::MalformedStreamFrame`] when `value` is zero.
    pub fn new(value: u64) -> Result<Self, NetbatError> {
        if value == 0 {
            return Err(NetbatError::MalformedStreamFrame {
                reason: "delivery index must be nonzero",
            });
        }
        Ok(Self(value))
    }

    /// Raw delivery index value.
    #[must_use]
    pub fn get(self) -> u64 {
        self.0
    }
}

/// Client-side receive window (nonzero).
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct ClientWindow(u32);

impl ClientWindow {
    /// Construct a nonzero client window.
    ///
    /// # Errors
    /// [`NetbatError::MalformedStreamFrame`] when `value` is zero.
    pub fn new(value: u32) -> Result<Self, NetbatError> {
        if value == 0 {
            return Err(NetbatError::MalformedStreamFrame {
                reason: "client window must be nonzero",
            });
        }
        Ok(Self(value))
    }

    /// Raw client window value.
    #[must_use]
    pub fn get(self) -> u32 {
        self.0
    }
}

/// Stable lowercase stream reason/code token.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StreamReasonCode(String);

impl StreamReasonCode {
    /// Construct a reason/code token.
    ///
    /// # Errors
    /// [`NetbatError::MalformedStreamFrame`] when grammar checks fail.
    pub fn new(code: impl Into<String>) -> Result<Self, NetbatError> {
        let code = code.into();
        validate_reason_code(&code)
            .map_err(|reason| NetbatError::MalformedStreamFrame { reason })?;
        Ok(Self(code))
    }

    /// Token as a string slice.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

/// Opaque payload schema ref token (interpreted above netbat).
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PayloadSchemaRef(String);

impl PayloadSchemaRef {
    /// Construct a payload schema ref token.
    ///
    /// # Errors
    /// [`NetbatError::MalformedStreamFrame`] when grammar checks fail.
    pub fn new(reference: impl Into<String>) -> Result<Self, NetbatError> {
        let reference = reference.into();
        validate_payload_schema_ref(&reference)
            .map_err(|reason| NetbatError::MalformedStreamFrame { reason })?;
        Ok(Self(reference))
    }

    /// Reference as a string slice.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

/// `NETBAT/2 SUBSCRIBE` frame.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SubscribeFrame {
    /// Globally unique subscription id.
    pub subscription_id: SubscriptionToken,
    /// Resume cursor, if any.
    pub resume_cursor: MaybeCursor,
    /// Client-side receive window.
    pub client_window: ClientWindow,
}

/// `NETBAT/2 SUB_EVENT` frame.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SubEventFrame {
    /// Globally unique subscription id.
    pub subscription_id: SubscriptionToken,
    /// Monotonic delivery index for this event.
    pub delivery_index: DeliveryIndex,
    /// Cursor before this delivery (opaque bytes).
    pub cursor_before: MaybeCursor,
    /// Cursor after this delivery (opaque bytes).
    pub cursor_after: MaybeCursor,
    /// Payload schema ref token.
    pub payload_schema_ref: PayloadSchemaRef,
    /// Canonical payload bytes.
    pub payload: Vec<u8>,
}

/// `NETBAT/2 SUB_WATERMARK` frame.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SubWatermarkFrame {
    /// Globally unique subscription id.
    pub subscription_id: SubscriptionToken,
    /// Monotonic delivery index watermark.
    pub delivery_index: DeliveryIndex,
    /// Cursor after watermark (opaque bytes).
    pub cursor_after: CursorBytes,
}

/// `NETBAT/2 SUB_ACK` frame.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SubAckFrame {
    /// Globally unique subscription id.
    pub subscription_id: SubscriptionToken,
    /// Cumulative delivery index acknowledged.
    pub delivery_index: DeliveryIndex,
    /// Cumulative cursor after acknowledged deliveries.
    pub cursor_after: CursorBytes,
}

/// `NETBAT/2 SUB_CANCEL` frame.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SubCancelFrame {
    /// Globally unique subscription id.
    pub subscription_id: SubscriptionToken,
    /// Stable cancel reason code.
    pub reason_code: StreamReasonCode,
}

/// `NETBAT/2 SUB_ERR` frame.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SubErrFrame {
    /// Globally unique subscription id.
    pub subscription_id: SubscriptionToken,
    /// Stable error code token.
    pub code: StreamReasonCode,
    /// Last delivered cursor, if any.
    pub last_delivered_cursor: MaybeCursor,
    /// Last acknowledged cursor, if any.
    pub last_acked_cursor: MaybeCursor,
    /// UTF-8 error message bytes (hex on wire).
    pub message: Vec<u8>,
}

/// `NETBAT/2 SUB_END` frame.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SubEndFrame {
    /// Globally unique subscription id.
    pub subscription_id: SubscriptionToken,
    /// Final cursor after stream end, if any.
    pub cursor_after: MaybeCursor,
    /// Stable end reason code.
    pub reason_code: StreamReasonCode,
}

/// Decoded NETBAT/2 streaming frame.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum StreamFrame {
    /// Open or resume a subscription stream.
    Subscribe(SubscribeFrame),
    /// Deliver one subscription event/payload.
    SubEvent(SubEventFrame),
    /// Publish a cumulative delivery watermark.
    SubWatermark(SubWatermarkFrame),
    /// Acknowledge cumulative delivery progress.
    SubAck(SubAckFrame),
    /// Cancel a subscription stream.
    SubCancel(SubCancelFrame),
    /// Report a subscription stream error.
    SubErr(SubErrFrame),
    /// End a subscription stream.
    SubEnd(SubEndFrame),
}

/// Decode one NETBAT/2 streaming line frame.
///
/// # Errors
/// Returns [`NetbatError`] when the frame is malformed or exceeds limits.
pub fn decode_stream_line(line: &[u8], limits: &Limits) -> Result<StreamFrame, NetbatError> {
    if line.len() > limits.max_line_bytes {
        return Err(NetbatError::LineTooLong {
            max: limits.max_line_bytes,
        });
    }

    let line = strip_line_ending(line);
    if line.is_empty() {
        return Err(NetbatError::MalformedStreamFrame {
            reason: "empty line",
        });
    }

    let mut parts = line.split(|byte| *byte == b' ');
    let version = parts.next().ok_or(NetbatError::MalformedStreamFrame {
        reason: "missing protocol version",
    })?;
    if version != STREAM_PROTOCOL_VERSION.as_bytes() {
        return Err(NetbatError::UnsupportedProtocolVersion {
            version: std::str::from_utf8(version)
                .map(ToOwned::to_owned)
                .unwrap_or_else(|_| format!("0x{}", encode_hex_into_lossy(version))),
        });
    }

    let verb = parts.next().ok_or(NetbatError::MalformedStreamFrame {
        reason: "missing stream verb",
    })?;
    let frame = decode_stream_frame_body(verb, &mut parts, limits)?;

    if parts.next().is_some() {
        return Err(NetbatError::MalformedStreamFrame {
            reason: "too many fields",
        });
    }
    Ok(frame)
}

fn decode_stream_frame_body<'a>(
    verb: &[u8],
    parts: &mut impl Iterator<Item = &'a [u8]>,
    limits: &Limits,
) -> Result<StreamFrame, NetbatError> {
    match verb {
        b if b == SUBSCRIBE_VERB.as_bytes() => {
            Ok(StreamFrame::Subscribe(decode_subscribe(parts, limits)?))
        }
        b if b == SUB_EVENT_VERB.as_bytes() => {
            Ok(StreamFrame::SubEvent(decode_sub_event(parts, limits)?))
        }
        b if b == SUB_WATERMARK_VERB.as_bytes() => Ok(StreamFrame::SubWatermark(
            decode_sub_watermark(parts, limits)?,
        )),
        b if b == SUB_ACK_VERB.as_bytes() => {
            Ok(StreamFrame::SubAck(decode_sub_ack(parts, limits)?))
        }
        b if b == SUB_CANCEL_VERB.as_bytes() => {
            Ok(StreamFrame::SubCancel(decode_sub_cancel(parts, limits)?))
        }
        b if b == SUB_ERR_VERB.as_bytes() => {
            Ok(StreamFrame::SubErr(decode_sub_err(parts, limits)?))
        }
        b if b == SUB_END_VERB.as_bytes() => {
            Ok(StreamFrame::SubEnd(decode_sub_end(parts, limits)?))
        }
        _ => Err(NetbatError::MalformedStreamFrame {
            reason: "unknown stream verb",
        }),
    }
}

/// Encode one NETBAT/2 streaming frame as a newline-terminated line.
#[must_use]
pub fn encode_stream_frame(frame: &StreamFrame) -> Vec<u8> {
    let mut line = Vec::new();
    line.extend_from_slice(STREAM_PROTOCOL_VERSION.as_bytes());
    line.push(b' ');
    match frame {
        StreamFrame::Subscribe(frame) => {
            line.extend_from_slice(SUBSCRIBE_VERB.as_bytes());
            line.push(b' ');
            line.extend_from_slice(frame.subscription_id.as_str().as_bytes());
            line.push(b' ');
            frame.resume_cursor.encode_token(&mut line);
            line.push(b' ');
            encode_decimal_u32(frame.client_window.get(), &mut line);
        }
        StreamFrame::SubEvent(frame) => {
            line.extend_from_slice(SUB_EVENT_VERB.as_bytes());
            line.push(b' ');
            line.extend_from_slice(frame.subscription_id.as_str().as_bytes());
            line.push(b' ');
            encode_decimal_u64(frame.delivery_index.get(), &mut line);
            line.push(b' ');
            frame.cursor_before.encode_token(&mut line);
            line.push(b' ');
            frame.cursor_after.encode_token(&mut line);
            line.push(b' ');
            line.extend_from_slice(frame.payload_schema_ref.as_str().as_bytes());
            line.push(b' ');
            encode_hex_into(&frame.payload, &mut line);
        }
        StreamFrame::SubWatermark(frame) => {
            line.extend_from_slice(SUB_WATERMARK_VERB.as_bytes());
            line.push(b' ');
            line.extend_from_slice(frame.subscription_id.as_str().as_bytes());
            line.push(b' ');
            encode_decimal_u64(frame.delivery_index.get(), &mut line);
            line.push(b' ');
            encode_hex_into(frame.cursor_after.as_bytes(), &mut line);
        }
        StreamFrame::SubAck(frame) => {
            line.extend_from_slice(SUB_ACK_VERB.as_bytes());
            line.push(b' ');
            line.extend_from_slice(frame.subscription_id.as_str().as_bytes());
            line.push(b' ');
            encode_decimal_u64(frame.delivery_index.get(), &mut line);
            line.push(b' ');
            encode_hex_into(frame.cursor_after.as_bytes(), &mut line);
        }
        StreamFrame::SubCancel(frame) => {
            line.extend_from_slice(SUB_CANCEL_VERB.as_bytes());
            line.push(b' ');
            line.extend_from_slice(frame.subscription_id.as_str().as_bytes());
            line.push(b' ');
            line.extend_from_slice(frame.reason_code.as_str().as_bytes());
        }
        StreamFrame::SubErr(frame) => {
            line.extend_from_slice(SUB_ERR_VERB.as_bytes());
            line.push(b' ');
            line.extend_from_slice(frame.subscription_id.as_str().as_bytes());
            line.push(b' ');
            line.extend_from_slice(frame.code.as_str().as_bytes());
            line.push(b' ');
            frame.last_delivered_cursor.encode_token(&mut line);
            line.push(b' ');
            frame.last_acked_cursor.encode_token(&mut line);
            line.push(b' ');
            encode_hex_into(&frame.message, &mut line);
        }
        StreamFrame::SubEnd(frame) => {
            line.extend_from_slice(SUB_END_VERB.as_bytes());
            line.push(b' ');
            line.extend_from_slice(frame.subscription_id.as_str().as_bytes());
            line.push(b' ');
            frame.cursor_after.encode_token(&mut line);
            line.push(b' ');
            line.extend_from_slice(frame.reason_code.as_str().as_bytes());
        }
    }
    line.push(b'\n');
    line
}

fn decode_subscribe<'a>(
    parts: &mut impl Iterator<Item = &'a [u8]>,
    limits: &Limits,
) -> Result<SubscribeFrame, NetbatError> {
    let subscription_id = next_token(parts, "missing subscription id")?;
    let resume = next_maybe_cursor(parts, limits, "missing resume cursor")?;
    let window = next_token(parts, "missing client window")?;
    Ok(SubscribeFrame {
        subscription_id: SubscriptionToken::new(parse_utf8(subscription_id)?, limits)?,
        resume_cursor: resume,
        client_window: ClientWindow::new(parse_u32(window)?)?,
    })
}

fn decode_sub_event<'a>(
    parts: &mut impl Iterator<Item = &'a [u8]>,
    limits: &Limits,
) -> Result<SubEventFrame, NetbatError> {
    let subscription_id = next_token(parts, "missing subscription id")?;
    let delivery_index = next_token(parts, "missing delivery index")?;
    let cursor_before = next_maybe_cursor(parts, limits, "missing cursor_before")?;
    let cursor_after = next_maybe_cursor(parts, limits, "missing cursor_after")?;
    let payload_schema_ref = next_token(parts, "missing payload schema ref")?;
    let payload_hex = next_token(parts, "missing payload hex")?;
    Ok(SubEventFrame {
        subscription_id: SubscriptionToken::new(parse_utf8(subscription_id)?, limits)?,
        delivery_index: DeliveryIndex::new(parse_u64(delivery_index)?)?,
        cursor_before,
        cursor_after,
        payload_schema_ref: PayloadSchemaRef::new(parse_utf8(payload_schema_ref)?)?,
        payload: decode_stream_hex(payload_hex, limits.max_stream_payload_bytes, |max| {
            NetbatError::StreamPayloadTooLarge { max }
        })?,
    })
}

fn decode_sub_watermark<'a>(
    parts: &mut impl Iterator<Item = &'a [u8]>,
    limits: &Limits,
) -> Result<SubWatermarkFrame, NetbatError> {
    let subscription_id = next_token(parts, "missing subscription id")?;
    let delivery_index = next_token(parts, "missing delivery index")?;
    let cursor_after = next_token(parts, "missing cursor_after")?;
    Ok(SubWatermarkFrame {
        subscription_id: SubscriptionToken::new(parse_utf8(subscription_id)?, limits)?,
        delivery_index: DeliveryIndex::new(parse_u64(delivery_index)?)?,
        cursor_after: decode_required_cursor(cursor_after, limits)?,
    })
}

fn decode_sub_ack<'a>(
    parts: &mut impl Iterator<Item = &'a [u8]>,
    limits: &Limits,
) -> Result<SubAckFrame, NetbatError> {
    let subscription_id = next_token(parts, "missing subscription id")?;
    let delivery_index = next_token(parts, "missing delivery index")?;
    let cursor_after = next_token(parts, "missing cursor_after")?;
    Ok(SubAckFrame {
        subscription_id: SubscriptionToken::new(parse_utf8(subscription_id)?, limits)?,
        delivery_index: DeliveryIndex::new(parse_u64(delivery_index)?)?,
        cursor_after: decode_required_cursor(cursor_after, limits)?,
    })
}

fn decode_sub_cancel<'a>(
    parts: &mut impl Iterator<Item = &'a [u8]>,
    limits: &Limits,
) -> Result<SubCancelFrame, NetbatError> {
    let subscription_id = next_token(parts, "missing subscription id")?;
    let reason_code = next_token(parts, "missing reason code")?;
    Ok(SubCancelFrame {
        subscription_id: SubscriptionToken::new(parse_utf8(subscription_id)?, limits)?,
        reason_code: StreamReasonCode::new(parse_utf8(reason_code)?)?,
    })
}

fn decode_sub_err<'a>(
    parts: &mut impl Iterator<Item = &'a [u8]>,
    limits: &Limits,
) -> Result<SubErrFrame, NetbatError> {
    let subscription_id = next_token(parts, "missing subscription id")?;
    let code = next_token(parts, "missing error code")?;
    let last_delivered = next_maybe_cursor(parts, limits, "missing last delivered cursor")?;
    let last_acked = next_maybe_cursor(parts, limits, "missing last acked cursor")?;
    let message_hex = next_token(parts, "missing message hex")?;
    Ok(SubErrFrame {
        subscription_id: SubscriptionToken::new(parse_utf8(subscription_id)?, limits)?,
        code: StreamReasonCode::new(parse_utf8(code)?)?,
        last_delivered_cursor: last_delivered,
        last_acked_cursor: last_acked,
        message: decode_stream_hex(message_hex, limits.max_stream_error_message_bytes, |max| {
            NetbatError::StreamMessageTooLarge { max }
        })?,
    })
}

fn decode_sub_end<'a>(
    parts: &mut impl Iterator<Item = &'a [u8]>,
    limits: &Limits,
) -> Result<SubEndFrame, NetbatError> {
    let subscription_id = next_token(parts, "missing subscription id")?;
    let cursor_after = next_maybe_cursor(parts, limits, "missing cursor_after")?;
    let reason_code = next_token(parts, "missing reason code")?;
    Ok(SubEndFrame {
        subscription_id: SubscriptionToken::new(parse_utf8(subscription_id)?, limits)?,
        cursor_after,
        reason_code: StreamReasonCode::new(parse_utf8(reason_code)?)?,
    })
}

fn next_token<'a>(
    parts: &mut impl Iterator<Item = &'a [u8]>,
    missing: &'static str,
) -> Result<&'a [u8], NetbatError> {
    parts
        .next()
        .ok_or(NetbatError::MalformedStreamFrame { reason: missing })
}

fn next_maybe_cursor<'a>(
    parts: &mut impl Iterator<Item = &'a [u8]>,
    limits: &Limits,
    missing: &'static str,
) -> Result<MaybeCursor, NetbatError> {
    let token = next_token(parts, missing)?;
    decode_maybe_cursor(token, limits)
}

fn decode_maybe_cursor(token: &[u8], limits: &Limits) -> Result<MaybeCursor, NetbatError> {
    if token == b"-" {
        return Ok(MaybeCursor::Absent);
    }
    Ok(MaybeCursor::Present(decode_required_cursor(token, limits)?))
}

fn decode_required_cursor(token: &[u8], limits: &Limits) -> Result<CursorBytes, NetbatError> {
    Ok(CursorBytes::new(decode_stream_hex(
        token,
        limits.max_cursor_bytes,
        |max| NetbatError::CursorTooLarge { max },
    )?))
}

fn decode_stream_hex(
    token: &[u8],
    max_bytes: usize,
    too_large: impl FnOnce(usize) -> NetbatError,
) -> Result<Vec<u8>, NetbatError> {
    decode_hex(token, max_bytes).map_err(|error| match error {
        NetbatError::InputTooLarge { max } => too_large(max),
        NetbatError::MalformedRequest { reason } => NetbatError::MalformedStreamFrame { reason },
        NetbatError::Io { .. }
        | NetbatError::EmptyStream
        | NetbatError::LineTooLong { .. }
        | NetbatError::UnsupportedProtocolVersion { .. }
        | NetbatError::OperationNameTooLong { .. }
        | NetbatError::OutputTooLarge { .. }
        | NetbatError::Runtime(_)
        | NetbatError::MalformedStreamFrame { .. }
        | NetbatError::SubscriptionIdTooLong { .. }
        | NetbatError::CursorTooLarge { .. }
        | NetbatError::StreamPayloadTooLarge { .. }
        | NetbatError::StreamMessageTooLarge { .. } => error,
    })
}

fn parse_utf8(token: &[u8]) -> Result<String, NetbatError> {
    std::str::from_utf8(token)
        .map(ToOwned::to_owned)
        .map_err(|_| NetbatError::MalformedStreamFrame {
            reason: "token is not utf-8",
        })
}

fn parse_u64(token: &[u8]) -> Result<u64, NetbatError> {
    let text = parse_utf8(token)?;
    text.parse::<u64>()
        .map_err(|_| NetbatError::MalformedStreamFrame {
            reason: "integer field is not decimal",
        })
}

fn parse_u32(token: &[u8]) -> Result<u32, NetbatError> {
    let text = parse_utf8(token)?;
    text.parse::<u32>()
        .map_err(|_| NetbatError::MalformedStreamFrame {
            reason: "integer field is not decimal",
        })
}

fn encode_decimal_u64(value: u64, out: &mut Vec<u8>) {
    out.extend_from_slice(value.to_string().as_bytes());
}

fn encode_decimal_u32(value: u32, out: &mut Vec<u8>) {
    out.extend_from_slice(value.to_string().as_bytes());
}

fn strip_line_ending(line: &[u8]) -> &[u8] {
    line.strip_suffix(b"\n")
        .unwrap_or(line)
        .strip_suffix(b"\r")
        .unwrap_or_else(|| line.strip_suffix(b"\n").unwrap_or(line))
}

fn encode_hex_into_lossy(bytes: &[u8]) -> String {
    bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}

/// Validate subscription id grammar:
/// `^[a-z0-9][a-z0-9._-]*\.v[1-9][0-9]*$` with length and dot rules.
fn validate_subscription_id(id: &str) -> Result<(), &'static str> {
    if id.is_empty() {
        return Err("empty subscription id");
    }
    if id.len() > MAX_SUBSCRIPTION_ID_BYTES {
        return Err("subscription id longer than 128 bytes");
    }
    if id.starts_with('.') || id.ends_with('.') {
        return Err("subscription id has a leading or trailing '.'");
    }
    if id.contains("..") {
        return Err("subscription id has a doubled '.'");
    }
    let Some(dot_v) = id.rfind(".v") else {
        return Err("subscription id must contain a .v version suffix");
    };
    let name = &id[..dot_v];
    let version = &id[dot_v + 2..];
    if name.is_empty() {
        return Err("subscription id name prefix is empty");
    }
    if !name
        .bytes()
        .next()
        .is_some_and(|b| b.is_ascii_lowercase() || b.is_ascii_digit())
    {
        return Err("subscription id must start with [a-z0-9]");
    }
    if !name
        .bytes()
        .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || matches!(b, b'.' | b'_' | b'-'))
    {
        return Err("subscription id has characters outside [a-z0-9._-]");
    }
    if version.is_empty() {
        return Err("subscription id missing version digits after .v");
    }
    let first = version.as_bytes()[0];
    if !first.is_ascii_digit() || first == b'0' {
        return Err("subscription id version must start with 1-9");
    }
    if !version.chars().all(|c| c.is_ascii_digit()) {
        return Err("subscription id version must be digits only");
    }
    Ok(())
}

fn validate_reason_code(code: &str) -> Result<(), &'static str> {
    if code.is_empty() {
        return Err("empty reason code");
    }
    if code.len() > MAX_STREAM_REASON_CODE_BYTES {
        return Err("reason code longer than 128 bytes");
    }
    if !code
        .bytes()
        .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || matches!(b, b'.' | b'_' | b'-'))
    {
        return Err("reason code has characters outside [a-z0-9._-]");
    }
    Ok(())
}

fn validate_payload_schema_ref(reference: &str) -> Result<(), &'static str> {
    if reference.is_empty() {
        return Err("empty payload schema ref");
    }
    if reference.len() > MAX_PAYLOAD_SCHEMA_REF_BYTES {
        return Err("payload schema ref longer than 256 bytes");
    }
    if reference.contains(char::is_whitespace) {
        return Err("payload schema ref contains whitespace");
    }
    if !reference
        .bytes()
        .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || matches!(b, b'.' | b'_' | b'-'))
    {
        return Err("payload schema ref has characters outside [a-z0-9._-]");
    }
    Ok(())
}