Skip to main content

suture_sse/
extractor.rs

1use crate::target::Targets;
2
3/// A provider-specific adapter that interprets SSE event payloads, routes JSON
4/// delta fragments into `Targets`, recognises the stream terminator, and
5/// synthesizes the closing SSE bytes from computed repairs.
6pub trait DeltaExtractor: Send {
7    /// Interpret one SSE `data` payload, updating `targets`.
8    fn on_event(&self, data: &[u8], targets: &mut Targets);
9
10    /// True if this payload is the provider's stream terminator.
11    fn is_terminator(&self, data: &[u8]) -> bool;
12
13    /// Build the synthetic SSE bytes that close `repairs` (already filtered to
14    /// safe, non-noop targets). `terminated` indicates the upstream already sent
15    /// its terminator (so we don't duplicate it).
16    fn synthesize(&self, repairs: &[Repair], targets: &Targets, terminated: bool) -> Vec<u8>;
17}
18
19/// A single resolved repair: which target, and the bytes to append to it.
20pub struct Repair {
21    pub kind: crate::target::TargetKind,
22    pub append: Vec<u8>,
23}
24
25/// Escape raw bytes for embedding inside a JSON string literal. The repair tail
26/// is always ASCII structural chars (`"`, `}`, `]`).
27pub fn json_escape(bytes: &[u8]) -> String {
28    let mut s = String::with_capacity(bytes.len() + 2);
29    for &b in bytes {
30        match b {
31            b'"' => s.push_str("\\\""),
32            b'\\' => s.push_str("\\\\"),
33            _ => s.push(b as char),
34        }
35    }
36    s
37}