Skip to main content

conjure_cp_core/representation/
errors.rs

1use super::stored::ReprRuleStored;
2#[allow(unused_imports)]
3use super::types::{ReprDeclLevel, ReprDomainLevel};
4use crate::ast::{DeclarationPtr, DomainPtr, Literal};
5use std::fmt::Debug;
6use thiserror::Error;
7
8/// Errors that can be thrown by [ReprDomainLevel::init]
9#[derive(Debug, Error)]
10pub enum ReprInitError {
11    /// The given domain isn't supported by this representation
12    #[error("domain `{}` is not supported by representation `{}`{}", .0, .1, if .2.is_empty() { String::from("") } else { format!(": {}", .2) })]
13    UnsupportedDomain(DomainPtr, &'static str, String),
14    /// This representation only targets other solver families.
15    #[error("representation `{}` is not applicable to solver family `{}`", .0, .1.as_str())]
16    WrongSolverFamily(&'static str, crate::settings::SolverFamily),
17    /// Can't initialise representation for a different reason
18    #[error(transparent)]
19    Other(#[from] anyhow::Error),
20}
21
22/// Errors that can be thrown by [ReprDomainLevel::instantiate]
23#[derive(Debug, Error)]
24pub enum ReprInstantiateError {
25    /// An instance of this representation already exists for this variable
26    #[error("representation `{}` is already initialised for `{}`", .1, .0.name())]
27    AlreadyExists(DeclarationPtr, &'static str),
28    /// Channelling is disabled and another representation has already been selected.
29    #[error(
30        "cannot initialise representation `{requested}` for `{declaration}` because it already uses `{existing}` and channelling is disabled"
31    )]
32    ConflictingRepresentation {
33        declaration: DeclarationPtr,
34        existing: &'static str,
35        requested: &'static str,
36    },
37    /// The given variable had no domain
38    #[error("declaration `{}` (`{}`) had no domain", .0.name(), .0)]
39    NoDomain(DeclarationPtr),
40    /// The given variable was of an unsupported kind
41    #[error("declaration `{}` (`{}`) had an unsupported kind{}", .0.name(), .0, if .1.is_empty() { String::from("") } else { format!(": {}", .1) })]
42    BadKind(DeclarationPtr, String),
43    /// This variable's domain is different from the one this domain-level representation is for
44    #[error("got `{}: {}`, but this representation is for domain `{}`", .0.name(), .0.domain().map(|d| d.to_string()).unwrap_or(String::from("None")), .1)]
45    BadDomain(DeclarationPtr, DomainPtr),
46    /// Can't instantiate representation for a different reason
47    #[error(transparent)]
48    Other(#[from] anyhow::Error),
49}
50
51/// Either [ReprInitError] or [ReprInstantiateError]
52#[derive(Debug, Error)]
53pub enum ReprError {
54    #[error(transparent)]
55    Init(#[from] ReprInitError),
56    #[error(transparent)]
57    Instantiate(#[from] ReprInstantiateError),
58}
59
60/// Either [ReprInitError] | [ReprInstantiateError] | [ReprSelectError]
61#[derive(Debug, Error)]
62pub enum ReferenceReprError {
63    #[error(transparent)]
64    Init(#[from] ReprInitError),
65    #[error(transparent)]
66    Instantiate(#[from] ReprInstantiateError),
67    #[error(transparent)]
68    Select(#[from] ReprSelectError),
69}
70
71impl From<ReprError> for ReferenceReprError {
72    fn from(e: ReprError) -> Self {
73        match e {
74            ReprError::Init(e) => Self::Init(e),
75            ReprError::Instantiate(e) => Self::Instantiate(e),
76        }
77    }
78}
79
80#[derive(Debug, Error)]
81pub enum ReprDownError {
82    /// The given literal cannot be represented by this representation
83    #[error("unexpected value `{}`{}", .0, if .1.is_empty() { String::from("") } else { format!(": {}", .1) })]
84    BadValue(Literal, String),
85    /// Can't go down for a different reason
86    #[error(transparent)]
87    Other(#[from] anyhow::Error),
88}
89
90#[derive(Debug, Error)]
91pub enum ReprUpError {
92    /// Looking up a representation variable failed
93    #[error("no value found for `{}`", .0.name())]
94    NotFound(DeclarationPtr),
95    /// Lookup succeeded but the value is not what we expected
96    #[error("value `{} = {}` is not allowed by domain {}", .0.name(), .1, .2)]
97    BadDomain(DeclarationPtr, Literal, DomainPtr),
98    /// Can't go up for a different reason
99    #[error(transparent)]
100    Other(#[from] anyhow::Error),
101}
102
103#[derive(Debug, Error)]
104pub enum ReprSelectError {
105    #[error("representation `{}` does not exist for variable `{}`", .1, .0.name())]
106    DoesNotExist(DeclarationPtr, &'static str),
107    #[error("this reference already has representation `{}`", .0.name())]
108    AlreadySelected(&'static dyn ReprRuleStored),
109}