poisson_diskus/
error.rs

1use std::fmt;
2
3#[derive(Clone, Debug)]
4/// Errors encountered when sampling coordinates.
5pub enum Error<const D: usize> {
6    /// Invalid input box size to sample coordinates in.
7    ///
8    /// All box size lengths must be positive, real values.
9    InvalidBoxSize { value: f64, box_size: Vec<f64> },
10    /// Invalid input minimum distance between coordinates.
11    ///
12    /// Minimum distance must be a positive, real value. This will also be yielded
13    /// for infinite and not-a-number values.
14    InvalidRmin(f64),
15    /// Generated a coordinate that was outside the box.
16    ///
17    /// This should not happen. Please file an issue if you encounter this.
18    GenCoordOutOfBounds([f64; D]),
19    /// The active list is inconsistent.
20    ///
21    /// This should not happen. Please file an issue if you encounter this.
22    InvalidActiveList,
23}
24
25impl<const D: usize> fmt::Display for Error<D> {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            Error::InvalidBoxSize { value, box_size } => write!(
29                f,
30                "invalid value '{}' in input box size '{:?}': must be a positive number",
31                value, box_size
32            ),
33            Error::InvalidRmin(rmin) => write!(
34                f,
35                "invalid value for input `rmin` ({}): must be a positive number",
36                rmin
37            ),
38            Error::GenCoordOutOfBounds(coord) => write!(
39                f,
40                "generated and used an out-of-box coordinate ({:?}): this should not happen, please file an issue",
41                coord
42            ),
43            Error::InvalidActiveList => write!(
44                f,
45                "active list contains an index which does not lead to a sample: this should not happen, please file an issue"
46            ),
47        }
48    }
49}
50
51impl<const D: usize> std::error::Error for Error<D> {}
52
53#[cfg(test)]
54impl<const D: usize> Error<D> {
55    pub(crate) fn is_invalid_box_size(&self) -> bool {
56        match self {
57            Error::InvalidBoxSize { .. } => true,
58            _ => false,
59        }
60    }
61
62    pub(crate) fn is_invalid_rmin(&self) -> bool {
63        match self {
64            Error::InvalidRmin(_) => true,
65            _ => false,
66        }
67    }
68}