Skip to main content

rill_core/time/
error.rs

1//! # Time-related errors
2//!
3//! This module defines errors that can occur during time and clock operations.
4
5use thiserror::Error;
6
7/// Errors that can occur during time and clock operations
8#[derive(Error, Debug, Clone, PartialEq)]
9pub enum TimeError {
10    /// Hardware error (ALSA, JACK, etc.)
11    #[error("Hardware error: {0}")]
12    Hardware(String),
13
14    /// Invalid sample rate
15    #[error("Invalid sample rate: {0}")]
16    InvalidSampleRate(f32),
17
18    /// Clock not started
19    #[error("Clock not started")]
20    NotStarted,
21
22    /// Clock already started
23    #[error("Clock already started")]
24    AlreadyStarted,
25
26    /// Clock underflow (data not available in time)
27    #[error("Clock underflow")]
28    Underflow,
29
30    /// Clock overflow (data produced too fast)
31    #[error("Clock overflow")]
32    Overflow,
33
34    /// Invalid tempo
35    #[error("Invalid tempo: {0}")]
36    InvalidTempo(f32),
37
38    /// Timing error
39    #[error("Timing error: {0}")]
40    Timing(String),
41}
42
43/// Result type for time operations
44#[allow(dead_code)]
45pub type TimeResult<T> = Result<T, TimeError>;
46
47impl TimeError {
48    /// Create a new hardware error
49    pub fn hardware(msg: impl Into<String>) -> Self {
50        Self::Hardware(msg.into())
51    }
52
53    /// Create a new invalid sample rate error
54    pub fn invalid_sample_rate(rate: f32) -> Self {
55        Self::InvalidSampleRate(rate)
56    }
57
58    /// Create a new invalid tempo error
59    pub fn invalid_tempo(tempo: f32) -> Self {
60        Self::InvalidTempo(tempo)
61    }
62
63    /// Check if the error is recoverable
64    pub fn is_recoverable(&self) -> bool {
65        match self {
66            Self::Underflow | Self::Overflow => true,
67            _ => false,
68        }
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn test_time_error_creation() {
78        let err = TimeError::hardware("ALSA error");
79        assert!(matches!(err, TimeError::Hardware(_)));
80
81        let err = TimeError::invalid_sample_rate(96000.0);
82        assert!(matches!(err, TimeError::InvalidSampleRate(_)));
83    }
84
85    #[test]
86    fn test_time_error_recoverable() {
87        assert!(TimeError::Underflow.is_recoverable());
88        assert!(TimeError::Overflow.is_recoverable());
89        assert!(!TimeError::NotStarted.is_recoverable());
90    }
91}