#[derive(Debug, Clone, Default)]
#[cfg(test)]
pub struct StreamingConfig {
pub prefix_delta_threshold: u32,
pub prefix_time_threshold: Option<Duration>,
}
#[derive(Debug, Clone)]
#[cfg(test)]
pub struct PrefixDebouncer {
config: StreamingConfig,
delta_count: u32,
last_prefix_time: Option<Instant>,
}
#[cfg(test)]
impl PrefixDebouncer {
#[must_use]
pub const fn new(config: StreamingConfig) -> Self {
Self {
config,
delta_count: 0,
last_prefix_time: None,
}
}
pub const fn reset(&mut self) {
self.delta_count = 0;
self.last_prefix_time = None;
}
pub fn should_show_prefix(&mut self, is_first_delta: bool) -> bool {
if is_first_delta {
self.delta_count = 0;
self.last_prefix_time = Some(Instant::now());
return true;
}
self.delta_count = self.delta_count.saturating_add(1);
if let Some(threshold) = self.config.prefix_time_threshold {
if let Some(last_time) = self.last_prefix_time {
if last_time.elapsed() >= threshold {
self.delta_count = 0;
self.last_prefix_time = Some(Instant::now());
return true;
}
}
}
if self.config.prefix_delta_threshold > 0
&& self.delta_count >= self.config.prefix_delta_threshold
{
self.delta_count = 0;
self.last_prefix_time = Some(Instant::now());
return true;
}
false
}
}
#[cfg(test)]
impl Default for PrefixDebouncer {
fn default() -> Self {
Self::new(StreamingConfig::default())
}
}