1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#[cfg(feature = "failure-crate")]
use failure::Fail;

#[cfg(not(feature = "failure-crate"))]
use std::fmt::{self, Display};

#[derive(Debug)]
#[cfg_attr(feature = "failure-crate", derive(Fail))]
// We allow the name repitition because this struct will not make sense outside of the crate
// otherwise, and this is exported as part of the library.
#[allow(clippy::module_name_repetitions)]
pub enum PermutationError {
    #[cfg_attr(
        feature = "failure-crate",
        fail(
            display = "Attempted to shuffle {}, where the highest number is {}",
            shuffle, max_shuffle
        )
    )]
    ShuffleOutOfRange { shuffle: u32, max_shuffle: u32 },

    #[cfg_attr(
        feature = "failure-crate",
        fail(display = "Attempted to create a permutation struct with a length less than 1")
    )]
    LengthTooSmall {},
}

#[cfg(not(feature = "failure-crate"))]
impl Display for PermutationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PermutationError::ShuffleOutOfRange {
                shuffle,
                max_shuffle: max,
            } => write!(
                f,
                "Attempted to shuffle {}, where the highest number is {}",
                shuffle, max
            ),
            PermutationError::LengthTooSmall {} => write!(
                f,
                "Attempted to create a permutation struct with a length less than 1"
            ),
        }
    }
}

/// A permutation result, which is simply an alias for any type that could return a permutation
/// error.
pub type PermutationResult<T> = Result<T, PermutationError>;