dsfb_gray/adapter.rs
1//! Telemetry adapters bridge real telemetry shapes into [`ResidualSample`].
2//!
3//! DSFB's core observer accepts immutable [`ResidualSample`] values. In real
4//! systems, telemetry often arrives as tracing events, metrics snapshots, or
5//! protocol-specific records. [`TelemetryAdapter`] lets library users keep
6//! those native types while projecting them into the DSFB residual model.
7
8use crate::residual::ResidualSample;
9
10/// Adapter from an application-specific telemetry record into a DSFB sample.
11///
12/// # Example
13///
14/// ```rust
15/// use dsfb_gray::{ResidualSample, ResidualSource, TelemetryAdapter};
16///
17/// struct LatencyPoint {
18/// p95_ns: u64,
19/// baseline_ns: u64,
20/// ts_ns: u64,
21/// }
22///
23/// struct LatencyAdapter;
24///
25/// impl TelemetryAdapter<LatencyPoint> for LatencyAdapter {
26/// fn adapt(&self, input: &LatencyPoint) -> ResidualSample {
27/// ResidualSample {
28/// value: input.p95_ns as f64,
29/// baseline: input.baseline_ns as f64,
30/// timestamp_ns: input.ts_ns,
31/// source: ResidualSource::Latency,
32/// }
33/// }
34/// }
35/// ```
36pub trait TelemetryAdapter<T> {
37 /// Convert one application-specific telemetry record into a DSFB sample.
38 fn adapt(&self, input: &T) -> ResidualSample;
39}
40
41/// Identity adapter for callers that already hold [`ResidualSample`] values.
42#[derive(Debug, Clone, Copy, Default)]
43pub struct IdentityTelemetryAdapter;
44
45impl TelemetryAdapter<ResidualSample> for IdentityTelemetryAdapter {
46 fn adapt(&self, input: &ResidualSample) -> ResidualSample {
47 *input
48 }
49}