#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Anchor {
pub period: f64,
pub time: f64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum PulseError {
#[error("invalid pulse anchor at index {index}")]
InvalidAnchor { index: usize },
#[error("pulse timestamp cannot advance at anchor index {index}")]
CannotAdvance { index: usize },
}
pub fn reconstruct(anchors: &[Anchor], mut emit: impl FnMut(f64)) -> Result<(), PulseError> {
if let Some(index) = anchors
.iter()
.position(|a| !a.time.is_finite() || !a.period.is_finite() || a.period <= 0.)
{
return Err(PulseError::InvalidAnchor { index });
}
let mut i = 0;
while let Some(&anchor) = anchors.get(i) {
emit(anchor.time);
let Some(next_anchor) = anchors.get(i + 1) else {
break;
};
let limit = next_anchor.time - 0.5 * anchor.period;
let mut multiple = 1.;
let mut time = anchor.time + anchor.period;
if time < limit && time <= anchor.time {
return Err(PulseError::CannotAdvance { index: i });
}
while time < limit {
emit(time);
multiple += 1.;
let next = anchor.time + multiple * anchor.period;
if next <= time {
return Err(PulseError::CannotAdvance { index: i });
}
time = next;
}
i += 1;
while i < anchors.len()
&& anchors[i].time < anchors[i - 1].time + 0.5 * anchors[i - 1].period
{
i += 1;
}
}
Ok(())
}