Skip to main content

sim_lib_pitch_ratio/
error.rs

1//! Error types for exact ratio work.
2
3use crate::MAX_PRIME_LIMIT;
4
5/// Error raised by pitch-ratio validation, factoring, ranking, or approximation.
6#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
7pub enum PitchRatioError {
8    /// Ratios must have positive numerator and denominator.
9    #[error("pitch ratios require positive numerator and denominator")]
10    NonPositiveRatio,
11    /// A checked integer operation overflowed.
12    #[error("pitch ratio integer operation overflowed")]
13    Overflow,
14    /// Prime-limit factorization must be explicitly bounded.
15    #[error("factorization requires a bounded prime limit")]
16    UnboundedFactorization,
17    /// The requested prime limit is outside the supported bounded domain.
18    #[error("prime limit {0} is outside 2..={MAX_PRIME_LIMIT}")]
19    InvalidPrimeLimit(u32),
20    /// A factor remains above the configured prime limit.
21    #[error(
22        "ratio has factors above prime limit {prime_limit}: remaining {remaining_numerator}/{remaining_denominator}"
23    )]
24    PrimeLimitExceeded {
25        /// Numerator remainder after bounded factoring.
26        remaining_numerator: u64,
27        /// Denominator remainder after bounded factoring.
28        remaining_denominator: u64,
29        /// Configured prime limit.
30        prime_limit: u32,
31    },
32    /// A signed exponent exceeded the represented bound.
33    #[error("prime exponent exceeded bounded rank domain")]
34    ExponentOverflow,
35    /// A factor vector is malformed.
36    #[error("factor vector primes and exponents do not match")]
37    InvalidFactorVector,
38    /// A rank/unrank request exceeded the finite exponent domain.
39    #[error("factor exponent {exponent} is outside {min}..={max}")]
40    RankExponentOutOfRange {
41        /// Observed exponent.
42        exponent: i16,
43        /// Minimum supported exponent.
44        min: i16,
45        /// Maximum supported exponent.
46        max: i16,
47    },
48    /// Discrete mixed-radix ranking failed.
49    #[error("discrete rank error: {0}")]
50    DiscreteRank(String),
51    /// A chord operation requires at least one ratio.
52    #[error("ratio chord requires at least one ratio")]
53    EmptyChord,
54    /// A chord root index did not identify a ratio in the chord.
55    #[error("ratio chord root index {root_index} is outside chord length {len}")]
56    InvalidRootIndex {
57        /// Requested root index.
58        root_index: usize,
59        /// Chord length.
60        len: usize,
61    },
62    /// A generalized mean exponent must be finite and non-zero.
63    #[error("generalized mean exponent must be finite and non-zero")]
64    InvalidMeanExponent,
65}