Skip to main content

silence_split/
errors.rs

1//! Configuration errors and the messages the crate reports.
2
3use core::fmt;
4
5/// A configuration or input that cannot produce an analysis or a plan.
6///
7/// Errors are raised only while building configurations and analyses. Analysis and planning
8/// themselves never fail: constraints that cannot be met are reported as flags on the result.
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum ConfigError {
12    /// The sample rate is zero.
13    ZeroSampleRate,
14    /// The channel count is zero.
15    ZeroChannels,
16    /// The sample rate leaves no room for the 80 Hz detection high-pass filter.
17    SampleRateTooLow,
18    /// The hop is zero, the window is shorter than the hop, or either exceeds its sample limit.
19    InvalidFrames,
20    /// The hop rounds to zero samples at this sample rate.
21    HopTooShort,
22    /// A threshold is not finite, or a relative threshold is negative.
23    InvalidThreshold,
24    /// The lengths are not `0 <= min <= target <= max` with a non-zero target.
25    InvalidLengths,
26    /// A placement ratio is outside `0.0..=1.0`.
27    InvalidRatio,
28    /// A cost weight is negative or not finite, the per-cut cost is not positive, or the pause
29    /// reference is zero.
30    InvalidWeights,
31    /// The score rate or score hop is zero, or a score frame is shorter than one sample.
32    InvalidScoreFrames,
33    /// A score is outside `0.0..=1.0` or not finite, a level is not finite, or scores carry
34    /// signal-free marks.
35    InvalidValue,
36    /// The number of frames does not cover the stated audio length.
37    FrameCountMismatch,
38}
39
40const ZERO_SAMPLE_RATE: &str = "sample rate must be greater than zero";
41const ZERO_CHANNELS: &str = "channel count must be greater than zero";
42const SAMPLE_RATE_TOO_LOW: &str = "sample rate must be above 160 Hz for the 80 Hz high-pass filter";
43const INVALID_FRAMES: &str = "hop must be greater than zero, no longer than the window, and fit in u32 samples; window must fit in u64 samples";
44const HOP_TOO_SHORT: &str = "hop rounds to zero samples at this sample rate";
45const INVALID_THRESHOLD: &str = "threshold must be finite, and a relative threshold non-negative";
46const INVALID_LENGTHS: &str = "lengths must satisfy min <= target <= max with a non-zero target";
47const INVALID_RATIO: &str = "placement ratio must be within 0.0..=1.0";
48const INVALID_WEIGHTS: &str =
49    "weights must be finite and non-negative, the per-cut cost and pause reference above zero";
50const INVALID_SCORE_FRAMES: &str =
51    "score rate and hop must be greater than zero, with each frame at least one sample long";
52const INVALID_VALUE: &str =
53    "scores must be within 0.0..=1.0 without signal-free marks, and levels must be finite";
54const FRAME_COUNT_MISMATCH: &str = "frame count does not cover the audio length";
55
56/// Panic message for planar input whose channel count differs from the layout.
57pub(crate) const PLANAR_CHANNELS: &str = "planar input must have one slice per channel";
58/// Panic message for planar input whose channel slices differ in length.
59pub(crate) const PLANAR_LENGTHS: &str = "planar channel slices must have equal lengths";
60/// Panic message for planar input after a partial interleaved frame.
61pub(crate) const PLANAR_PENDING: &str = "planar input cannot follow a partial interleaved frame";
62
63impl fmt::Display for ConfigError {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        f.write_str(match self {
66            Self::ZeroSampleRate => ZERO_SAMPLE_RATE,
67            Self::ZeroChannels => ZERO_CHANNELS,
68            Self::SampleRateTooLow => SAMPLE_RATE_TOO_LOW,
69            Self::InvalidFrames => INVALID_FRAMES,
70            Self::HopTooShort => HOP_TOO_SHORT,
71            Self::InvalidThreshold => INVALID_THRESHOLD,
72            Self::InvalidLengths => INVALID_LENGTHS,
73            Self::InvalidRatio => INVALID_RATIO,
74            Self::InvalidWeights => INVALID_WEIGHTS,
75            Self::InvalidScoreFrames => INVALID_SCORE_FRAMES,
76            Self::InvalidValue => INVALID_VALUE,
77            Self::FrameCountMismatch => FRAME_COUNT_MISMATCH,
78        })
79    }
80}
81
82impl core::error::Error for ConfigError {}