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
//! Helpers for adapting streaming response frames into domain items.
//!
//! This module layers on top of [`super::ResponseStream`] and other compatible
//! streams without changing transport semantics, terminator handling, or the
//! exclusive client borrow held by the underlying response stream.
use ;
use Stream;
use ClientError;
/// Extension methods for adapting streaming response frames into typed items.
///
/// The helper keeps the underlying transport semantics intact:
///
/// - yielded items preserve their original order;
/// - `Ok(None)` mapper results skip control frames;
/// - mapper failures stop the stream immediately;
/// - underlying [`ClientError`] values are forwarded unchanged; and
/// - termination still occurs when the wrapped stream returns `None`.
///
/// This is useful when a protocol multiplexes data frames with notices,
/// progress updates, or other control packets that should not appear in the
/// final consumer-facing stream.
///
/// # Examples
///
/// ```rust,no_run
/// use std::net::SocketAddr;
///
/// use futures::TryStreamExt;
/// use wireframe::{
/// app::{Packet, PacketParts},
/// client::{ClientError, StreamingResponseExt, WireframeClient},
/// correlation::CorrelatableFrame,
/// };
///
/// #[derive(bincode::BorrowDecode, bincode::Encode, Debug)]
/// struct MyEnvelope {
/// id: u32,
/// correlation_id: Option<u64>,
/// payload: Vec<u8>,
/// }
///
/// impl CorrelatableFrame for MyEnvelope {
/// fn correlation_id(&self) -> Option<u64> { self.correlation_id }
///
/// fn set_correlation_id(&mut self, cid: Option<u64>) { self.correlation_id = cid; }
/// }
///
/// impl Packet for MyEnvelope {
/// fn id(&self) -> u32 { self.id }
///
/// fn into_parts(self) -> PacketParts {
/// PacketParts::new(self.id, self.correlation_id, self.payload)
/// }
///
/// fn from_parts(parts: PacketParts) -> Self {
/// Self {
/// id: parts.id(),
/// correlation_id: parts.correlation_id(),
/// payload: parts.into_payload(),
/// }
/// }
///
/// fn is_stream_terminator(&self) -> bool { self.id == 0 }
/// }
///
/// #[derive(Debug, PartialEq, Eq)]
/// struct Row(Vec<u8>);
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), ClientError> {
/// let addr: SocketAddr = "127.0.0.1:9000".parse().expect("valid socket address");
/// let mut client = WireframeClient::builder().connect(addr).await?;
///
/// fn map_frame(frame: MyEnvelope) -> Result<Option<Row>, ClientError> {
/// match frame.id {
/// 1 => Ok(Some(Row(frame.payload))),
/// 2 => Ok(None),
/// other => Err(ClientError::from(std::io::Error::new(
/// std::io::ErrorKind::InvalidData,
/// format!("unexpected frame id {other}"),
/// ))),
/// }
/// }
///
/// let request = MyEnvelope {
/// id: 1,
/// correlation_id: None,
/// payload: vec![],
/// };
///
/// let rows: Vec<Row> = client
/// .call_streaming::<MyEnvelope>(request)
/// .await?
/// .typed_with(map_frame)
/// .try_collect()
/// .await?;
///
/// assert!(!rows.is_empty());
/// # Ok(())
/// # }
/// ```
/// Stream adapter that maps protocol frames into domain items.
///
/// Construct this via [`StreamingResponseExt::typed_with`].
///
/// # Examples
///
/// ```rust
/// use futures::{StreamExt, TryStreamExt, stream};
/// use wireframe::client::StreamingResponseExt;
///
/// # async fn demo() -> Result<(), wireframe::client::ClientError> {
/// let items: Vec<u8> = stream::iter(vec![Ok::<u8, _>(1), Ok(2), Ok(3)])
/// .typed_with(|frame| {
/// if frame % 2 == 0 {
/// Ok(None)
/// } else {
/// Ok(Some(frame))
/// }
/// })
/// .try_collect()
/// .await?;
///
/// assert_eq!(items, vec![1, 3]);
/// # Ok(())
/// # }
/// ```