pitch_estimate/error.rs
1//! Constructor-time configuration errors.
2
3use std::fmt;
4
5/// Largest accepted frame length, in samples.
6///
7/// A frame of this length with `tau_max` equal to `frame_len` uses an FFT
8/// of `2^25` points. Scratch for `f64` is about 1.3 GB plus the FFT
9/// planner's workspace. Larger values are rejected as a configuration
10/// mistake.
11pub const MAX_FRAME_LEN: usize = 1 << 24;
12
13/// Why a detector could not be constructed.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15pub enum ConfigError {
16 /// A size that must be positive was zero.
17 ZeroSize {
18 /// The parameter name: `"frame_len"`, `"tau_max"`, or `"window"`.
19 what: &'static str,
20 },
21 /// A size was outside the range the other sizes allow.
22 OutOfRange {
23 /// The parameter name: `"frame_len"`, `"tau_max"`, or `"window"`.
24 what: &'static str,
25 /// The rejected value.
26 value: usize,
27 /// The smallest accepted value.
28 min: usize,
29 /// The largest accepted value.
30 max: usize,
31 },
32}
33
34impl fmt::Display for ConfigError {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 match self {
37 ConfigError::ZeroSize { what } => write!(f, "{what} must be at least 1, got 0"),
38 ConfigError::OutOfRange {
39 what,
40 value,
41 min,
42 max,
43 } => write!(f, "{what} must lie in {min}..={max}, got {value}"),
44 }
45 }
46}
47
48impl std::error::Error for ConfigError {}