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
//! Generic encode/decode helpers for any [`FrameCodec`] implementation.
//!
//! These functions translate between raw payload bytes and codec-framed wire
//! bytes, allowing test drivers to work transparently with arbitrary codecs.
use io;
use ;
use ;
use FrameCodec;
/// Encode each payload into wire bytes using `codec`.
///
/// For every payload the codec's [`FrameCodec::wrap_payload`] produces a
/// frame, which is then serialized through the codec's encoder. The resulting
/// raw byte vectors are suitable for writing directly to a duplex stream.
///
/// # Errors
///
/// Returns an error if the encoder rejects a frame (e.g. payload too large).
///
/// ```rust
/// use wireframe::codec::examples::HotlineFrameCodec;
/// use wireframe_testing::encode_payloads_with_codec;
///
/// let codec = HotlineFrameCodec::new(4096);
/// let frames = encode_payloads_with_codec(&codec, vec![vec![1, 2, 3]])
/// .expect("encoding should succeed for a valid payload");
/// assert!(!frames.is_empty());
/// ```
/// Decode raw wire bytes into frames using `codec`.
///
/// The byte vector is fed into the codec's decoder until no more complete
/// frames can be extracted. After the main decode loop, `decode_eof` is called
/// to flush any partial frame remaining in the buffer. If the buffer still
/// contains unconsumed bytes after `decode_eof` returns `None`, an error is
/// returned — this catches truncation bugs where the server emits incomplete
/// trailing data. Decoder errors are preserved so callers can inspect any
/// underlying structured codec errors through the `io::Error` source chain.
///
/// # Errors
///
/// Returns an error if the decoder encounters malformed data or if trailing
/// bytes remain that do not form a complete frame.
///
/// ```rust
/// use wireframe::codec::examples::HotlineFrameCodec;
/// use wireframe_testing::{decode_frames_with_codec, encode_payloads_with_codec};
///
/// let codec = HotlineFrameCodec::new(4096);
/// let wire: Vec<u8> = encode_payloads_with_codec(&codec, vec![vec![42]])
/// .expect("encoding should succeed")
/// .into_iter()
/// .flatten()
/// .collect();
/// let frames = decode_frames_with_codec(&codec, wire)
/// .expect("decoding should succeed for well-formed wire bytes");
/// assert_eq!(frames.len(), 1);
/// ```
/// Extract raw payload bytes from a slice of codec frames.
///
/// Calls [`FrameCodec::frame_payload`] on each frame and collects the
/// results into owned byte vectors.
///
/// ```rust
/// use bytes::Bytes;
/// use wireframe::codec::{
/// FrameCodec,
/// examples::{HotlineFrame, HotlineFrameCodec},
/// };
/// use wireframe_testing::extract_payloads;
///
/// let frame = HotlineFrame {
/// transaction_id: 1,
/// payload: Bytes::from_static(b"hi"),
/// };
/// let payloads = extract_payloads::<HotlineFrameCodec>(&[frame]);
/// assert_eq!(payloads, vec![b"hi".to_vec()]);
/// ```