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 the hop is too long.
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    /// digital-zero 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 =
44    "hop must be greater than zero, no longer than the window, and fit in u32 samples";
45const HOP_TOO_SHORT: &str = "hop rounds to zero samples at this sample rate";
46const INVALID_THRESHOLD: &str = "threshold must be finite, and a relative threshold non-negative";
47const INVALID_LENGTHS: &str = "lengths must satisfy min <= target <= max with a non-zero target";
48const INVALID_RATIO: &str = "placement ratio must be within 0.0..=1.0";
49const INVALID_WEIGHTS: &str =
50    "weights must be finite and non-negative, the per-cut cost and pause reference above zero";
51const INVALID_SCORE_FRAMES: &str =
52    "score rate and hop must be greater than zero, with each frame at least one sample long";
53const INVALID_VALUE: &str =
54    "scores must be within 0.0..=1.0 without zero marks, and levels must be finite";
55const FRAME_COUNT_MISMATCH: &str = "frame count does not cover the audio length";
56
57/// Panic message for planar input whose channel count differs from the layout.
58pub(crate) const PLANAR_CHANNELS: &str = "planar input must have one slice per channel";
59/// Panic message for planar input whose channel slices differ in length.
60pub(crate) const PLANAR_LENGTHS: &str = "planar channel slices must have equal lengths";
61/// Panic message for planar input after a partial interleaved frame.
62pub(crate) const PLANAR_PENDING: &str = "planar input cannot follow a partial interleaved frame";
63
64impl fmt::Display for ConfigError {
65    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66        f.write_str(match self {
67            Self::ZeroSampleRate => ZERO_SAMPLE_RATE,
68            Self::ZeroChannels => ZERO_CHANNELS,
69            Self::SampleRateTooLow => SAMPLE_RATE_TOO_LOW,
70            Self::InvalidFrames => INVALID_FRAMES,
71            Self::HopTooShort => HOP_TOO_SHORT,
72            Self::InvalidThreshold => INVALID_THRESHOLD,
73            Self::InvalidLengths => INVALID_LENGTHS,
74            Self::InvalidRatio => INVALID_RATIO,
75            Self::InvalidWeights => INVALID_WEIGHTS,
76            Self::InvalidScoreFrames => INVALID_SCORE_FRAMES,
77            Self::InvalidValue => INVALID_VALUE,
78            Self::FrameCountMismatch => FRAME_COUNT_MISMATCH,
79        })
80    }
81}
82
83impl core::error::Error for ConfigError {}