Skip to content

Commit ab43498

Browse files
committed
Add ConvertError::new() + ::default()
1 parent 9900076 commit ab43498

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

godot-core/src/builtin/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ impl<T: GodotType> Array<T> {
434434
} else {
435435
Err(FromGodotError::BadArrayType {
436436
expected: target_ty,
437-
got: self_ty,
437+
actual: self_ty,
438438
}
439439
.into_error(self))
440440
}

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

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use crate::builtin::{array_inner, meta::ClassName};
1515
type Cause = Box<dyn Error + Send + Sync>;
1616

1717
/// Represents errors that can occur when converting values from Godot.
18+
///
19+
/// To create user-defined errors, you can use [`ConvertError::default()`] or [`ConvertError::new("message")`][Self::new].
1820
#[derive(Debug)]
1921
pub struct ConvertError {
2022
kind: ErrorKind,
@@ -23,12 +25,21 @@ pub struct ConvertError {
2325
}
2426

2527
impl ConvertError {
26-
// Constructors are private (or hidden) as only the library or its proc-macros should construct this type.
28+
/// Construct with a user-defined message.
29+
///
30+
/// If you don't need a custom message, consider using [`ConvertError::default()`] instead.
31+
pub fn new(user_message: impl Into<String>) -> Self {
32+
Self {
33+
kind: ErrorKind::Custom(Some(user_message.into())),
34+
cause: None,
35+
value_str: None,
36+
}
37+
}
2738

2839
/// Create a new custom error for a conversion.
2940
fn custom() -> Self {
3041
Self {
31-
kind: ErrorKind::Custom,
42+
kind: ErrorKind::Custom(None),
3243
cause: None,
3344
value_str: None,
3445
}
@@ -52,9 +63,10 @@ impl ConvertError {
5263
where
5364
C: Into<Cause>,
5465
{
55-
let mut err = Self::custom();
56-
err.cause = Some(cause.into());
57-
err
66+
Self {
67+
cause: Some(cause.into()),
68+
..Default::default()
69+
}
5870
}
5971

6072
/// Create a new custom error with a rust-error as an underlying cause for the conversion error, and the
@@ -65,10 +77,11 @@ impl ConvertError {
6577
C: Into<Cause>,
6678
V: fmt::Debug,
6779
{
68-
let mut err = Self::custom();
69-
err.cause = Some(cause.into());
70-
err.value_str = Some(format!("{value:?}"));
71-
err
80+
Self {
81+
cause: Some(cause.into()),
82+
value_str: Some(format!("{value:?}")),
83+
..Default::default()
84+
}
7285
}
7386

7487
/// Returns the rust-error that caused this error, if one exists.
@@ -111,12 +124,21 @@ impl Error for ConvertError {
111124
}
112125
}
113126

127+
impl Default for ConvertError {
128+
/// Create a custom error, without any description.
129+
///
130+
/// If you need a custom message, consider using [`ConvertError::new("message")`][Self::new] instead.
131+
fn default() -> Self {
132+
Self::custom()
133+
}
134+
}
135+
114136
#[derive(Eq, PartialEq, Debug)]
115137
pub(crate) enum ErrorKind {
116138
FromGodot(FromGodotError),
117139
FromFfi(FromFfiError),
118140
FromVariant(FromVariantError),
119-
Custom,
141+
Custom(Option<String>),
120142
}
121143

122144
impl ErrorKind {
@@ -125,7 +147,7 @@ impl ErrorKind {
125147
Self::FromGodot(from_godot) => Some(from_godot.description()),
126148
Self::FromVariant(from_variant) => Some(from_variant.description()),
127149
Self::FromFfi(from_ffi) => Some(from_ffi.description()),
128-
Self::Custom => None,
150+
Self::Custom(description) => description.clone(),
129151
}
130152
}
131153
}
@@ -135,7 +157,7 @@ impl ErrorKind {
135157
pub(crate) enum FromGodotError {
136158
BadArrayType {
137159
expected: array_inner::TypeInfo,
138-
got: array_inner::TypeInfo,
160+
actual: array_inner::TypeInfo,
139161
},
140162
/// InvalidEnum is also used by bitfields.
141163
InvalidEnum,
@@ -152,32 +174,32 @@ impl FromGodotError {
152174

153175
fn description(&self) -> String {
154176
match self {
155-
Self::BadArrayType { expected, got } => {
156-
if expected.variant_type() != got.variant_type() {
157-
if expected.is_typed() {
158-
return format!(
177+
Self::BadArrayType { expected, actual } => {
178+
if expected.variant_type() != actual.variant_type() {
179+
return if expected.is_typed() {
180+
format!(
159181
"expected array of type {:?}, got array of type {:?}",
160182
expected.variant_type(),
161-
got.variant_type()
162-
);
183+
actual.variant_type()
184+
)
163185
} else {
164-
return format!(
186+
format!(
165187
"expected untyped array, got array of type {:?}",
166-
got.variant_type()
167-
);
168-
}
188+
actual.variant_type()
189+
)
190+
};
169191
}
170192

171193
assert_ne!(
172194
expected.class_name(),
173-
got.class_name(),
195+
actual.class_name(),
174196
"BadArrayType with expected == got, this is a gdext bug"
175197
);
176198

177199
format!(
178200
"expected array of class {}, got array of class {}",
179201
expected.class_name(),
180-
got.class_name()
202+
actual.class_name()
181203
)
182204
}
183205
Self::InvalidEnum => "invalid engine enum value".into(),

0 commit comments

Comments
 (0)