Skip to main content

sightloom_analysis/
config.rs

1//! Zone analytics configuration.
2
3use crate::{AnalyticsError, AnchorPolicy};
4use sightloom_core::ClassId;
5
6/// Configuration for [`crate::ZoneAnalytics`].
7#[derive(Clone, Copy, Debug, PartialEq)]
8pub struct ZoneAnalyticsConfig {
9    /// Which box anchor is tested for membership.
10    pub anchor: AnchorPolicy,
11    /// Consecutive inside samples required to confirm enter/dwell start.
12    pub enter_hysteresis: u32,
13    /// Consecutive outside samples required to confirm exit/dwell end.
14    pub exit_hysteresis: u32,
15    /// Missed frames tolerated before counting as outside.
16    pub missed_frame_tolerance: u32,
17    /// Optional class filter; `None` accepts all classes.
18    pub class_filter: Option<ClassId>,
19    /// Debounce dwell start in nanoseconds after enter confirmed.
20    pub dwell_start_debounce_ns: i64,
21}
22
23impl Default for ZoneAnalyticsConfig {
24    fn default() -> Self {
25        Self {
26            anchor: AnchorPolicy::Center,
27            enter_hysteresis: 1,
28            exit_hysteresis: 1,
29            missed_frame_tolerance: 0,
30            class_filter: None,
31            dwell_start_debounce_ns: 0,
32        }
33    }
34}
35
36impl ZoneAnalyticsConfig {
37    /// Validates hysteresis and debounce fields.
38    ///
39    /// # Errors
40    ///
41    /// Returns [`AnalyticsError::InvalidConfig`] when hysteresis is zero.
42    pub fn validate(self) -> Result<Self, AnalyticsError> {
43        if self.enter_hysteresis == 0 || self.exit_hysteresis == 0 {
44            return Err(AnalyticsError::InvalidConfig);
45        }
46        if self.dwell_start_debounce_ns < 0 {
47            return Err(AnalyticsError::InvalidConfig);
48        }
49        Ok(self)
50    }
51}