repe 3.10.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
use crate::{constants::HEADER_SIZE, error::RepeError, header::Header, message::Message};
use std::io::{Read, Write};

/// Read a full REPE message from a stream implementing `Read`.
/// Blocks until the complete header+query+body are read or an error occurs.
pub fn read_message<R: Read>(r: &mut R) -> Result<Message, RepeError> {
    let mut hdr_buf = [0u8; HEADER_SIZE];
    read_exact(r, &mut hdr_buf)?;
    let header = Header::decode(&hdr_buf)?;
    let mut query = vec![0u8; header.query_length as usize];
    if !query.is_empty() {
        read_exact(r, &mut query)?;
    }
    let mut body = vec![0u8; header.body_length as usize];
    if !body.is_empty() {
        read_exact(r, &mut body)?;
    }
    Message::new(header, query, body)
}

/// Read a full REPE message frame into `buf`, reusing its allocation across
/// calls.
///
/// On success `buf` holds the complete wire frame (header + query + body) and
/// can be parsed with [`MessageView::from_slice`](crate::MessageView::from_slice)
/// to dispatch without the per-request query and body `Vec` allocations that
/// [`read_message`] makes. A server reading many requests on one connection
/// keeps one `buf` and reuses it, so steady-state framing is allocation-free.
///
/// `buf` is cleared first; its capacity is retained, so once it has grown to the
/// largest frame seen no further allocation occurs.
pub fn read_message_into<R: Read>(r: &mut R, buf: &mut Vec<u8>) -> Result<(), RepeError> {
    buf.clear();
    buf.resize(HEADER_SIZE, 0);
    read_exact(r, &mut buf[..HEADER_SIZE])?;
    let header = Header::decode(&buf[..HEADER_SIZE])?;
    let total = HEADER_SIZE + header.query_length as usize + header.body_length as usize;
    buf.resize(total, 0);
    read_exact(r, &mut buf[HEADER_SIZE..total])?;
    Ok(())
}

/// Write a full REPE message to a stream implementing `Write`.
pub fn write_message<W: Write>(w: &mut W, msg: &Message) -> Result<(), RepeError> {
    let header_bytes = msg.header.encode();
    w.write_all(&header_bytes)?;
    if !msg.query.is_empty() {
        w.write_all(&msg.query)?;
    }
    if !msg.body.is_empty() {
        w.write_all(&msg.body)?;
    }
    Ok(())
}

/// Emit a REPE message whose body is produced by `body_writer` rather than
/// owned by a `Message` value.
///
/// The caller supplies the wire body length (`body_len`) up front; this lets
/// the header be written before the body and avoids buffering the body into
/// a `Vec<u8>` first. `header.query_length`, `header.body_length`, and
/// `header.length` are overwritten to reflect `query.len()` and `body_len`,
/// so the caller does not need to set them before calling this function.
///
/// The `body_writer` closure must emit exactly `body_len` bytes; failing to
/// do so produces an unframed message on the wire and the receiving side will
/// either block (short body) or misinterpret subsequent frames (long body).
///
/// `body_writer`'s error type only has to be convertible into [`RepeError`], so
/// a raw `w.write_all(..)` (yielding [`std::io::Error`] → [`RepeError::Io`]) and
/// a streaming encode such as `beve::to_writer_streaming` (yielding
/// [`beve::Error`] → [`RepeError::Beve`]) both work without wrapping, and the
/// failure keeps its original error variant.
///
/// Because REPE frames are length-prefixed, `body_len` has to be known before
/// the body is written. Three patterns supply a serialized body without
/// building a full wire-frame `Vec`:
///
/// * **Reusable scratch buffer** (recommended for a writer sending many
///   frames): serialize the body once into a caller-owned `Vec<u8>` with
///   [`beve::to_vec_into`], which reuses the buffer's allocation and clears it
///   on each call, then pass `scratch.len()` as `body_len` and
///   `|w| w.write_all(&scratch)` as the writer. One encode per message and,
///   once the buffer has grown to fit the largest body, no further allocation.
///   See `examples/beve_streaming_body.rs`.
/// * **Known-length raw body**: when the length is already known (a file
///   chunk, a pre-sized buffer), pass it directly and have `body_writer` emit
///   the bytes with `w.write_all(..)` — no body buffer at all.
/// * **Zero-buffer streaming** (for a body too large to hold in memory):
///   measure the encoded length with [`beve::serialized_size`], then stream the
///   body straight to the sink with [`beve::to_writer_streaming`] inside
///   `body_writer` — the body is never materialized as a `Vec`:
///
///   ```no_run
///   # use repe::{Header, RepeError, write_message_streaming};
///   # use serde::Serialize;
///   # fn frame<W: std::io::Write, T: Serialize>(w: &mut W, header: Header, query: &[u8], value: &T) -> Result<(), RepeError> {
///   let body_len = beve::serialized_size(value)?;
///   write_message_streaming(w, header, query, body_len, |w| {
///       beve::to_writer_streaming(w, value)
///   })
///   # }
///   ```
///
/// The zero-buffer path costs a size pass over the value before the write pass.
/// That pass allocates nothing and moves no bytes (each leaf is an integer add):
/// it is O(1) for a `&[u8]`/`serde_bytes` blob or a `beve::TypedSlice<T>`, but
/// O(payload) for a bare numeric `Vec<T>` or a nested structure, which beve
/// walks element by element. Prefer it only when not holding the whole body at
/// once outweighs that second traversal; otherwise the reusable scratch buffer
/// is a single encode. One caveat on error fidelity: beve owns the sink during a
/// streaming encode and folds any write failure into a `beve::Error`, so a
/// mid-body sink error surfaces as [`RepeError::Beve`] rather than
/// [`RepeError::Io`] — the scratch buffer keeps that distinction, since its
/// `write_all` goes straight through repe.
pub fn write_message_streaming<W, F, E>(
    w: &mut W,
    mut header: Header,
    query: &[u8],
    body_len: u64,
    body_writer: F,
) -> Result<(), RepeError>
where
    W: Write,
    F: FnOnce(&mut W) -> Result<(), E>,
    E: Into<RepeError>,
{
    header.query_length = query.len() as u64;
    header.body_length = body_len;
    header.length = (HEADER_SIZE as u64) + header.query_length + body_len;
    w.write_all(&header.encode())?;
    if !query.is_empty() {
        w.write_all(query)?;
    }
    body_writer(w).map_err(Into::into)?;
    Ok(())
}

/// Frame a REPE message whose body is a contiguous numeric slice, encoded as a
/// BEVE typed array straight to the sink with no intermediate body buffer.
///
/// This is the whole-body streaming fast path for a large numeric payload: the
/// body length is computed in closed form with [`beve::typed_slice_size`] (O(1),
/// no traversal) and the payload is written by [`beve::to_writer_typed_slice`]
/// (a single `write_all` of the slice's bytes on little-endian targets). So
/// framing a multi-MiB `&[f64]` costs a header write plus one bulk write, versus
/// the two element-by-element walks (size, then encode) a serde body would take.
///
/// `header.body_format` is set to [`BodyFormat::Beve`]; `query_length`,
/// `body_length`, and `length` are filled in from `query` and the slice (as in
/// [`write_message_streaming`]). The bytes on the wire are identical to a
/// [`MessageBuilder::body_typed_slice`] message written with [`write_message`];
/// decode them with [`Message::decode_typed_slice`].
///
/// For a complex body use the sibling [`write_message_complex_slice`]. For an
/// owned message, build a [`Message`] with
/// [`body_typed_slice`](crate::message::MessageBuilder::body_typed_slice) /
/// [`body_complex_slice`](crate::message::MessageBuilder::body_complex_slice) and
/// frame it with [`write_message`].
///
/// [`BodyFormat::Beve`]: crate::constants::BodyFormat::Beve
/// [`MessageBuilder::body_typed_slice`]: crate::message::MessageBuilder::body_typed_slice
/// [`MessageBuilder`]: crate::message::MessageBuilder
pub fn write_message_typed_slice<W, T>(
    w: &mut W,
    mut header: Header,
    query: &[u8],
    slice: &[T],
) -> Result<(), RepeError>
where
    W: Write,
    T: beve::BeveTypedSlice,
{
    header.body_format = crate::constants::BodyFormat::Beve as u16;
    let body_len = beve::typed_slice_size(slice);
    write_message_streaming(w, header, query, body_len, |w| {
        beve::to_writer_typed_slice(w, slice)
    })
}

/// Frame a REPE message whose entire body is a complex numeric array, writing the
/// BEVE complex extension array straight to the sink with no intermediate body
/// buffer — the complex counterpart of [`write_message_typed_slice`].
///
/// The body length is computed in closed form with [`beve::complex_slice_size`]
/// (O(1), no traversal) and the payload is written by
/// [`beve::to_writer_complex_slice`] (a single `write_all` of the interleaved
/// `(re, im)` bytes on little-endian targets). So framing a large
/// `&[Complex<f64>]` costs a header write plus one bulk write, versus building and
/// allocating the whole body up front via
/// [`body_complex_slice`](crate::message::MessageBuilder::body_complex_slice).
///
/// `header.body_format` is set to [`BodyFormat::Beve`]; `query_length`,
/// `body_length`, and `length` are filled in from `query` and the slice (as in
/// [`write_message_streaming`]). The bytes on the wire are identical to a
/// [`MessageBuilder::body_complex_slice`] message written with [`write_message`];
/// decode them with [`Message::decode_complex_slice`].
///
/// [`BodyFormat::Beve`]: crate::constants::BodyFormat::Beve
/// [`MessageBuilder::body_complex_slice`]: crate::message::MessageBuilder::body_complex_slice
/// [`Message::decode_complex_slice`]: crate::message::Message::decode_complex_slice
pub fn write_message_complex_slice<W, T>(
    w: &mut W,
    mut header: Header,
    query: &[u8],
    slice: &[beve::Complex<T>],
) -> Result<(), RepeError>
where
    W: Write,
    T: beve::BeveTypedSlice,
{
    header.body_format = crate::constants::BodyFormat::Beve as u16;
    let body_len = beve::complex_slice_size(slice);
    write_message_streaming(w, header, query, body_len, |w| {
        beve::to_writer_complex_slice(w, slice)
    })
}

fn read_exact<R: Read>(r: &mut R, mut buf: &mut [u8]) -> Result<(), RepeError> {
    while !buf.is_empty() {
        let n = r.read(buf)?;
        if n == 0 {
            return Err(RepeError::Io(std::io::Error::from(
                std::io::ErrorKind::UnexpectedEof,
            )));
        }
        let tmp = buf;
        buf = &mut tmp[n..];
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::constants::{BodyFormat, QueryFormat};
    use std::io::Cursor;

    #[test]
    fn read_write_roundtrip() {
        let msg = Message::builder()
            .id(7)
            .query_str("/echo")
            .query_format(QueryFormat::JsonPointer)
            .body_json(&serde_json::json!({"x": 1}))
            .unwrap()
            .build();

        // Write to a buffer
        let mut buf = Vec::new();
        {
            let mut w = Cursor::new(&mut buf);
            write_message(&mut w, &msg).unwrap();
        }

        // Read back
        let mut r = Cursor::new(buf);
        let parsed = read_message(&mut r).unwrap();
        assert_eq!(parsed.header.id, 7);
        assert_eq!(parsed.header.query_format, QueryFormat::JsonPointer as u16);
        assert_eq!(parsed.header.body_format, BodyFormat::Json as u16);
        let v: serde_json::Value = parsed.json_body().unwrap();
        assert_eq!(v["x"], 1);
    }

    #[test]
    fn write_message_streaming_matches_write_message() {
        let body_bytes = vec![0xABu8; 4096];
        let reference = Message::builder()
            .id(99)
            .query_str("/collect/file_chunk")
            .query_format(QueryFormat::JsonPointer)
            .body_bytes(body_bytes.clone())
            .body_format(BodyFormat::Beve)
            .build();

        let mut expected = Vec::new();
        write_message(&mut expected, &reference).unwrap();

        let mut header = Header::new();
        header.id = 99;
        header.query_format = QueryFormat::JsonPointer as u16;
        header.body_format = BodyFormat::Beve as u16;
        let query = b"/collect/file_chunk";

        let mut got = Vec::new();
        write_message_streaming(&mut got, header, query, body_bytes.len() as u64, |w| {
            w.write_all(&body_bytes)
        })
        .unwrap();

        assert_eq!(got, expected);
    }

    #[test]
    fn streaming_beve_body_via_reused_scratch() {
        use serde::{Deserialize, Serialize};

        #[derive(Serialize, Deserialize, Debug, PartialEq)]
        struct SensorFrame {
            id: u64,
            samples: Vec<f64>,
        }

        let query = b"/ingest/frame";
        // One scratch buffer for the writer; reused across every frame.
        let mut scratch = Vec::new();

        let big = SensorFrame {
            id: 1,
            samples: vec![1.25; 4096],
        };

        // Serialize the body once into the caller-owned buffer, then frame it.
        beve::to_vec_into(&mut scratch, &big).unwrap();
        let mut header = Header::new();
        header.id = big.id;
        header.query_format = QueryFormat::JsonPointer as u16;
        header.body_format = BodyFormat::Beve as u16;
        let mut streamed = Vec::new();
        write_message_streaming(&mut streamed, header, query, scratch.len() as u64, |w| {
            w.write_all(&scratch)
        })
        .unwrap();

        // The streamed frame is byte-identical to the fully-buffered Message.
        let reference = Message::builder()
            .id(big.id)
            .query_bytes(query.to_vec())
            .query_format(QueryFormat::JsonPointer)
            .body_bytes(beve::to_vec(&big).unwrap())
            .body_format(BodyFormat::Beve)
            .build()
            .to_vec();
        assert_eq!(streamed, reference);

        // ...and round-trips back to the original value.
        let parsed = Message::from_slice(&streamed).unwrap();
        let decoded: SensorFrame = parsed.beve_body().unwrap();
        assert_eq!(decoded, big);

        // A subsequent smaller body reuses the buffer: `to_vec_into` clears but
        // keeps capacity, so no reallocation once it has grown to the largest
        // body seen.
        let cap_after_big = scratch.capacity();
        let small = SensorFrame {
            id: 2,
            samples: vec![0.0; 16],
        };
        beve::to_vec_into(&mut scratch, &small).unwrap();
        assert!(
            scratch.capacity() <= cap_after_big,
            "smaller body must reuse the scratch allocation, not grow it"
        );
        assert!(scratch.len() < cap_after_big);
    }

    #[test]
    fn streaming_beve_body_zero_buffer_via_serialized_size() {
        use serde::{Deserialize, Serialize};

        #[derive(Serialize, Deserialize, Debug, PartialEq)]
        struct SensorFrame {
            id: u64,
            samples: Vec<f64>,
        }

        let query = b"/ingest/frame";
        let frame = SensorFrame {
            id: 7,
            samples: vec![2.5; 4096],
        };

        // Measure the encoded length up front, then stream the body straight to
        // the sink -- the body is never materialized as a `Vec`.
        let body_len = beve::serialized_size(&frame).unwrap();
        let mut header = Header::new();
        header.id = frame.id;
        header.query_format = QueryFormat::JsonPointer as u16;
        header.body_format = BodyFormat::Beve as u16;
        let mut streamed = Vec::new();
        // The body_writer returns beve::Result directly -- its error type only
        // needs to be Into<RepeError>, so no manual error mapping.
        write_message_streaming(&mut streamed, header, query, body_len, |w| {
            beve::to_writer_streaming(w, &frame)
        })
        .unwrap();

        // The core framing contract: serialized_size must predict exactly what
        // to_writer_streaming emits, so the advertised body_length matches the
        // bytes actually written. A wrong prediction would desync the wire.
        let mut streamed_body = Vec::new();
        beve::to_writer_streaming(&mut streamed_body, &frame).unwrap();
        assert_eq!(body_len, streamed_body.len() as u64);

        let parsed = Message::from_slice(&streamed).unwrap();
        assert_eq!(parsed.header.body_length, body_len);
        assert_eq!(parsed.body, streamed_body);

        // Streaming-encoded bytes round-trip through the normal (non-streaming)
        // decoder back to the original value.
        let decoded: SensorFrame = parsed.beve_body().unwrap();
        assert_eq!(decoded, frame);
    }

    #[test]
    fn streaming_body_writer_error_keeps_repe_error_variant() {
        // A beve error raised inside body_writer surfaces as RepeError::Beve,
        // not a flattened RepeError::Io.
        let mut sink = Vec::new();
        let beve_err = write_message_streaming(&mut sink, Header::new(), b"/x", 3, |_w| {
            Err(beve::Error::msg("encode failed"))
        })
        .unwrap_err();
        assert!(
            matches!(beve_err, RepeError::Beve(_)),
            "expected RepeError::Beve, got {beve_err:?}"
        );

        // And an io::Error keeps RepeError::Io with its ErrorKind intact.
        let mut sink = Vec::new();
        let io_err = write_message_streaming(&mut sink, Header::new(), b"/x", 3, |_w| {
            Err(std::io::Error::from(std::io::ErrorKind::BrokenPipe))
        })
        .unwrap_err();
        match io_err {
            RepeError::Io(e) => assert_eq!(e.kind(), std::io::ErrorKind::BrokenPipe),
            other => panic!("expected RepeError::Io, got {other:?}"),
        }
    }

    #[test]
    fn write_message_streaming_with_empty_query_and_body() {
        let header = Header::new();
        let mut got = Vec::new();
        write_message_streaming(&mut got, header, &[], 0, |w| w.write_all(b"")).unwrap();

        let parsed = Message::from_slice(&got).unwrap();
        assert_eq!(parsed.header.length, HEADER_SIZE as u64);
        assert!(parsed.query.is_empty());
        assert!(parsed.body.is_empty());
    }

    #[test]
    fn read_message_unexpected_eof() {
        let header = Header::new();
        let mut partial = header.encode().to_vec();
        partial.truncate(HEADER_SIZE - 4);
        let mut r = Cursor::new(partial);
        let err = read_message(&mut r).unwrap_err();
        match err {
            RepeError::Io(e) => assert_eq!(e.kind(), std::io::ErrorKind::UnexpectedEof),
            _ => panic!("unexpected err {err:?}"),
        }
    }
}