Skip to content

Commit 2984e19

Browse files
committed
- Make ConvertError only appear in meta
- Reorder derives - Remove `#[non_exhaustive]` - Change error message for `WrongClass` - Remove builder pattern from `ConvertError`
1 parent 5bd9984 commit 2984e19

File tree

18 files changed

+127
-115
lines changed

18 files changed

+127
-115
lines changed

godot-codegen/src/class_generator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ fn make_native_structure(
909909
}
910910

911911
impl FromGodot for *mut #class_name {
912-
fn try_from_godot(via: Self::Via) -> Result<Self, crate::builtin::ConvertError> {
912+
fn try_from_godot(via: Self::Via) -> Result<Self, crate::builtin::meta::ConvertError> {
913913
Ok(via as Self)
914914
}
915915
}
@@ -925,7 +925,7 @@ fn make_native_structure(
925925
}
926926

927927
impl FromGodot for *const #class_name {
928-
fn try_from_godot(via: Self::Via) -> Result<Self, crate::builtin::ConvertError> {
928+
fn try_from_godot(via: Self::Via) -> Result<Self, crate::builtin::meta::ConvertError> {
929929
Ok(via as Self)
930930
}
931931
}

godot-codegen/src/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,9 @@ pub fn make_enum_definition(enum_: &Enum) -> TokenStream {
337337
}
338338

339339
impl crate::builtin::meta::FromGodot for #enum_name {
340-
fn try_from_godot(via: Self::Via) -> std::result::Result<Self, crate::builtin::ConvertError> {
340+
fn try_from_godot(via: Self::Via) -> std::result::Result<Self, crate::builtin::meta::ConvertError> {
341341
<Self as crate::obj::EngineEnum>::try_from_ord(via)
342-
.ok_or_else(|| crate::builtin::ConvertError::from(crate::builtin::meta::FromGodotError::InvalidEnum).with_value(via))
342+
.ok_or_else(|| crate::builtin::meta::FromGodotError::InvalidEnum.into_error(via))
343343
}
344344
}
345345

godot-core/src/builtin/array.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use std::marker::PhantomData;
1414
use sys::{ffi_methods, interface_fn, GodotFfi};
1515

1616
use super::meta::{
17-
FromGodot, FromGodotError, FromVariantError, GodotConvert, GodotFfiVariant, GodotType, ToGodot,
17+
ConvertError, FromGodot, FromGodotError, FromVariantError, GodotConvert, GodotFfiVariant,
18+
GodotType, ToGodot,
1819
};
1920

2021
/// Godot's `Array` type.
@@ -346,11 +347,11 @@ impl<T: GodotType> Array<T> {
346347
if self_ty == target_ty {
347348
Ok(self)
348349
} else {
349-
Err(ConvertError::from(FromGodotError::BadArrayType {
350+
Err(FromGodotError::BadArrayType {
350351
expected: target_ty,
351352
got: self_ty,
352-
})
353-
.with_value(self))
353+
}
354+
.into_error(self))
354355
}
355356
}
356357

@@ -801,11 +802,11 @@ impl<T: GodotType> GodotFfiVariant for Array<T> {
801802

802803
fn ffi_from_variant(variant: &Variant) -> Result<Self, ConvertError> {
803804
if variant.get_type() != Self::variant_type() {
804-
return Err(ConvertError::from(FromVariantError::BadType {
805+
return Err(FromVariantError::BadType {
805806
expected: Self::variant_type(),
806807
got: variant.get_type(),
807-
})
808-
.with_value(variant.clone()));
808+
}
809+
.into_error(variant.clone()));
809810
}
810811

811812
let array = unsafe {

godot-core/src/builtin/meta/godot_convert/convert_error.rs

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,54 @@ pub struct ConvertError {
2222
}
2323

2424
impl ConvertError {
25-
/// Create a new error for a conversion.
26-
fn new(kind: ErrorKind) -> Self {
25+
/// Create a new custom error for a conversion.
26+
pub fn new() -> Self {
2727
Self {
28-
kind,
28+
kind: ErrorKind::Custom,
2929
cause: None,
3030
value: None,
3131
}
3232
}
3333

34-
/// Create a new custom error for a conversion.
35-
pub fn custom() -> Self {
36-
Self::new(ErrorKind::Custom)
34+
fn custom() -> Self {
35+
Self {
36+
kind: ErrorKind::Custom,
37+
cause: None,
38+
value: None,
39+
}
3740
}
3841

39-
/// Add a rust-error as an underlying cause for the conversion error.
40-
pub fn with_cause<C: Into<Cause>>(mut self, cause: C) -> Self {
41-
self.cause = Some(cause.into());
42-
self
42+
/// Create a new custom error for a conversion with the value that failed to convert.
43+
pub fn new_with_value<V: fmt::Debug + 'static>(value: V) -> Self {
44+
let mut err = Self::custom();
45+
err.value = Some(Box::new(value));
46+
err
47+
}
48+
49+
/// Create a new custom error with a rust-error as an underlying cause for the conversion error.
50+
pub fn new_with_cause<C: Into<Cause>>(cause: C) -> Self {
51+
let mut err = Self::custom();
52+
err.cause = Some(cause.into());
53+
err
54+
}
55+
56+
/// Create a new custom error with a rust-error as an underlying cause for the conversion error, and the
57+
/// value that failed to convert.
58+
pub fn new_with_cause_value<C: Into<Cause>, V: fmt::Debug + 'static>(
59+
cause: C,
60+
value: V,
61+
) -> Self {
62+
let mut err = Self::custom();
63+
err.cause = Some(cause.into());
64+
err.value = Some(Box::new(value));
65+
err
4366
}
4467

4568
/// Returns the rust-error that caused this error, if one exists.
4669
pub fn cause(&self) -> Option<&(dyn Error + Send + Sync)> {
4770
self.cause.as_deref()
4871
}
4972

50-
/// Add the value that failed to be converted.
51-
pub fn with_value<V: fmt::Debug + 'static>(mut self, value: V) -> Self {
52-
self.value = Some(Box::new(value));
53-
self
54-
}
55-
5673
/// Returns the value that failed to convert, if one exists.
5774
pub fn value(&self) -> Option<&(dyn fmt::Debug)> {
5875
self.value.as_deref()
@@ -63,6 +80,12 @@ impl ConvertError {
6380
}
6481
}
6582

83+
impl Default for ConvertError {
84+
fn default() -> Self {
85+
Self::new()
86+
}
87+
}
88+
6689
impl fmt::Display for ConvertError {
6790
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6891
match (self.description(), self.cause.as_ref()) {
@@ -89,7 +112,6 @@ impl Error for ConvertError {
89112
}
90113

91114
#[derive(Debug, PartialEq, Eq)]
92-
#[non_exhaustive]
93115
pub(crate) enum ErrorKind {
94116
FromGodot(FromGodotError),
95117
FromFfi(FromFfiError),
@@ -109,8 +131,7 @@ impl ErrorKind {
109131
}
110132

111133
/// Conversion failed during a [`FromGodot`](crate::builtin::meta::FromGodot) call.
112-
#[derive(Debug, PartialEq, Eq)]
113-
#[non_exhaustive]
134+
#[derive(Eq, PartialEq, Debug)]
114135
pub(crate) enum FromGodotError {
115136
BadArrayType {
116137
expected: array_inner::TypeInfo,
@@ -121,6 +142,14 @@ pub(crate) enum FromGodotError {
121142
}
122143

123144
impl FromGodotError {
145+
pub fn into_error<V: fmt::Debug + 'static>(self, value: V) -> ConvertError {
146+
ConvertError {
147+
kind: ErrorKind::FromGodot(self),
148+
cause: None,
149+
value: Some(Box::new(value)),
150+
}
151+
}
152+
124153
fn description(&self) -> String {
125154
match self {
126155
Self::BadArrayType { expected, got } => {
@@ -157,14 +186,8 @@ impl FromGodotError {
157186
}
158187
}
159188

160-
impl From<FromGodotError> for ConvertError {
161-
fn from(from_godot: FromGodotError) -> Self {
162-
ConvertError::new(ErrorKind::FromGodot(from_godot))
163-
}
164-
}
165-
166189
/// Conversion failed during a [`GodotType::try_from_ffi()`](crate::builtin::meta::GodotType::try_from_ffi()) call.
167-
#[derive(Debug, PartialEq, Eq)]
190+
#[derive(Eq, PartialEq, Debug)]
168191
#[non_exhaustive]
169192
pub(crate) enum FromFfiError {
170193
NullRawGd,
@@ -178,6 +201,14 @@ pub(crate) enum FromFfiError {
178201
}
179202

180203
impl FromFfiError {
204+
pub fn into_error<V: fmt::Debug + 'static>(self, value: V) -> ConvertError {
205+
ConvertError {
206+
kind: ErrorKind::FromFfi(self),
207+
cause: None,
208+
value: Some(Box::new(value)),
209+
}
210+
}
211+
181212
fn description(&self) -> String {
182213
let target = match self {
183214
Self::NullRawGd => return "`Gd` cannot be null".into(),
@@ -194,14 +225,7 @@ impl FromFfiError {
194225
}
195226
}
196227

197-
impl From<FromFfiError> for ConvertError {
198-
fn from(from_ffi: FromFfiError) -> Self {
199-
ConvertError::new(ErrorKind::FromFfi(from_ffi))
200-
}
201-
}
202-
203-
#[derive(Debug, PartialEq, Eq)]
204-
#[non_exhaustive]
228+
#[derive(Eq, PartialEq, Debug)]
205229
pub(crate) enum FromVariantError {
206230
/// Variant type does not match expected type
207231
BadType {
@@ -212,27 +236,25 @@ pub(crate) enum FromVariantError {
212236
WrongClass {
213237
expected: ClassName,
214238
},
215-
216-
/// Variant value cannot be represented in target type
217-
BadValue,
218239
}
219240

220241
impl FromVariantError {
242+
pub fn into_error<V: fmt::Debug + 'static>(self, value: V) -> ConvertError {
243+
ConvertError {
244+
kind: ErrorKind::FromVariant(self),
245+
cause: None,
246+
value: Some(Box::new(value)),
247+
}
248+
}
249+
221250
fn description(&self) -> String {
222251
match self {
223252
Self::BadType { expected, got } => {
224253
format!("expected Variant of type `{expected:?}` but got Variant of type `{got:?}`")
225254
}
226255
Self::WrongClass { expected } => {
227-
format!("got variant of wrong class, expected class `{expected}`")
256+
format!("expected class `{expected}`, got variant with wrong class")
228257
}
229-
Self::BadValue => "Variant value cannot be represented in target type".into(),
230258
}
231259
}
232260
}
233-
234-
impl From<FromVariantError> for ConvertError {
235-
fn from(from_variant: FromVariantError) -> Self {
236-
ConvertError::new(ErrorKind::FromVariant(from_variant))
237-
}
238-
}

godot-core/src/builtin/meta/godot_convert/impls.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
55
*/
66

7-
use crate::builtin::{
8-
meta::{impl_godot_as_self, FromGodot, GodotConvert, GodotType, ToGodot},
9-
ConvertError, Variant,
7+
use crate::builtin::meta::{
8+
impl_godot_as_self, ConvertError, FromGodot, GodotConvert, GodotType, ToGodot,
109
};
10+
use crate::builtin::Variant;
1111
use godot_ffi as sys;
1212

1313
// The following ToGodot/FromGodot/Convert impls are auto-generated for each engine type, co-located with their definitions:
@@ -137,7 +137,7 @@ macro_rules! impl_godot_scalar {
137137
}
138138

139139
fn try_from_ffi(ffi: Self::Ffi) -> Result<Self, ConvertError> {
140-
Self::try_from(ffi).map_err(|_| ConvertError::from($err).with_value(ffi))
140+
Self::try_from(ffi).map_err(|_| $err.into_error(ffi))
141141
}
142142

143143
$(

godot-core/src/builtin/meta/godot_convert/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ macro_rules! impl_godot_as_self {
116116

117117
impl $crate::builtin::meta::FromGodot for $T {
118118
#[inline]
119-
fn try_from_godot(via: Self::Via) -> Result<Self, $crate::builtin::ConvertError> {
119+
fn try_from_godot(via: Self::Via) -> Result<Self, $crate::builtin::meta::ConvertError> {
120120
Ok(via)
121121
}
122122
}

godot-core/src/builtin/meta/return_marshal.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
55
*/
66

7-
use crate::builtin::ConvertError;
87
use crate::obj::{Gd, GodotClass};
98
use crate::sys;
109

11-
use super::{FromGodot, GodotType};
10+
use super::{ConvertError, FromGodot, GodotType};
1211

1312
/// Specifies how the return type is marshalled in a ptrcall.
1413
#[doc(hidden)]

godot-core/src/builtin/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ pub use vectors::*;
5858

5959
/// Meta-information about variant types, properties and class names.
6060
pub mod meta;
61-
pub use meta::ConvertError;
6261

6362
/// Math-related functions and traits like [`ApproxEq`][math::ApproxEq].
6463
pub mod math;

godot-core/src/builtin/string/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ pub use gstring::*;
1616
pub use node_path::*;
1717
pub use string_name::*;
1818

19-
use super::meta::{FromGodot, GodotConvert, ToGodot};
20-
use super::ConvertError;
19+
use super::meta::{ConvertError, FromGodot, GodotConvert, ToGodot};
2120

2221
impl GodotConvert for &str {
2322
type Via = GString;

godot-core/src/builtin/variant/impls.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ macro_rules! impl_ffi_variant {
3737
fn ffi_from_variant(variant: &Variant) -> Result<Self, ConvertError> {
3838
// Type check -- at the moment, a strict match is required.
3939
if variant.get_type() != Self::variant_type() {
40-
return Err(ConvertError::from(FromVariantError::BadType {
40+
return Err(FromVariantError::BadType {
4141
expected: Self::variant_type(),
4242
got: variant.get_type(),
43-
})
44-
.with_value(variant.clone()));
43+
}
44+
.into_error(variant.clone()));
4545
}
4646

4747
// For 4.0:
@@ -155,7 +155,11 @@ impl GodotFfiVariant for () {
155155
return Ok(());
156156
}
157157

158-
Err(ConvertError::from(FromVariantError::BadValue).with_value(variant.clone()))
158+
Err(FromVariantError::BadType {
159+
expected: VariantType::Nil,
160+
got: variant.get_type(),
161+
}
162+
.into_error(variant.clone()))
159163
}
160164
}
161165

0 commit comments

Comments
 (0)