1use std::fmt::Formatter;
2use thiserror::Error;
3
4use crate::model::*;
5
6pub type BindResult<T> = Result<T, BindingError>;
7
8#[derive(Debug)]
9pub struct BindingError {
10 inner: BindingErrorVariant,
11}
12
13impl From<BindingErrorVariant> for BindingError {
14 fn from(x: BindingErrorVariant) -> Self {
15 BindingError { inner: x }
16 }
17}
18
19impl std::fmt::Display for BindingError {
20 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
21 write!(f, "{}", self.inner)
22 }
23}
24
25impl std::error::Error for BindingError {}
26
27#[derive(Error, Debug)]
28pub(crate) enum BindingErrorVariant {
29 #[error("Symbol '{}' already used in the library", name)]
31 SymbolAlreadyUsed { name: Name },
32 #[error("Item '{}' is not part of this library", name)]
33 NotPartOfThisLibrary { name: Name },
34 #[error("'{}'", err)]
36 BadName { err: BadName },
37 #[error("Documentation of '{}' was already defined", symbol_name)]
39 DocAlreadyDefined { symbol_name: Name },
40 #[error("Documentation of '{}' was not defined", symbol_name)]
41 DocNotDefined { symbol_name: Name },
42 #[error(
43 "Documentation of '{}' contains an argument reference to '{}' which is not valid in this context",
44 symbol_name,
45 ref_name
46 )]
47 DocInvalidArgumentContext {
48 symbol_name: String,
49 ref_name: String,
50 },
51 #[error(
52 "Documentation of '{}' references '{}' which does not exist",
53 symbol_name,
54 ref_name
55 )]
56 DocInvalidReference {
57 symbol_name: String,
58 ref_name: String,
59 },
60 #[error("Invalid documentation string")]
62 InvalidDocString,
63 #[error("Class '{}' was already defined", handle.name)]
65 ClassAlreadyDefined { handle: ClassDeclarationHandle },
66 #[error("Constructor for class '{}' was already defined", handle.name)]
67 ConstructorAlreadyDefined { handle: ClassDeclarationHandle },
68 #[error("Destructor for class '{}' was already defined", handle.name)]
69 DestructorAlreadyDefined { handle: ClassDeclarationHandle },
70 #[error("Member '{}' is associated with class '{}' but was added to '{}'", name, declared.name, added_to.name)]
71 ClassMemberWrongAssociatedClass {
72 name: Name,
73 declared: ClassDeclarationHandle,
74 added_to: ClassDeclarationHandle,
75 },
76 #[error("Method name '{}' contains the name of the owning class '{}'", class.name, method_name)]
77 BadMethodName {
78 class: ClassDeclarationHandle,
79 method_name: Name,
80 },
81 #[error("No destructor defined for class '{}', but asking for manual/disposable destruction", handle.name)]
82 NoDestructorForManualDestruction { handle: ClassDeclarationHandle },
83 #[error(
85 "ConstantSet '{}' already contains constant name '{}'",
86 set_name,
87 constant_name
88 )]
89 ConstantNameAlreadyUsed { set_name: Name, constant_name: Name },
90 #[error("Enum '{}' does not contain a variant named '{}'", name, variant_name)]
92 UnknownEnumVariant { name: Name, variant_name: String },
93 #[error(
94 "Enum '{}' already contains a variant with name '{}'",
95 name,
96 variant_name
97 )]
98 DuplicateEnumVariantName { name: Name, variant_name: String },
99 #[error(
100 "Enum '{}' already contains a variant with value '{}'",
101 name,
102 variant_value
103 )]
104 DuplicateEnumVariantValue { name: Name, variant_value: i32 },
105 #[error("Return type of native function '{}' was already defined", func_name)]
107 ReturnTypeAlreadyDefined { func_name: Name },
108 #[error(
109 "Function '{}' already has an error type specified: '{}'",
110 function,
111 error_type
112 )]
113 ErrorTypeAlreadyDefined { function: Name, error_type: Name },
114 #[error(
116 "Symbol '{}' is reserved and cannot be used as an interface method name",
117 name
118 )]
119 InterfaceMethodWithReservedName { name: Name },
120 #[error(
121 "Interface '{}' already has callback with the name '{}'",
122 interface_name,
123 callback_name
124 )]
125 InterfaceDuplicateCallbackName {
126 interface_name: Name,
127 callback_name: Name,
128 },
129 #[error(
130 "Symbol '{}' is reserved and cannot be used as a callback argument name",
131 name
132 )]
133 CallbackMethodArgumentWithReservedName { name: Name },
134 #[error(
135 "Initializer '{}' does not exist within struct '{}'",
136 name,
137 struct_name
138 )]
139 InitializerDoesNotExist {
140 name: &'static str,
141 struct_name: Name,
142 },
143 #[error(
144 "Initializer '{}' within struct '{}' is not parameterless",
145 name,
146 struct_name
147 )]
148 InitializerNotParameterless {
149 name: &'static str,
150 struct_name: Name,
151 },
152
153 #[error("Native struct '{}' was already defined", handle.name)]
155 StructAlreadyDefined { handle: StructDeclarationHandle },
156 #[error(
157 "Initializer field type '{}' doesn't match value '{:?}",
158 field_type,
159 value
160 )]
161 StructInitializerBadValueForType {
162 field_type: String,
163 value: InitializerDefault,
164 },
165 #[error("Initializer contains a default struct field but struct '{}' doesn't have a default initializer", struct_name)]
166 StructInitializerStructFieldWithoutDefaultInitializer { struct_name: String },
167 #[error("Native struct '{}' already contains field with name '{}'", handle.name, field_name)]
168 StructFieldDuplicateName {
169 handle: StructDeclarationHandle,
170 field_name: Name,
171 },
172 #[error(
173 "Struct '{}' already contains an initializer with the name '{}'",
174 struct_name,
175 initializer_name
176 )]
177 StructInitializerDuplicateName {
178 struct_name: Name,
179 initializer_name: Name,
180 },
181 #[error(
182 "Initializer field '{}' doesn't exist within struct '{}",
183 field_name,
184 struct_name
185 )]
186 StructInitializerUnknownField { struct_name: Name, field_name: Name },
187 #[error(
188 "Duplicate initializer field default '{}' in struct '{}",
189 field_name,
190 struct_name
191 )]
192 StructInitializerDuplicateField { struct_name: Name, field_name: Name },
193 #[error(
194 "Struct ({}) initializer {} uses the same arguments as initializer {}",
195 struct_name,
196 this_initializer,
197 other_initializer
198 )]
199 StructDuplicateInitializerArgs {
200 struct_name: Name,
201 this_initializer: Name,
202 other_initializer: Name,
203 },
204}
205
206impl From<BadName> for BindingError {
207 fn from(err: BadName) -> Self {
208 BindingErrorVariant::BadName { err }.into()
209 }
210}