1
use crate::ast::ReturnType;
2
use crate::bug;
3
use crate::utils::CombinatoricsError;
4
use std::num::TryFromIntError;
5
use thiserror::Error;
6

            
7
/// An error thrown by an operation on domains.
8
#[non_exhaustive]
9
#[derive(Clone, Debug, PartialEq, Eq, Error)]
10
pub enum DomainOpError {
11
    #[error(
12
        "The operation only supports bounded / finite domains, but was given an unbounded input domain."
13
    )]
14
    Unbounded,
15

            
16
    #[error("The operation only supports integer input domains, but got a {0:?} input domain.")]
17
    NotInteger(ReturnType),
18

            
19
    #[error("The operation was given input domains of the wrong type.")]
20
    WrongType,
21

            
22
    #[error("The operation failed as the input domain was not ground")]
23
    NotGround,
24

            
25
    #[error("Result would exceed the bounds of this integer domain")]
26
    OutOfBounds,
27

            
28
    #[error("Could not enumerate the domain as it is too large")]
29
    TooLarge,
30

            
31
    #[error("The attributes provided are conflicting and impossible")]
32
    ConflictingAttrs,
33
}
34

            
35
impl From<CombinatoricsError> for DomainOpError {
36
1
    fn from(value: CombinatoricsError) -> Self {
37
1
        match value {
38
1
            CombinatoricsError::Overflow => Self::TooLarge,
39
            CombinatoricsError::NotDefined(msg) => {
40
                bug!("Are we passing the right arguments here? ({})", msg)
41
            }
42
        }
43
1
    }
44
}
45

            
46
impl From<TryFromIntError> for DomainOpError {
47
    fn from(_: TryFromIntError) -> Self {
48
        Self::OutOfBounds
49
    }
50
}