Skip to main content

dspk_core/time/
range.rs

1use serde::{Deserialize, Serialize};
2use super::rational::RationalTime;
3use crate::error::{DspError, DspResult};
4
5/// A continuous interval of time between start and end.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
7pub struct TimeRange {
8    pub start: RationalTime,
9    pub end: RationalTime,
10}
11
12impl TimeRange {
13    pub fn new(start: RationalTime, end: RationalTime) -> DspResult<Self> {
14        if start > end {
15            return Err(DspError::InvalidConfig(format!(
16                "TimeRange start ({}) cannot be greater than end ({})",
17                start, end
18            )));
19        }
20        Ok(Self { start, end })
21    }
22
23    pub fn contains(&self, time: RationalTime) -> bool {
24        time >= self.start && time <= self.end
25    }
26}