repe 9.0.0

Rust implementation of the REPE RPC protocol (JSON-focused)
Documentation
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
use crate::constants::{BodyFormat, ErrorCode, HEADER_SIZE, QueryFormat};
use crate::error::RepeError;
use crate::header::Header;
use beve::{from_slice as beve_from_slice, to_vec as beve_to_vec};
use std::borrow::Cow;
use std::io::{self, Write};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Message {
    pub header: Header,
    pub query: Vec<u8>,
    pub body: Vec<u8>,
}

impl Message {
    pub fn new(header: Header, query: Vec<u8>, body: Vec<u8>) -> Result<Self, RepeError> {
        if header.query_length != query.len() as u64 || header.body_length != body.len() as u64 {
            return Err(RepeError::LengthMismatch {
                // Saturating: these are the caller's own advertised lengths, and
                // this arm is already reporting that they are wrong. A `u64` sum
                // that wraps here would turn a bad-input report into a panic in
                // debug builds, for no gain — the number only has to be large
                // enough to show the mismatch.
                expected: (HEADER_SIZE as u64)
                    .saturating_add(header.query_length)
                    .saturating_add(header.body_length),
                got: HEADER_SIZE as u64 + query.len() as u64 + body.len() as u64,
            });
        }
        Ok(Self {
            header,
            query,
            body,
        })
    }

    pub fn builder() -> MessageBuilder {
        MessageBuilder::default()
    }

    pub fn to_vec(&self) -> Vec<u8> {
        let mut out = Vec::with_capacity(HEADER_SIZE + self.query.len() + self.body.len());
        out.extend_from_slice(&self.header.encode());
        if !self.query.is_empty() {
            out.extend_from_slice(&self.query);
        }
        if !self.body.is_empty() {
            out.extend_from_slice(&self.body);
        }
        out
    }

    /// Wire size of the serialized message: `HEADER_SIZE + query.len() + body.len()`.
    /// O(1); does not serialize.
    pub fn serialized_len(&self) -> usize {
        HEADER_SIZE + self.query.len() + self.body.len()
    }

    /// Write the message to `w` without allocating an intermediate frame buffer.
    ///
    /// Equivalent to `w.write_all(&self.to_vec())` byte-for-byte, but emits the
    /// header, query, and body in three writes instead of building a single
    /// owned `Vec<u8>`. Useful when the body is large (e.g. multi-MiB chunks).
    pub fn write_to<W: Write>(&self, w: &mut W) -> io::Result<()> {
        w.write_all(&self.header.encode())?;
        if !self.query.is_empty() {
            w.write_all(&self.query)?;
        }
        if !self.body.is_empty() {
            w.write_all(&self.body)?;
        }
        Ok(())
    }

    /// Consume the message and produce its wire-frame bytes.
    ///
    /// Prefer this over [`to_vec`](Self::to_vec) on outbound paths where the
    /// `Message` is being shipped to a sink that takes an owned `Vec<u8>` (e.g.
    /// the WebSocket writer): `to_vec` always allocates a fresh frame buffer
    /// and copies the body into it, whereas `into_wire_bytes` reuses the body
    /// allocation when it already has the spare capacity to host the header
    /// and query prefix.
    ///
    /// Fast path (zero new allocations, one body memcpy to shift to the back):
    /// triggers when `body.capacity() >= HEADER_SIZE + query.len() +
    /// body.len()`. Callers that care about streaming throughput can guarantee
    /// the fast path by constructing the body via
    /// `Vec::with_capacity(body_len + HEADER_SIZE + query.len())` and then
    /// passing it to `MessageBuilder::body_bytes`.
    ///
    /// Slow path: when the body has no spare capacity for the prefix, a fresh
    /// `Vec<u8>` is allocated. The byte output is identical to `to_vec`.
    pub fn into_wire_bytes(self) -> Vec<u8> {
        let Self {
            header,
            query,
            mut body,
        } = self;
        let prefix_len = HEADER_SIZE + query.len();
        let body_len = body.len();
        let total = prefix_len + body_len;

        if body.capacity() >= total {
            // Reuse the body allocation: grow in place, shift the body bytes
            // to the back, then overwrite the prefix with header + query.
            body.resize(total, 0);
            if body_len > 0 {
                body.copy_within(0..body_len, prefix_len);
            }
            body[..HEADER_SIZE].copy_from_slice(&header.encode());
            if !query.is_empty() {
                body[HEADER_SIZE..prefix_len].copy_from_slice(&query);
            }
            body
        } else {
            // No room in body for the prefix: fall back to a fresh allocation.
            // Same cost as `to_vec`, but consumes self so the original query
            // and body buffers are released as soon as the wire bytes are
            // produced.
            let mut out = Vec::with_capacity(total);
            out.extend_from_slice(&header.encode());
            if !query.is_empty() {
                out.extend_from_slice(&query);
            }
            if body_len > 0 {
                out.append(&mut body);
            }
            out
        }
    }

    pub fn from_slice(buf: &[u8]) -> Result<Self, RepeError> {
        if buf.len() < HEADER_SIZE {
            return Err(RepeError::InvalidHeaderLength(buf.len()));
        }
        let header = Header::decode(&buf[..HEADER_SIZE])?;
        let expected = HEADER_SIZE + header.query_length as usize + header.body_length as usize;
        if buf.len() < expected {
            return Err(RepeError::BufferTooSmall {
                need: expected,
                have: buf.len(),
            });
        }
        let mut o = HEADER_SIZE;
        let query = buf[o..o + header.query_length as usize].to_vec();
        o += header.query_length as usize;
        let body = buf[o..o + header.body_length as usize].to_vec();
        Self::new(header, query, body)
    }

    pub fn from_slice_exact(buf: &[u8]) -> Result<Self, RepeError> {
        let message = Self::from_slice(buf)?;
        let expected = HEADER_SIZE + message.query.len() + message.body.len();
        if buf.len() != expected {
            return Err(RepeError::LengthMismatch {
                expected: expected as u64,
                got: buf.len() as u64,
            });
        }
        Ok(message)
    }

    pub fn is_error(&self) -> bool {
        self.header.ec != ErrorCode::Ok as u32
    }

    pub fn error_code(&self) -> Option<ErrorCode> {
        ErrorCode::try_from(self.header.ec).ok()
    }

    pub fn error_message_utf8(&self) -> Option<String> {
        if !self.is_error() {
            return None;
        }
        Some(String::from_utf8_lossy(&self.body).into_owned())
    }

    pub fn query_utf8(&self) -> String {
        match self.query_str() {
            Ok(s) => s.to_owned(),
            Err(_) => String::from_utf8_lossy(&self.query).into_owned(),
        }
    }

    pub fn query_str(&self) -> Result<&str, std::str::Utf8Error> {
        std::str::from_utf8(&self.query)
    }

    pub fn body_utf8(&self) -> String {
        String::from_utf8_lossy(&self.body).into_owned()
    }

    pub fn json_body<T: serde::de::DeserializeOwned>(&self) -> Result<T, RepeError> {
        self.require_body_format(BodyFormat::Json)?;
        Ok(serde_json::from_slice(&self.body)?)
    }

    pub fn beve_body<T: serde::de::DeserializeOwned>(&self) -> Result<T, RepeError> {
        self.require_body_format(BodyFormat::Beve)?;
        Ok(beve_from_slice(&self.body)?)
    }

    /// Decode a BEVE typed-numeric-array body into a `Vec<T>` via a single
    /// bounds-checked bulk read, bypassing serde.
    ///
    /// The decode counterpart of [`MessageBuilder::body_typed_slice`], and the
    /// whole-body fast path for a high-throughput numeric payload. Errors with
    /// [`RepeError::UnexpectedBodyFormat`] if the body is not
    /// [`BodyFormat::Beve`], or [`RepeError::Beve`] if the bytes are not a typed
    /// numeric array of `T` (wrong element class/width, or a truncated payload).
    /// Works on any BEVE typed numeric array, including one produced by serde via
    /// [`body_beve`](MessageBuilder::body_beve) over a `Vec<T>`.
    pub fn decode_typed_slice<T: beve::BeveTypedSlice>(&self) -> Result<Vec<T>, RepeError> {
        self.require_body_format(BodyFormat::Beve)?;
        Ok(beve::read_typed_slice(&self.body)?)
    }

    /// Decode a BEVE complex-array body into a `Vec<Complex<T>>` via a single
    /// bounds-checked bulk read, bypassing serde.
    ///
    /// The complex counterpart of [`decode_typed_slice`](Self::decode_typed_slice)
    /// and the decode counterpart of
    /// [`MessageBuilder::body_complex_slice`]. Same error contract.
    pub fn decode_complex_slice<T: beve::BeveTypedSlice>(
        &self,
    ) -> Result<Vec<beve::Complex<T>>, RepeError> {
        self.require_body_format(BodyFormat::Beve)?;
        Ok(beve::read_complex_slice(&self.body)?)
    }

    /// `Ok(())` if the body's format matches `expected`, else
    /// [`RepeError::UnexpectedBodyFormat`]. The shared format guard for the
    /// body decoders ([`json_body`](Self::json_body), [`beve_body`](Self::beve_body),
    /// [`decode_typed_slice`](Self::decode_typed_slice), and
    /// [`decode_complex_slice`](Self::decode_complex_slice)), so a wrong-format
    /// body produces one structured error shape across all of them.
    fn require_body_format(&self, expected: BodyFormat) -> Result<(), RepeError> {
        if self.header.body_format == expected as u16 {
            Ok(())
        } else {
            Err(RepeError::UnexpectedBodyFormat {
                expected,
                got: self.header.body_format,
            })
        }
    }
}

/// Borrowing view over a serialized REPE message.
///
/// Unlike [`Message::from_slice`], which copies the query and body out of the
/// caller's buffer, `MessageView` keeps both as borrowed slices of the input.
/// Useful when a large body (e.g. a multi-MiB chunk) will be deserialized with
/// `serde_bytes::Bytes<'a>` so the bulk payload stays borrowed end-to-end.
///
/// The `header` is decoded by value because it's only 48 bytes and downstream
/// code typically wants the parsed fields rather than the raw header bytes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MessageView<'a> {
    pub header: Header,
    pub query: &'a [u8],
    pub body: &'a [u8],
}

impl<'a> MessageView<'a> {
    /// Parse a `MessageView` from `buf`. The returned view borrows from `buf`;
    /// no copies are made.
    pub fn from_slice(buf: &'a [u8]) -> Result<Self, RepeError> {
        if buf.len() < HEADER_SIZE {
            return Err(RepeError::InvalidHeaderLength(buf.len()));
        }
        let header = Header::decode(&buf[..HEADER_SIZE])?;
        let expected = HEADER_SIZE + header.query_length as usize + header.body_length as usize;
        if buf.len() < expected {
            return Err(RepeError::BufferTooSmall {
                need: expected,
                have: buf.len(),
            });
        }
        let q_start = HEADER_SIZE;
        let q_end = q_start + header.query_length as usize;
        let b_end = q_end + header.body_length as usize;
        Ok(Self {
            header,
            query: &buf[q_start..q_end],
            body: &buf[q_end..b_end],
        })
    }

    /// Like [`from_slice`](Self::from_slice) but rejects trailing bytes: errors
    /// if `buf` is longer than the framed message. The borrowing counterpart of
    /// [`Message::from_slice_exact`], for transports (e.g. a WebSocket binary
    /// frame) that carry exactly one message per buffer.
    pub fn from_slice_exact(buf: &'a [u8]) -> Result<Self, RepeError> {
        let view = Self::from_slice(buf)?;
        let expected = HEADER_SIZE + view.query.len() + view.body.len();
        if buf.len() != expected {
            return Err(RepeError::LengthMismatch {
                expected: expected as u64,
                got: buf.len() as u64,
            });
        }
        Ok(view)
    }

    /// View the query as a `&str`. Errors if the query is not valid UTF-8.
    pub fn query_str(&self) -> Result<&'a str, std::str::Utf8Error> {
        std::str::from_utf8(self.query)
    }

    /// Copy this borrowed view into an owned [`Message`], allocating the query
    /// and body. Used as the fallback for [`HandlerErased::handle_view`] when a
    /// handler has not overridden the borrowing path.
    ///
    /// [`HandlerErased::handle_view`]: crate::server::HandlerErased::handle_view
    pub fn to_message(&self) -> Message {
        Message {
            header: self.header,
            query: self.query.to_vec(),
            body: self.body.to_vec(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::{Deserialize, Serialize};

    #[test]
    fn json_body_non_json_errors() {
        let msg = Message::builder()
            .id(1)
            .query_str("/q")
            .query_format(QueryFormat::JsonPointer)
            .body_utf8("not json")
            .build();
        let err = msg.json_body::<serde_json::Value>().unwrap_err();
        assert!(
            matches!(
                err,
                RepeError::UnexpectedBodyFormat {
                    expected: BodyFormat::Json,
                    ..
                }
            ),
            "expected UnexpectedBodyFormat, got {err:?}"
        );
    }

    #[derive(Serialize, Deserialize, Debug, PartialEq)]
    struct Pair {
        a: i32,
        b: i32,
    }

    #[test]
    fn create_response_json_and_utf8() {
        let req = Message::builder()
            .id(5)
            .query_str("/sum")
            .query_format(QueryFormat::JsonPointer)
            .body_json(&Pair { a: 1, b: 2 })
            .unwrap()
            .build();

        // JSON body
        let resp_json =
            create_response(&req, serde_json::json!({"ok": true}), BodyFormat::Json).unwrap();
        assert_eq!(resp_json.header.id, 5);
        assert_eq!(resp_json.header.ec, ErrorCode::Ok as u32);
        assert_eq!(resp_json.header.body_format, BodyFormat::Json as u16);

        // UTF8 body (stringified JSON)
        let resp_utf8 =
            create_response(&req, serde_json::json!([1, 2, 3]), BodyFormat::Utf8).unwrap();
        assert_eq!(resp_utf8.header.body_format, BodyFormat::Utf8 as u16);
        assert!(std::str::from_utf8(&resp_utf8.body).unwrap().contains("1"));
    }

    #[test]
    fn message_from_slice_truncated_returns_buffer_too_small() {
        let msg = Message::builder()
            .id(10)
            .query_str("/a")
            .query_format(QueryFormat::JsonPointer)
            .body_utf8("payload")
            .build();
        let mut bytes = msg.to_vec();
        let full_len = bytes.len();
        bytes.truncate(bytes.len() - 1);
        match Message::from_slice(&bytes).unwrap_err() {
            RepeError::BufferTooSmall { need, have } => {
                assert_eq!(need, full_len);
                assert_eq!(have, full_len - 1);
            }
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[test]
    fn message_from_slice_exact_rejects_trailing_bytes() {
        let msg = Message::builder()
            .id(11)
            .query_str("/a")
            .query_format(QueryFormat::JsonPointer)
            .body_utf8("payload")
            .build();
        let mut bytes = msg.to_vec();
        bytes.extend_from_slice(&[0xAA, 0xBB]);

        match Message::from_slice_exact(&bytes).unwrap_err() {
            RepeError::LengthMismatch { expected, got } => {
                assert_eq!(expected, msg.to_vec().len() as u64);
                assert_eq!(got, bytes.len() as u64);
            }
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[test]
    fn create_response_beve_serializes_body() {
        let req = Message::builder()
            .id(3)
            .query_str("/noop")
            .query_format(QueryFormat::JsonPointer)
            .body_utf8("{}");
        let req = req.build();

        let resp =
            create_response(&req, serde_json::json!({"ignored": true}), BodyFormat::Beve).unwrap();
        assert_eq!(resp.header.id, req.header.id);
        assert_eq!(resp.query, req.query);
        assert!(!resp.body.is_empty());
        assert_eq!(resp.header.body_format, BodyFormat::Beve as u16);

        let value: serde_json::Value = resp.beve_body().unwrap();
        assert_eq!(value["ignored"], true);
    }

    #[test]
    fn unstamped_plus_stamp_equals_create_response() {
        let req = Message::builder()
            .id(5)
            .query_str("/sum")
            .query_format(QueryFormat::JsonPointer)
            .body_json(&Pair { a: 1, b: 2 })
            .unwrap()
            .build();
        let value = serde_json::json!({"ok": true});

        // The echoing path.
        let echoed = create_response(&req, &value, BodyFormat::Json).unwrap();

        // The boundary path: build query-less, then stamp the request query in.
        // The off-reader boundary owns its request, so it moves the buffer in
        // (`Cow::Owned`, no copy).
        let mut staged = create_response_unstamped(&req, &value, BodyFormat::Json).unwrap();
        assert!(staged.query.is_empty(), "handler leaves the query empty");
        assert_eq!(staged.header.query_length, 0);
        stamp_response_query(&mut staged, Cow::Owned(req.query.clone()));

        // Byte-for-byte identical to the echoing path, including the header.
        assert_eq!(staged, echoed);
        assert_eq!(staged.query, req.query);
        assert_eq!(staged.header.length, echoed.header.length);

        // Stamp is a no-op once a query is present (e.g. an error response) and
        // when the request query is empty; a borrowed query is then never copied.
        let before = staged.clone();
        stamp_response_query(&mut staged, Cow::Borrowed(&b"/other"[..]));
        assert_eq!(staged, before, "stamp must not overwrite an existing query");
    }

    #[test]
    fn error_response_view_matches_owned_query_format() {
        // The borrowing error path must frame byte-identically to the owned
        // (WebSocket) path. Both leave query_format at the create_error_message
        // default (RawBinary), so a JsonPointer request still yields the same
        // error frame regardless of transport.
        let req = Message::builder()
            .id(9)
            .query_str("/missing")
            .query_format(QueryFormat::JsonPointer)
            .body_utf8("{}")
            .build();
        let owned = create_error_response_like(&req, ErrorCode::MethodNotFound, "nope");

        let bytes = req.to_vec();
        let view = MessageView::from_slice(&bytes).unwrap();
        let viewed = create_error_response_unstamped_view(&view, ErrorCode::MethodNotFound, "nope");

        assert_eq!(viewed.header.query_format, owned.header.query_format);
        assert_eq!(viewed.header.id, owned.header.id);
        assert_eq!(viewed.header.ec, owned.header.ec);
        assert_eq!(viewed.body, owned.body);
    }

    #[test]
    fn response_echo_query_prefers_a_handler_set_query() {
        // Empty response query -> echo the request query (the common case).
        let empty = Message::builder().id(1).build();
        assert_eq!(response_echo_query(&empty, b"/req"), b"/req");

        // A query the handler set itself is preserved, not overwritten.
        let custom = Message::builder()
            .id(1)
            .query_str("/handler-set")
            .query_format(QueryFormat::JsonPointer)
            .build();
        assert_eq!(response_echo_query(&custom, b"/req"), b"/handler-set");
    }

    #[test]
    fn create_response_propagates_serialization_error() {
        struct Fails;

        impl serde::Serialize for Fails {
            fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
            where
                S: serde::Serializer,
            {
                Err(serde::ser::Error::custom("nope"))
            }
        }

        let req = Message::builder()
            .id(12)
            .query_str("/bad")
            .query_format(QueryFormat::JsonPointer)
            .body_utf8("{}");
        let req = req.build();

        let err = create_response(&req, Fails, BodyFormat::Json).unwrap_err();
        matches!(err, RepeError::Json(_));
    }

    #[test]
    fn body_beve_roundtrip() {
        #[derive(Serialize, Deserialize, Debug, PartialEq)]
        struct Data {
            x: i32,
            y: String,
        }

        let msg = Message::builder()
            .id(7)
            .query_str("/d")
            .query_format(QueryFormat::JsonPointer)
            .body_beve(&Data {
                x: 10,
                y: "ok".into(),
            })
            .unwrap()
            .build();

        assert_eq!(msg.header.body_format, BodyFormat::Beve as u16);
        let decoded: Data = msg.beve_body().unwrap();
        assert_eq!(
            decoded,
            Data {
                x: 10,
                y: "ok".into()
            }
        );
    }

    #[test]
    fn message_view_from_slice_borrows() {
        let msg = Message::builder()
            .id(7)
            .query_str("/echo")
            .query_format(QueryFormat::JsonPointer)
            .body_bytes(vec![1u8, 2, 3, 4])
            .body_format(BodyFormat::RawBinary)
            .build();
        let bytes = msg.to_vec();

        let view = MessageView::from_slice(&bytes).unwrap();
        assert_eq!(view.header, msg.header);
        assert_eq!(view.query, msg.query.as_slice());
        assert_eq!(view.body, msg.body.as_slice());

        // Verify the slices point inside `bytes`, not into a fresh allocation.
        let bytes_start = bytes.as_ptr() as usize;
        let bytes_end = bytes_start + bytes.len();
        let q_addr = view.query.as_ptr() as usize;
        let b_addr = view.body.as_ptr() as usize;
        assert!(q_addr >= bytes_start && q_addr <= bytes_end);
        assert!(b_addr >= bytes_start && b_addr <= bytes_end);

        assert_eq!(view.query_str().unwrap(), "/echo");
    }

    #[test]
    fn message_view_truncated_returns_buffer_too_small() {
        let msg = Message::builder()
            .id(1)
            .query_str("/x")
            .query_format(QueryFormat::JsonPointer)
            .body_utf8("payload")
            .build();
        let mut bytes = msg.to_vec();
        let full_len = bytes.len();
        bytes.truncate(bytes.len() - 1);
        match MessageView::from_slice(&bytes).unwrap_err() {
            RepeError::BufferTooSmall { need, have } => {
                assert_eq!(need, full_len);
                assert_eq!(have, full_len - 1);
            }
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[test]
    fn write_to_matches_to_vec() {
        let msg = Message::builder()
            .id(42)
            .query_str("/collect/file_chunk")
            .query_format(QueryFormat::JsonPointer)
            .body_bytes(vec![0xDEu8; 1024])
            .body_format(BodyFormat::Beve)
            .build();

        let expected = msg.to_vec();
        let mut got = Vec::new();
        msg.write_to(&mut got).expect("write_to");
        assert_eq!(got, expected);
        assert_eq!(msg.serialized_len(), expected.len());
    }

    #[test]
    fn write_to_handles_empty_query_and_body() {
        let msg = Message::builder().id(1).build();
        let expected = msg.to_vec();
        let mut got = Vec::new();
        msg.write_to(&mut got).expect("write_to");
        assert_eq!(got, expected);
        assert_eq!(msg.serialized_len(), expected.len());
    }

    #[test]
    fn into_wire_bytes_matches_to_vec_slow_path() {
        // Body allocated with cap == len, so into_wire_bytes hits the fresh-
        // allocation fallback. Output must still equal to_vec.
        let msg = Message::builder()
            .id(42)
            .query_str("/collect/file_chunk")
            .query_format(QueryFormat::JsonPointer)
            .body_bytes(vec![0xDEu8; 1024])
            .body_format(BodyFormat::Beve)
            .build();
        let expected = msg.to_vec();
        let got = msg.into_wire_bytes();
        assert_eq!(got, expected);
    }

    #[test]
    fn into_wire_bytes_matches_to_vec_fast_path() {
        // Body pre-allocated with prefix room so into_wire_bytes takes the
        // in-place fast path. Output must still equal to_vec.
        let query = b"/collect/file_chunk";
        let body_len = 4096;
        let mut body = Vec::with_capacity(HEADER_SIZE + query.len() + body_len);
        body.resize(body_len, 0xAB);
        let msg = Message::builder()
            .id(42)
            .query_bytes(query.to_vec())
            .query_format(QueryFormat::JsonPointer)
            .body_bytes(body)
            .body_format(BodyFormat::Beve)
            .build();
        let expected = msg.clone().to_vec();
        let got = msg.into_wire_bytes();
        assert_eq!(got, expected);
    }

    #[test]
    fn into_wire_bytes_handles_empty_query_and_body() {
        let msg = Message::builder().id(1).build();
        let expected = msg.to_vec();
        let got = msg.into_wire_bytes();
        assert_eq!(got, expected);
    }

    #[test]
    fn into_wire_bytes_handles_empty_body_only() {
        let msg = Message::builder()
            .id(2)
            .query_str("/notify")
            .query_format(QueryFormat::JsonPointer)
            .build();
        let expected = msg.to_vec();
        let got = msg.into_wire_bytes();
        assert_eq!(got, expected);
    }

    #[test]
    fn into_wire_bytes_handles_empty_query_only() {
        let msg = Message::builder()
            .id(3)
            .body_bytes(vec![1u8, 2, 3])
            .body_format(BodyFormat::RawBinary)
            .build();
        let expected = msg.clone().to_vec();
        let got = msg.into_wire_bytes();
        assert_eq!(got, expected);
    }

    #[test]
    fn into_wire_bytes_fast_path_does_not_reallocate() {
        // Confirm that when the body has room for the prefix, the resulting
        // Vec reuses the body's original allocation (same data pointer).
        let query = b"/q";
        let body_len = 256;
        let total = HEADER_SIZE + query.len() + body_len;
        let mut body = Vec::with_capacity(total);
        body.resize(body_len, 0x42);
        let body_ptr = body.as_ptr();
        let msg = Message::builder()
            .id(1)
            .query_bytes(query.to_vec())
            .query_format(QueryFormat::JsonPointer)
            .body_bytes(body)
            .body_format(BodyFormat::RawBinary)
            .build();
        let wire = msg.into_wire_bytes();
        assert_eq!(wire.len(), total);
        assert_eq!(
            wire.as_ptr(),
            body_ptr,
            "fast path should reuse body buffer"
        );
    }

    #[test]
    fn typed_slice_body_reserves_wire_prefix_headroom() {
        // With the query set before the body, `body_typed_slice` must reserve
        // `HEADER_SIZE + query.len()` of spare capacity after the encoded
        // payload so `into_wire_bytes` reuses the body allocation (no fresh
        // frame buffer). Verified by pointer identity across the framing.
        let query = b"/sensors/raw";
        let data: Vec<f64> = (0..512).map(|i| i as f64 * 0.25).collect();
        let msg = Message::builder()
            .query_bytes(query.to_vec())
            .query_format(QueryFormat::JsonPointer)
            .body_typed_slice(&data)
            .build();
        let total = HEADER_SIZE + query.len() + msg.body.len();
        let body_ptr = msg.body.as_ptr();
        let expected = msg.clone().to_vec();
        let wire = msg.into_wire_bytes();
        assert_eq!(wire, expected, "wire bytes must equal to_vec");
        assert_eq!(wire.len(), total);
        assert_eq!(
            wire.as_ptr(),
            body_ptr,
            "typed-slice body headroom should let into_wire_bytes reuse the body buffer"
        );
    }

    #[test]
    fn complex_slice_body_reserves_wire_prefix_headroom() {
        let query = b"/spectra/iq";
        let data: Vec<beve::Complex<f64>> = (0..256)
            .map(|i| beve::Complex {
                re: i as f64,
                im: -(i as f64) * 0.5,
            })
            .collect();
        let msg = Message::builder()
            .query_bytes(query.to_vec())
            .query_format(QueryFormat::JsonPointer)
            .body_complex_slice(&data)
            .build();
        let total = HEADER_SIZE + query.len() + msg.body.len();
        let body_ptr = msg.body.as_ptr();
        let expected = msg.clone().to_vec();
        let wire = msg.into_wire_bytes();
        assert_eq!(wire, expected, "wire bytes must equal to_vec");
        assert_eq!(wire.len(), total);
        assert_eq!(
            wire.as_ptr(),
            body_ptr,
            "complex-slice body headroom should let into_wire_bytes reuse the body buffer"
        );
    }

    #[test]
    fn typed_slice_body_query_after_body_falls_back_to_fresh_frame() {
        // The headroom is sized from the query length known at `body_typed_slice`
        // time. With the query set *afterward*, the reserved prefix covers only
        // the header, so a non-empty query no longer fits and `into_wire_bytes`
        // takes the slow path (fresh frame). This pins the documented fallback:
        // bytes still equal `to_vec`, but the body allocation is not reused.
        let query = b"/sensors/raw";
        let data: Vec<f64> = (0..512).map(|i| i as f64 * 0.25).collect();
        let msg = Message::builder()
            .body_typed_slice(&data)
            .query_bytes(query.to_vec())
            .query_format(QueryFormat::JsonPointer)
            .build();
        let body_ptr = msg.body.as_ptr();
        let expected = msg.clone().to_vec();
        let wire = msg.into_wire_bytes();
        assert_eq!(wire, expected, "wire bytes must equal to_vec");
        assert_ne!(
            wire.as_ptr(),
            body_ptr,
            "query set after body should not fit the reserved prefix; expected a fresh frame"
        );
    }

    #[test]
    fn beve_body_non_beve_errors() {
        let msg = Message::builder()
            .id(2)
            .query_str("/a")
            .query_format(QueryFormat::JsonPointer)
            .body_utf8("text")
            .build();

        let err = msg.beve_body::<serde_json::Value>().unwrap_err();
        assert!(
            matches!(
                err,
                RepeError::UnexpectedBodyFormat {
                    expected: BodyFormat::Beve,
                    ..
                }
            ),
            "expected UnexpectedBodyFormat, got {err:?}"
        );
    }

    #[test]
    fn builder_format_code_methods_set_explicit_codes() {
        let msg = Message::builder()
            .id(9)
            .query_str("/raw")
            .query_format_code(0x7777)
            .body_bytes(vec![1, 2, 3])
            .body_format_code(0x8888)
            .build();

        assert_eq!(msg.header.query_format, 0x7777);
        assert_eq!(msg.header.body_format, 0x8888);
    }

    /// Copy `frame` into a genuinely 8-aligned backing buffer (via `Vec<u64>`) and
    /// return it, so a test can validate the zero-copy borrow at a known-aligned
    /// base address.
    fn into_aligned_buffer(frame: &[u8]) -> Vec<u64> {
        // `+ 1` word guarantees room even when `frame.len()` is a multiple of 8
        // (then `words * 8 == frame.len() + 8`), so the byte view always fits.
        let words = frame.len() / 8 + 1;
        let mut backing: Vec<u64> = vec![0; words];
        // SAFETY: `backing` is 8-aligned and large enough to hold `frame`.
        let bytes =
            unsafe { std::slice::from_raw_parts_mut(backing.as_mut_ptr() as *mut u8, words * 8) };
        bytes[..frame.len()].copy_from_slice(frame);
        backing
    }

    fn aligned_request(id: u64, path: &str, data: &[f64]) -> Message {
        Message::builder()
            .id(id)
            .query_str(path)
            .query_format(QueryFormat::JsonPointer)
            .body_aligned_typed_slice(data)
            .build()
    }

    #[test]
    fn aligned_body_builder_header_and_payload_round_trip() {
        let data: Vec<f64> = (0..300).map(|i| i as f64 * 0.5).collect();
        let frame = aligned_request(42, "/scale", &data).to_vec();

        // The header parses, the lengths add up, and the body is a valid aligned
        // typed array that decodes (owned, always works) back to the input.
        let view = MessageView::from_slice(&frame).expect("frame parses");
        assert_eq!(view.header.id, 42);
        assert_eq!(view.header.query_format, QueryFormat::JsonPointer as u16);
        assert_eq!(view.header.body_format, BodyFormat::Beve as u16);
        assert_eq!(view.query, b"/scale");
        assert_eq!(view.header.length as usize, frame.len());
        let back = beve::read_aligned_typed_slice::<f64>(view.body).expect("owned decode");
        assert_eq!(back, data);
    }

    #[test]
    fn aligned_body_payload_is_borrowable_when_buffer_is_aligned() {
        // Validate the padding math end to end: when the whole frame sits at an
        // 8-aligned base address, the f64 payload is aligned in memory and the
        // zero-copy borrow succeeds. A 7-byte query makes the unpadded payload
        // offset odd, so a correct borrow can only come from the inserted padding.
        let data: Vec<f64> = (0..257).map(|i| 1.0 / (i as f64 + 1.0)).collect();
        let frame = aligned_request(1, "/abcdef", &data).to_vec();

        let backing = into_aligned_buffer(&frame);
        let bytes =
            unsafe { std::slice::from_raw_parts(backing.as_ptr() as *const u8, frame.len()) };

        let view = MessageView::from_slice(bytes).expect("frame parses");
        let borrowed: &[f64] =
            beve::read_aligned_typed_slice_ref::<f64>(view.body).expect("zero-copy borrow");
        assert_eq!(borrowed, data.as_slice());
    }

    #[test]
    fn aligned_body_survives_into_wire_bytes() {
        // `into_wire_bytes` shifts the body to `HEADER_SIZE + query.len()`, which is
        // exactly the offset `body_aligned_typed_slice` padded for, so the alignment
        // is preserved through that outbound path too (not just `to_vec`).
        let data: Vec<f64> = (0..130).map(|i| i as f64 - 64.0).collect();
        let frame = aligned_request(7, "/abcdef", &data).into_wire_bytes();
        assert_eq!(
            beve::read_aligned_typed_slice::<f64>(MessageView::from_slice(&frame).unwrap().body)
                .unwrap(),
            data
        );

        let backing = into_aligned_buffer(&frame);
        let bytes =
            unsafe { std::slice::from_raw_parts(backing.as_ptr() as *const u8, frame.len()) };
        let view = MessageView::from_slice(bytes).expect("frame parses");
        let borrowed: &[f64] =
            beve::read_aligned_typed_slice_ref::<f64>(view.body).expect("zero-copy borrow");
        assert_eq!(borrowed, data.as_slice());
    }
}

#[derive(Default)]
pub struct MessageBuilder {
    id: u64,
    query: Vec<u8>,
    body: Vec<u8>,
    query_format: u16,
    body_format: u16,
    notify: bool,
    ec: u32,
}

impl MessageBuilder {
    pub fn id(mut self, id: u64) -> Self {
        self.id = id;
        self
    }
    pub fn notify(mut self, notify: bool) -> Self {
        self.notify = notify;
        self
    }
    pub fn error_code(mut self, ec: ErrorCode) -> Self {
        self.ec = ec as u32;
        self
    }
    pub fn query_format(mut self, f: QueryFormat) -> Self {
        self.query_format = u16::from(f);
        self
    }
    pub fn query_format_code(mut self, format_code: u16) -> Self {
        self.query_format = format_code;
        self
    }
    pub fn body_format(mut self, f: BodyFormat) -> Self {
        self.body_format = u16::from(f);
        self
    }
    pub fn body_format_code(mut self, format_code: u16) -> Self {
        self.body_format = format_code;
        self
    }
    pub fn query_bytes(mut self, q: impl Into<Vec<u8>>) -> Self {
        self.query = q.into();
        self
    }
    pub fn query_str(mut self, q: &str) -> Self {
        self.query = q.as_bytes().to_vec();
        self
    }
    pub fn body_bytes(mut self, b: impl Into<Vec<u8>>) -> Self {
        self.body = b.into();
        self
    }
    pub fn body_utf8(mut self, s: &str) -> Self {
        self.body = s.as_bytes().to_vec();
        self.body_format = BodyFormat::Utf8 as u16;
        self
    }
    pub fn body_json<T: serde::Serialize>(mut self, v: &T) -> Result<Self, RepeError> {
        self.body = serde_json::to_vec(v)?;
        self.body_format = BodyFormat::Json as u16;
        Ok(self)
    }

    pub fn body_beve<T: serde::Serialize>(mut self, v: &T) -> Result<Self, RepeError> {
        self.body = beve_to_vec(v)?;
        self.body_format = BodyFormat::Beve as u16;
        Ok(self)
    }

    /// Encode a contiguous numeric slice as a BEVE typed array via a single bulk
    /// write, bypassing serde, and set [`BodyFormat::Beve`].
    ///
    /// This is the whole-body fast path for a high-throughput numeric payload
    /// (`&[f64]`, `&[i32]`, ...). The body bytes are identical to
    /// `body_beve(&slice.to_vec())`, but the encode is O(1) in the element count
    /// on little-endian targets (one `copy_nonoverlapping`) rather than the
    /// per-element serde walk. Decode the result with
    /// [`Message::decode_typed_slice`].
    ///
    /// Infallible: the bulk encoder cannot fail (contrast [`body_beve`], whose
    /// serde encode returns a `Result`).
    ///
    /// The body buffer is allocated with `HEADER_SIZE + query.len()` of spare
    /// capacity reserved after the encoded payload, so that shipping the built
    /// message through [`Message::into_wire_bytes`] reuses this allocation
    /// instead of allocating a fresh frame buffer. This headroom is only
    /// effective when the query is already set on the builder (the common
    /// `.query_*(..).body_typed_slice(..)` order); with the query set afterward
    /// the reserved prefix covers only the header and a non-empty query falls
    /// back to a fresh frame, exactly as before.
    ///
    /// [`body_beve`]: Self::body_beve
    pub fn body_typed_slice<T: beve::BeveTypedSlice>(mut self, slice: &[T]) -> Self {
        let body_len = beve::typed_slice_size(slice) as usize;
        let mut body = Vec::with_capacity(body_len + HEADER_SIZE + self.query.len());
        beve::to_writer_typed_slice(&mut body, slice)
            .expect("writing a typed slice into a Vec is infallible");
        self.body = body;
        self.body_format = BodyFormat::Beve as u16;
        self
    }

    /// Encode a contiguous complex slice as a BEVE complex array via a single
    /// bulk write, bypassing serde, and set [`BodyFormat::Beve`].
    ///
    /// The complex counterpart of [`body_typed_slice`]; same O(1)-encode
    /// property, and the same `HEADER_SIZE + query.len()` wire-prefix headroom
    /// for the [`Message::into_wire_bytes`] fast path. Decode with
    /// [`Message::decode_complex_slice`].
    ///
    /// [`body_typed_slice`]: Self::body_typed_slice
    pub fn body_complex_slice<T: beve::BeveTypedSlice>(
        mut self,
        slice: &[beve::Complex<T>],
    ) -> Self {
        let body_len = beve::complex_slice_size(slice) as usize;
        let mut body = Vec::with_capacity(body_len + HEADER_SIZE + self.query.len());
        beve::to_writer_complex_slice(&mut body, slice)
            .expect("writing a complex slice into a Vec is infallible");
        self.body = body;
        self.body_format = BodyFormat::Beve as u16;
        self
    }

    /// Encode a contiguous numeric slice as a BEVE *aligned* typed array, padded so
    /// the element block lands on an `align_of::<T>()` boundary within the final
    /// wire frame, and set [`BodyFormat::Beve`].
    ///
    /// The zero-copy counterpart of [`body_typed_slice`]: it emits BEVE's aligned
    /// typed-array form (an explicit padding run before the payload) so a
    /// [`Router::with_typed_slice_ref`] server reading the frame into an aligned
    /// buffer can borrow the body as `&[T]` with no element copy. Decode the owned
    /// view with [`Message::decode_typed_slice`] as usual; the borrow happens
    /// server-side.
    ///
    /// The padding is sized for the payload's absolute offset in the frame
    /// (`HEADER_SIZE + query.len()`), so **the query must already be set** when this
    /// is called (the common `.query_*(..).body_aligned_typed_slice(..)` order, and
    /// what the `call_typed_slice_aligned` client helpers do). If the query is set
    /// *after* the body, the padding is computed for the wrong offset and the
    /// payload will not be borrowable in the frame — the server then falls back to a
    /// bulk copy, so the result is still correct, just not zero-copy. The body
    /// buffer carries the same `HEADER_SIZE + query.len()` wire-prefix headroom as
    /// [`body_typed_slice`], and [`Message::into_wire_bytes`] shifts the body to
    /// exactly that offset, so the alignment is preserved through that path too.
    ///
    /// Unlike [`body_typed_slice`], the bytes are a distinct BEVE type and are *not*
    /// interchangeable with the serde / regular typed-array path; they pair
    /// specifically with a `with_typed_slice_ref` route.
    ///
    /// [`body_typed_slice`]: Self::body_typed_slice
    /// [`Router::with_typed_slice_ref`]: crate::server::Router::with_typed_slice_ref
    pub fn body_aligned_typed_slice<T: beve::BeveTypedSlice>(mut self, slice: &[T]) -> Self {
        let base_offset = HEADER_SIZE + self.query.len();
        let body_len = beve::aligned_typed_slice_size(slice, base_offset);
        // Reserve the `HEADER_SIZE + query.len()` wire-prefix headroom (== base_offset)
        // so `into_wire_bytes` reuses this allocation; its body shift lands the
        // payload at exactly `base_offset`, which is what the padding was sized for.
        let mut body = Vec::with_capacity(body_len + base_offset);
        beve::write_aligned_typed_slice_at(&mut body, slice, base_offset);
        debug_assert_eq!(body.len(), body_len);
        self.body = body;
        self.body_format = BodyFormat::Beve as u16;
        self
    }

    pub fn build(self) -> Message {
        let mut header = Header::new();
        header.id = self.id;
        header.query_length = self.query.len() as u64;
        header.body_length = self.body.len() as u64;
        header.length = HEADER_SIZE as u64 + header.query_length + header.body_length;
        header.query_format = if self.query_format == 0 {
            QueryFormat::RawBinary as u16
        } else {
            self.query_format
        };
        header.body_format = if self.body_format == 0 {
            BodyFormat::RawBinary as u16
        } else {
            self.body_format
        };
        header.notify = if self.notify { 1 } else { 0 };
        header.ec = self.ec;
        Message {
            header,
            query: self.query,
            body: self.body,
        }
    }
}

pub fn create_error_message(code: ErrorCode, msg: impl AsRef<str>) -> Message {
    let body = msg.as_ref().as_bytes().to_vec();
    Message::builder()
        .error_code(code)
        .body_bytes(body)
        .body_format(BodyFormat::Utf8)
        .build()
}

pub fn create_error_response_like(
    request: &Message,
    code: ErrorCode,
    msg: impl AsRef<str>,
) -> Message {
    create_error_response_for(request.header.id, &request.query, code, msg)
}

/// [`create_error_response_like`] against a request's id and query rather than
/// the whole request.
///
/// Those two fields are all an error response takes from a request; everything
/// else in it comes from [`create_error_message`]. A caller holding them
/// separately — a borrowed [`MessageView`], or a handler that has already split
/// them out — reaches this directly instead of materializing a `Message` for two
/// fields to be read back out of.
pub fn create_error_response_for(
    id: u64,
    query: &[u8],
    code: ErrorCode,
    msg: impl AsRef<str>,
) -> Message {
    let mut err = create_error_message(code, msg.as_ref());
    err.header.id = id;
    err.query = query.to_vec();
    err.header.query_length = err.query.len() as u64;
    err.header.length = HEADER_SIZE as u64 + err.header.query_length + err.header.body_length;
    err
}

pub fn create_response(
    request: &Message,
    result: impl serde::Serialize,
    body_format: BodyFormat,
) -> Result<Message, RepeError> {
    let builder = response_header_builder(request.header.id, request.header.query_format)
        .query_bytes(request.query.clone());
    finish_response(builder, result, body_format)
}

/// Build a success response that leaves the query **empty**, for the dispatch
/// boundary to fill by moving the request's query in (see
/// [`stamp_response_query`]).
///
/// REPE responses echo the request query verbatim. Rather than have each
/// built-in handler clone the request query into its response, the handlers
/// build the response with this and the transport boundary — which owns the
/// request and drops it right after — moves the query buffer in. That turns the
/// per-response query echo from an allocation + copy into a buffer move.
///
/// Byte-for-byte equivalent to [`create_response`] once
/// [`stamp_response_query`] has run with the originating request's query.
pub(crate) fn create_response_unstamped(
    request: &Message,
    result: impl serde::Serialize,
    body_format: BodyFormat,
) -> Result<Message, RepeError> {
    let builder = response_header_builder(request.header.id, request.header.query_format);
    finish_response(builder, result, body_format)
}

/// Query-less success response whose body is already encoded.
///
/// The pre-encoded twin of [`create_response_unstamped`], used by the
/// struct-handler dispatch: the handler serializes straight into a buffer via
/// [`ResponseBody`](crate::structs::ResponseBody), so there is no `impl Serialize`
/// left to run and the format is whatever that write settled on.
pub(crate) fn create_body_response_unstamped(
    request: &Message,
    body: Vec<u8>,
    body_format: BodyFormat,
) -> Message {
    response_header_builder(request.header.id, request.header.query_format)
        .body_bytes(body)
        .body_format(body_format)
        .build()
}

/// Echo `request_query` into a response left query-less by
/// [`create_response_unstamped`] / [`create_response_unstamped_view`], fixing up
/// the header lengths.
///
/// A no-op when the response already carries a query (an error response from
/// [`create_error_response_like`], or a custom handler that set its own) or when
/// the request query is empty, so it is safe to call on every dispatched
/// response. Takes a [`Cow`] so the owned/WebSocket off-reader boundary moves its
/// request query buffer straight in (`Cow::Owned`, no copy), while the
/// WebSocket-inline path passes a borrowed slice of the read buffer
/// (`Cow::Borrowed`) that is copied only when the stamp actually commits -- so a
/// custom-query handler that set its own response query pays no allocation.
// Used only by the WebSocket server: the off-reader path moves its owned request
// query in, the inline path copies the borrowed view query. The TCP/async servers
// echo through `response_echo_query` (a pure borrow) instead.
// The predicate mirrors `websocket_server`'s own gate in lib.rs, not just its
// feature: on wasm32 that module is absent even with `websocket` on.
#[cfg_attr(
    not(all(feature = "websocket", not(target_arch = "wasm32"))),
    allow(dead_code)
)]
pub(crate) fn stamp_response_query(response: &mut Message, request_query: Cow<[u8]>) {
    if request_query.is_empty() || !response.query.is_empty() {
        return;
    }
    response.query = request_query.into_owned();
    response.header.query_length = response.query.len() as u64;
    response.header.length =
        HEADER_SIZE as u64 + response.header.query_length + response.header.body_length;
}

/// Pick the query bytes the borrowing dispatch path frames `response` with.
///
/// The borrowing twin of [`stamp_response_query`]: the request query (a borrowed
/// slice of the read buffer) is echoed when the handler left the response query
/// empty, but a query the handler set itself is preserved. Keeps the TCP/async
/// borrowing writers consistent with the owned/WebSocket path on the
/// [`HandlerErased`](crate::server::HandlerErased) custom-response-query
/// contract, instead of unconditionally overwriting with the request query.
// Called only from the TCP and async servers' borrowing writers, both
// `#[cfg(not(target_arch = "wasm32"))]`, so it is dead on a wasm build.
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
pub(crate) fn response_echo_query<'a>(response: &'a Message, request_query: &'a [u8]) -> &'a [u8] {
    if response.query.is_empty() {
        request_query
    } else {
        &response.query
    }
}

/// Query-less success response whose entire body is a BEVE typed numeric array,
/// written via the bulk [`MessageBuilder::body_typed_slice`] path (one
/// `copy_nonoverlapping`, no per-element serde walk). The typed-slice twin of
/// [`create_response_unstamped`], used by [`Router::with_typed_slice`] so a numeric
/// `Vec<R>` result is framed without serializing element by element.
///
/// [`Router::with_typed_slice`]: crate::server::Router::with_typed_slice
pub(crate) fn create_typed_slice_response_unstamped<T: beve::BeveTypedSlice>(
    request: &Message,
    result: &[T],
) -> Message {
    response_header_builder(request.header.id, request.header.query_format)
        .body_typed_slice(result)
        .build()
}

/// Borrowing twin of [`create_typed_slice_response_unstamped`]: same bulk typed-array
/// framing, built from a [`MessageView`] for the allocation-free dispatch path.
pub(crate) fn create_typed_slice_response_unstamped_view<T: beve::BeveTypedSlice>(
    view: &MessageView,
    result: &[T],
) -> Message {
    response_header_builder(view.header.id, view.header.query_format)
        .body_typed_slice(result)
        .build()
}

/// Borrowing twin of [`create_response_unstamped`]: builds the same query-less
/// success response from a [`MessageView`], without materializing an owned
/// request. The view's query is echoed by the writer (e.g.
/// [`write_message_streaming`] with the borrowed query), not by this builder.
pub(crate) fn create_response_unstamped_view(
    view: &MessageView,
    result: impl serde::Serialize,
    body_format: BodyFormat,
) -> Result<Message, RepeError> {
    let builder = response_header_builder(view.header.id, view.header.query_format);
    finish_response(builder, result, body_format)
}

/// Borrowing, query-less error response from a [`MessageView`]. Mirrors
/// [`create_error_response_like`] exactly: it sets only the request id and leaves
/// the query empty (for the writer to supply from the borrowed view) and the
/// `query_format` at its [`create_error_message`] default, so a view-path error
/// frame is byte-identical to the owned/WebSocket path's.
pub(crate) fn create_error_response_unstamped_view(
    view: &MessageView,
    code: ErrorCode,
    msg: impl AsRef<str>,
) -> Message {
    let mut err = create_error_message(code, msg.as_ref());
    err.header.id = view.header.id;
    err
}

/// Shared response-builder prefix: echo the request id and query format with an
/// `Ok` error code. The caller supplies the query bytes (cloned or moved).
fn response_header_builder(id: u64, query_format: u16) -> MessageBuilder {
    Message::builder()
        .id(id)
        .query_format(QueryFormat::try_from(query_format).unwrap_or(QueryFormat::RawBinary))
        .error_code(ErrorCode::Ok)
}

/// Serialize `result` into the response body per `body_format` and build the
/// message. Shared by [`create_response`], [`create_response_unstamped`], and the
/// `*_view` builders.
fn finish_response(
    builder: MessageBuilder,
    result: impl serde::Serialize,
    body_format: BodyFormat,
) -> Result<Message, RepeError> {
    let builder = match body_format {
        BodyFormat::Json => builder.body_json(&result)?,
        BodyFormat::Utf8 => {
            let s = serde_json::to_string(&result)?; // convenience: stringify
            builder.body_utf8(&s)
        }
        BodyFormat::RawBinary => {
            // Serialize JSON then treat as bytes; callers wanting true raw should supply bytes.
            let v = serde_json::to_vec(&result)?;
            builder.body_bytes(v).body_format(BodyFormat::RawBinary)
        }
        BodyFormat::Beve => builder.body_beve(&result)?,
    };
    Ok(builder.build())
}