xcsp3_rust/errors/
xcsp3error.rs

1/*=============================================================================
2* parser for CSP instances represented in XCSP3 Format
3*
4* Copyright (c) 2023 xcsp.org (contact @ xcsp.org)
5*
6* Permission is hereby granted, free of charge, to any person obtaining a copy
7* of this software and associated documentation files (the "Software"), to deal
8* in the Software without restriction, including without limitation the rights
9* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10* copies of the Software, and to permit persons to whom the Software is
11* furnished to do so, subject to the following conditions:
12*
13* The above copyright notice and this permission notice shall be included in
14* all copies or substantial portions of the Software.
15*
16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22* THE SOFTWARE.
23*=============================================================================
24*/
25
26/*
27* <p>@project_name: xcsp3-rust
28* </p>
29* <p>@author: luhan zhen
30* </p>
31* <p>@date:  2023/7/12 11:56
32* </p>
33* <p>@email: zhenlh20@mails.jlu.edu.cn
34* </p>
35* <p>@version: 1.0
36* </p>
37 * <p>@description: 1.0
38* </p>
39 */
40
41pub mod xcsp3_core {
42    use crate::errors::parse_constraint_error::xcsp3_core::ParseConstraintError;
43    use crate::errors::parse_domain_error::xcsp3_core::ParseDomainError;
44    use crate::errors::parse_objectives_error::xcsp3_core::ParseObjectivesError;
45    use crate::errors::parse_variable_error::ParseVariableError;
46    use quick_xml::DeError;
47    use std::fmt::{Display, Formatter};
48
49    #[derive(Debug, Clone)]
50    pub enum Xcsp3Error {
51        ParseDomainError(ParseDomainError),
52        ParseVariableError(ParseVariableError),
53        ParseConstraintError(ParseConstraintError),
54        ParseObjectivesError(ParseObjectivesError),
55        ReadXmlError(DeError),
56    }
57
58    impl Display for Xcsp3Error {
59        fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
60            write!(
61                f,
62                "{}",
63                match self {
64                    Xcsp3Error::ParseDomainError(e) => e.to_string(),
65                    Xcsp3Error::ParseVariableError(e) => e.to_string(),
66                    Xcsp3Error::ParseConstraintError(e) => e.to_string(),
67                    Xcsp3Error::ParseObjectivesError(e) => e.to_string(),
68                    Xcsp3Error::ReadXmlError(e) => e.to_string(),
69                }
70            )
71        }
72    }
73
74    /// error factory
75    impl Xcsp3Error {
76        pub fn get_read_xml_error(err: DeError) -> Xcsp3Error {
77            Xcsp3Error::ReadXmlError(err)
78        }
79        pub fn get_constraint_slide_error(s: &str) -> Xcsp3Error {
80            Xcsp3Error::ParseConstraintError(ParseConstraintError::get_slide_error(s))
81        }
82        pub fn get_objective_scope_error(s: &str) -> Xcsp3Error {
83            Xcsp3Error::ParseObjectivesError(ParseObjectivesError::get_scope_error(s))
84        }
85
86        pub fn get_objective_coeffs_error(s: &str) -> Xcsp3Error {
87            Xcsp3Error::ParseObjectivesError(ParseObjectivesError::get_coeffs_error(s))
88        }
89
90        pub fn get_objective_type_error(s: &str) -> Xcsp3Error {
91            Xcsp3Error::ParseObjectivesError(ParseObjectivesError::get_type_error(s))
92        }
93
94        pub fn get_objective_target_error(s: &str) -> Xcsp3Error {
95            Xcsp3Error::ParseObjectivesError(ParseObjectivesError::get_target_error(s))
96        }
97
98        pub fn get_constraint_element_error(s: &str) -> Xcsp3Error {
99            Xcsp3Error::ParseConstraintError(ParseConstraintError::get_element_error(s))
100        }
101        pub fn get_constraint_count_error(s: &str) -> Xcsp3Error {
102            Xcsp3Error::ParseConstraintError(ParseConstraintError::get_count_error(s))
103        }
104
105        pub fn get_constraint_no_overlap_error(s: &str) -> Xcsp3Error {
106            Xcsp3Error::ParseConstraintError(ParseConstraintError::get_no_overlap_error(s))
107        }
108
109        pub fn get_constraint_cumulative_error(s: &str) -> Xcsp3Error {
110            Xcsp3Error::ParseConstraintError(ParseConstraintError::get_cumulative_error(s))
111        }
112
113        pub fn get_constraint_cardinality_error(s: &str) -> Xcsp3Error {
114            Xcsp3Error::ParseConstraintError(ParseConstraintError::get_cardinality_error(s))
115        }
116        pub fn get_constraint_sum_error(s: &str) -> Xcsp3Error {
117            Xcsp3Error::ParseConstraintError(ParseConstraintError::get_sum_error(s))
118        }
119        pub fn get_constraint_group_error(s: &str) -> Xcsp3Error {
120            Xcsp3Error::ParseConstraintError(ParseConstraintError::get_group_error(s))
121        }
122        pub fn get_constraint_expression_error(s: &str) -> Xcsp3Error {
123            Xcsp3Error::ParseConstraintError(ParseConstraintError::get_expression_error(s))
124        }
125
126        pub fn get_constraint_channel_error(s: &str) -> Xcsp3Error {
127            Xcsp3Error::ParseConstraintError(ParseConstraintError::get_channel_error(s))
128        }
129        pub fn get_constraint_extension_error(s: &str) -> Xcsp3Error {
130            Xcsp3Error::ParseConstraintError(ParseConstraintError::get_extension_error(s))
131        }
132
133        pub fn get_constraint_scope_not_found_error(s: &str) -> Xcsp3Error {
134            Xcsp3Error::ParseConstraintError(ParseConstraintError::get_scope_not_found_error(s))
135        }
136
137        pub fn get_constraint_regular_transitions_error(s: &str) -> Xcsp3Error {
138            Xcsp3Error::ParseConstraintError(ParseConstraintError::get_regular_transitions_error(s))
139        }
140
141        pub fn get_constraint_list_of_values_error(s: &str) -> Xcsp3Error {
142            Xcsp3Error::ParseConstraintError(ParseConstraintError::get_list_of_values_error(s))
143        }
144        pub fn get_domain_integer_error(s: &str) -> Xcsp3Error {
145            Xcsp3Error::ParseDomainError(ParseDomainError::get_integer_error(s))
146        }
147
148        pub fn get_domain_interval_error(s: &str) -> Xcsp3Error {
149            Xcsp3Error::ParseDomainError(ParseDomainError::get_interval_error(s))
150        }
151
152        pub fn get_domain_for_error(s: &str) -> Xcsp3Error {
153            Xcsp3Error::ParseDomainError(ParseDomainError::get_domain_for_error(s))
154        }
155
156        pub fn get_variable_not_found_error(s: &str) -> Xcsp3Error {
157            Xcsp3Error::ParseVariableError(ParseVariableError::get_not_found_error(s))
158        }
159
160        pub fn get_variable_size_invalid_error(s: &str) -> Xcsp3Error {
161            Xcsp3Error::ParseVariableError(ParseVariableError::get_size_invalid_error(s))
162        }
163    }
164}