use std::time::Instant;
use crate::match_config::MatchResult;
use crate::rx_metadata::{RxStopMetadata, RxStopReason};
#[derive(Debug)]
pub enum RxStopDecision {
Continue,
Stop(RxStopOutcome),
}
#[derive(Debug)]
pub struct RxStopOutcome {
pub meta: RxStopMetadata,
pub matched: bool,
pub match_index: Option<usize>,
}
pub struct RxStopController {
deadline: Option<Instant>,
silence_duration: Option<std::time::Duration>,
silence_deadline: Option<Instant>,
max_bytes: usize,
bytes_observed: usize,
bytes_returned: usize,
matched: bool,
match_index: Option<usize>,
}
impl RxStopController {
pub fn new(
start: Instant,
timeout_ms: Option<u64>,
max_bytes: usize,
no_new_rx_timeout_ms: Option<u64>,
) -> Self {
let deadline = timeout_ms.map(|ms| start + std::time::Duration::from_millis(ms));
let silence_duration = no_new_rx_timeout_ms.map(std::time::Duration::from_millis);
let silence_deadline = silence_duration.map(|d| start + d);
Self {
deadline,
silence_duration,
silence_deadline,
max_bytes,
bytes_observed: 0,
bytes_returned: 0,
matched: false,
match_index: None,
}
}
pub fn check_timeout(&self) -> RxStopDecision {
if let Some(dl) = self.deadline {
if Instant::now() >= dl {
return RxStopDecision::Stop(RxStopOutcome {
meta: RxStopMetadata::timeout(self.bytes_observed)
.with_bytes(self.bytes_observed, self.bytes_returned),
matched: self.matched,
match_index: self.match_index,
});
}
}
RxStopDecision::Continue
}
pub fn check_silence_timeout(&self) -> RxStopDecision {
if let Some(dl) = self.silence_deadline {
if Instant::now() >= dl {
return RxStopDecision::Stop(RxStopOutcome {
meta: RxStopMetadata::no_new_rx_timeout(self.bytes_observed)
.with_bytes(self.bytes_observed, self.bytes_returned),
matched: self.matched,
match_index: self.match_index,
});
}
}
RxStopDecision::Continue
}
pub fn notify_data_received(&mut self) {
if let Some(dur) = self.silence_duration {
self.silence_deadline = Some(Instant::now() + dur);
}
}
pub fn reset_silence_timer(&mut self) {
self.silence_deadline = self.silence_duration.map(|dur| Instant::now() + dur);
}
pub fn push_data(
&mut self,
chunk_len: usize,
buffered_len: usize,
match_result: Option<MatchResult>,
) -> RxStopDecision {
self.bytes_observed += chunk_len;
self.bytes_returned = buffered_len;
if let Some(MatchResult::Found(idx)) = match_result {
self.matched = true;
self.match_index = Some(idx);
return RxStopDecision::Stop(RxStopOutcome {
meta: RxStopMetadata::match_found(self.bytes_observed, self.bytes_returned),
matched: self.matched,
match_index: self.match_index,
});
}
if self.max_bytes > 0 && buffered_len >= self.max_bytes {
let meta = RxStopMetadata::max_buffered_bytes(self.bytes_observed, self.bytes_returned);
return RxStopDecision::Stop(RxStopOutcome {
meta,
matched: self.matched,
match_index: self.match_index,
});
}
RxStopDecision::Continue
}
pub fn connection_closed(&self) -> RxStopOutcome {
RxStopOutcome {
meta: RxStopMetadata::connection_closed(self.bytes_observed, self.bytes_returned),
matched: self.matched,
match_index: self.match_index,
}
}
pub fn cancelled(&self) -> RxStopOutcome {
RxStopOutcome {
meta: RxStopMetadata::cancelled().with_bytes(self.bytes_observed, self.bytes_returned),
matched: self.matched,
match_index: self.match_index,
}
}
pub fn framing_error(&self, _error: crate::framing::FrameDecodeError) -> RxStopOutcome {
RxStopOutcome {
meta: RxStopMetadata::framing_error(self.bytes_observed),
matched: false,
match_index: None,
}
}
pub fn data_complete(&self) -> RxStopOutcome {
RxStopOutcome {
meta: RxStopMetadata::data_complete(self.bytes_observed, self.bytes_returned),
matched: false,
match_index: None,
}
}
pub fn bytes_observed(&self) -> usize {
self.bytes_observed
}
pub fn bytes_returned(&self) -> usize {
self.bytes_returned
}
pub fn matched(&self) -> bool {
self.matched
}
pub fn match_index(&self) -> Option<usize> {
self.match_index
}
pub fn deadline(&self) -> Option<Instant> {
self.deadline
}
pub fn max_bytes(&self) -> usize {
self.max_bytes
}
}
pub fn is_normal_stop(reason: RxStopReason) -> bool {
matches!(
reason,
RxStopReason::DataComplete
| RxStopReason::Timeout
| RxStopReason::MaxBufferedBytes
| RxStopReason::MatchFound
| RxStopReason::NoNewRxTimeout
)
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
#[test]
fn timeout_stops_at_deadline() {
let start = Instant::now();
let ctrl = RxStopController::new(start, Some(0), 1024, None);
let decision = ctrl.check_timeout();
assert!(matches!(decision, RxStopDecision::Stop(_)));
if let RxStopDecision::Stop(outcome) = decision {
assert_eq!(outcome.meta.stop_reason, RxStopReason::Timeout);
assert!(!outcome.matched);
assert!(outcome.match_index.is_none());
}
}
#[test]
fn continue_before_deadline() {
let start = Instant::now();
let ctrl = RxStopController::new(start, Some(5000), 1024, None);
assert!(matches!(ctrl.check_timeout(), RxStopDecision::Continue));
}
#[test]
fn no_timeout_without_deadline() {
let start = Instant::now();
let ctrl = RxStopController::new(start, None, 1024, None);
assert!(matches!(ctrl.check_timeout(), RxStopDecision::Continue));
}
#[test]
fn match_found_stops_immediately() {
let start = Instant::now();
let mut ctrl = RxStopController::new(start, None, 1024, None);
let decision = ctrl.push_data(5, 5, Some(MatchResult::Found(2)));
assert!(matches!(decision, RxStopDecision::Stop(_)));
if let RxStopDecision::Stop(outcome) = decision {
assert_eq!(outcome.meta.stop_reason, RxStopReason::MatchFound);
assert!(outcome.matched);
assert_eq!(outcome.match_index, Some(2));
assert_eq!(outcome.meta.bytes_observed, 5);
assert_eq!(outcome.meta.bytes_returned, 5);
}
}
#[test]
fn no_match_continues() {
let start = Instant::now();
let mut ctrl = RxStopController::new(start, None, 1024, None);
let decision = ctrl.push_data(5, 5, Some(MatchResult::NoMatch));
assert!(matches!(decision, RxStopDecision::Continue));
assert_eq!(ctrl.bytes_observed(), 5);
}
#[test]
fn max_buffered_bytes_stops() {
let start = Instant::now();
let mut ctrl = RxStopController::new(start, None, 10, None);
let decision = ctrl.push_data(10, 10, None);
assert!(matches!(decision, RxStopDecision::Stop(_)));
if let RxStopDecision::Stop(outcome) = decision {
assert_eq!(outcome.meta.stop_reason, RxStopReason::MaxBufferedBytes);
}
}
#[test]
fn match_found_takes_priority_over_max_bytes() {
let start = Instant::now();
let mut ctrl = RxStopController::new(start, None, 5, None);
let decision = ctrl.push_data(10, 10, Some(MatchResult::Found(3)));
assert!(matches!(decision, RxStopDecision::Stop(_)));
if let RxStopDecision::Stop(outcome) = decision {
assert_eq!(outcome.meta.stop_reason, RxStopReason::MatchFound);
}
}
#[test]
fn connection_closed_outcome() {
let start = Instant::now();
let mut ctrl = RxStopController::new(start, None, 1024, None);
ctrl.push_data(100, 80, None);
let outcome = ctrl.connection_closed();
assert_eq!(outcome.meta.stop_reason, RxStopReason::ConnectionClosed);
assert_eq!(outcome.meta.bytes_observed, 100);
assert_eq!(outcome.meta.bytes_returned, 80);
}
#[test]
fn cancelled_outcome() {
let start = Instant::now();
let mut ctrl = RxStopController::new(start, None, 1024, None);
ctrl.push_data(30, 30, None);
let outcome = ctrl.cancelled();
assert_eq!(outcome.meta.stop_reason, RxStopReason::Cancelled);
assert_eq!(outcome.meta.bytes_observed, 30);
}
#[test]
fn framing_error_outcome_not_normal_stop() {
let start = Instant::now();
let ctrl = RxStopController::new(start, None, 1024, None);
let outcome = ctrl.framing_error(crate::framing::FrameDecodeError::SlipInvalidEscape(0x41));
assert_eq!(outcome.meta.stop_reason, RxStopReason::FramingError);
assert!(!outcome.matched);
assert!(!is_normal_stop(RxStopReason::FramingError));
}
#[test]
fn data_complete_outcome() {
let start = Instant::now();
let mut ctrl = RxStopController::new(start, None, 1024, None);
ctrl.push_data(42, 42, None);
let outcome = ctrl.data_complete();
assert_eq!(outcome.meta.stop_reason, RxStopReason::DataComplete);
assert!(!outcome.matched);
}
#[test]
fn is_normal_stop_classifies_correctly() {
assert!(is_normal_stop(RxStopReason::DataComplete));
assert!(is_normal_stop(RxStopReason::Timeout));
assert!(is_normal_stop(RxStopReason::MaxBufferedBytes));
assert!(is_normal_stop(RxStopReason::MatchFound));
assert!(!is_normal_stop(RxStopReason::ConnectionClosed));
assert!(!is_normal_stop(RxStopReason::Cancelled));
}
#[test]
fn push_data_without_matcher_accumulates() {
let start = Instant::now();
let mut ctrl = RxStopController::new(start, None, 1024, None);
let decision = ctrl.push_data(10, 10, None);
assert!(matches!(decision, RxStopDecision::Continue));
assert_eq!(ctrl.bytes_observed(), 10);
let decision = ctrl.push_data(20, 30, None);
assert!(matches!(decision, RxStopDecision::Continue));
assert_eq!(ctrl.bytes_observed(), 30);
assert_eq!(ctrl.bytes_returned(), 30);
}
#[test]
fn timeout_preserves_match_state_from_earlier_data() {
let start = Instant::now();
let mut ctrl = RxStopController::new(start, Some(0), 1024, None);
ctrl.push_data(5, 5, None);
let decision = ctrl.check_timeout();
if let RxStopDecision::Stop(outcome) = decision {
assert_eq!(outcome.meta.bytes_observed, 5);
assert_eq!(outcome.meta.bytes_returned, 5);
assert_eq!(outcome.meta.stop_reason, RxStopReason::Timeout);
} else {
panic!("expected Stop decision for expired deadline");
}
}
#[test]
fn silence_timeout_stops_when_expired() {
let start = Instant::now();
let ctrl = RxStopController::new(start, None, 1024, Some(0));
let decision = ctrl.check_silence_timeout();
assert!(matches!(decision, RxStopDecision::Stop(_)));
if let RxStopDecision::Stop(outcome) = decision {
assert_eq!(outcome.meta.stop_reason, RxStopReason::NoNewRxTimeout);
assert!(!outcome.matched);
assert!(outcome.match_index.is_none());
}
}
#[test]
fn silence_timeout_continues_with_future_deadline() {
let start = Instant::now();
let ctrl = RxStopController::new(start, None, 1024, Some(5000));
assert!(matches!(
ctrl.check_silence_timeout(),
RxStopDecision::Continue
));
}
#[test]
fn silence_timeout_disabled_when_none() {
let start = Instant::now();
let ctrl = RxStopController::new(start, None, 1024, None);
assert!(matches!(
ctrl.check_silence_timeout(),
RxStopDecision::Continue
));
}
#[test]
fn notify_data_received_resets_silence_deadline() {
let start = Instant::now();
let mut ctrl = RxStopController::new(start, None, 1024, Some(5000));
assert!(matches!(
ctrl.check_silence_timeout(),
RxStopDecision::Continue
));
ctrl.notify_data_received();
assert!(matches!(
ctrl.check_silence_timeout(),
RxStopDecision::Continue
));
}
#[test]
fn silence_timeout_is_normal_stop() {
assert!(is_normal_stop(RxStopReason::NoNewRxTimeout));
}
#[test]
fn silence_timeout_with_data_produces_bytes_in_outcome() {
let start = Instant::now();
let mut ctrl = RxStopController::new(start, None, 1024, Some(0));
ctrl.push_data(100, 80, None);
let decision = ctrl.check_silence_timeout();
if let RxStopDecision::Stop(outcome) = decision {
assert_eq!(outcome.meta.stop_reason, RxStopReason::NoNewRxTimeout);
assert_eq!(outcome.meta.bytes_observed, 100);
assert_eq!(outcome.meta.bytes_returned, 80);
} else {
panic!("expected Stop decision");
}
}
#[test]
fn both_timeouts_can_be_set_independently() {
let start = Instant::now();
let ctrl = RxStopController::new(start, Some(10000), 1024, Some(1000));
assert!(matches!(ctrl.check_timeout(), RxStopDecision::Continue));
assert!(matches!(
ctrl.check_silence_timeout(),
RxStopDecision::Continue
));
}
#[test]
fn no_new_rx_timeout_metadata_preserves_bytes() {
let meta = RxStopMetadata::no_new_rx_timeout(42);
assert_eq!(meta.stop_reason, RxStopReason::NoNewRxTimeout);
assert_eq!(meta.bytes_observed, 42);
assert_eq!(meta.bytes_returned, 42);
assert!(!meta.truncated);
}
#[test]
fn no_new_rx_timeout_with_bytes() {
let meta = RxStopMetadata::no_new_rx_timeout(100).with_bytes(100, 80);
assert_eq!(meta.bytes_observed, 100);
assert_eq!(meta.bytes_returned, 80);
assert!(meta.truncated);
}
#[test]
fn notify_data_received_resets_expired_silence_deadline() {
let past = Instant::now() - Duration::from_millis(200);
let mut ctrl = RxStopController::new(past, None, 1024, Some(50));
assert!(matches!(
ctrl.check_silence_timeout(),
RxStopDecision::Stop(_)
));
ctrl.notify_data_received();
assert!(matches!(
ctrl.check_silence_timeout(),
RxStopDecision::Continue
));
}
#[test]
fn matched_getter_reflects_match_state() {
let start = Instant::now();
let mut ctrl = RxStopController::new(start, None, 1024, None);
assert!(!ctrl.matched());
ctrl.push_data(5, 5, Some(MatchResult::Found(2)));
assert!(ctrl.matched());
}
#[test]
fn match_index_getter_reflects_found_index() {
let start = Instant::now();
let mut ctrl = RxStopController::new(start, None, 1024, None);
assert_eq!(ctrl.match_index(), None);
ctrl.push_data(5, 5, Some(MatchResult::Found(3)));
assert_eq!(ctrl.match_index(), Some(3));
}
#[test]
fn max_bytes_getter_returns_configured_limit() {
let start = Instant::now();
let ctrl = RxStopController::new(start, None, 512, None);
assert_eq!(ctrl.max_bytes(), 512);
}
#[test]
fn deadline_getter_returns_configured_deadline() {
let start = Instant::now();
let ctrl = RxStopController::new(start, Some(5000), 1024, None);
assert!(ctrl.deadline().is_some());
let ctrl_no_deadline = RxStopController::new(start, None, 1024, None);
assert!(ctrl_no_deadline.deadline().is_none());
}
}