Skip to main content

wireframe_testing/
echo_server.rs

1//! Echo server utilities for testing client messaging with correlation IDs.
2//!
3//! Provides [`ServerMode`] and [`process_frame`] for building test servers
4//! that either echo envelopes back unchanged or return mismatched correlation
5//! IDs for error testing scenarios.
6
7use wireframe::{
8    app::{Envelope, Packet},
9    correlation::CorrelatableFrame,
10    serializer::{BincodeSerializer, Serializer},
11};
12
13/// Server mode for testing various correlation ID scenarios.
14#[derive(Debug, Clone, Copy, Default)]
15pub enum ServerMode {
16    /// Echo envelopes back with the same correlation ID.
17    #[default]
18    Echo,
19    /// Return envelopes with a different (mismatched) correlation ID.
20    Mismatch,
21}
22
23/// Process a single frame and return the response bytes.
24///
25/// Deserializes an [`Envelope`] from the input bytes, optionally modifies
26/// the correlation ID based on the [`ServerMode`], and re-serializes.
27///
28/// Returns `None` if deserialization or serialization fails.
29#[must_use]
30pub fn process_frame(mode: ServerMode, bytes: &[u8]) -> Option<Vec<u8>> {
31    let (envelope, _): (Envelope, usize) = BincodeSerializer.deserialize(bytes).ok()?;
32
33    let response = match mode {
34        ServerMode::Echo => envelope,
35        ServerMode::Mismatch => {
36            let wrong_id = envelope.correlation_id().map(|id| id.wrapping_add(999));
37            let parts = envelope.into_parts();
38            Envelope::new(parts.id(), wrong_id, parts.into_payload())
39        }
40    };
41
42    BincodeSerializer.serialize(&response).ok()
43}