Skip to content

Commit 056c4c5

Browse files
committed
+fix
1 parent c3232dd commit 056c4c5

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

godot-core/src/meta/error/error_strategies.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,33 @@ use crate::meta::{Element, ToGodot, ref_to_arg};
3232
/// #[func]
3333
/// fn try_parse(&self, input: GString) -> Result<i64, ErrorAsVariant<GString>> {
3434
/// input.to_string().parse::<i64>()
35-
/// .map_err(|e| ErrorAsVariant(GString::from(e.to_string())))
35+
/// .map_err(|e| ErrorAsVariant::new(&e.to_string()))
3636
/// }
3737
/// }
3838
/// ```
3939
///
4040
/// GDScript receives a `Variant`. On success, it contains the `i64` value. On error, it contains the error value.
41-
pub struct ErrorAsVariant<E: ToGodot>(pub E);
41+
pub struct ErrorAsVariant<E: ToGodot> {
42+
/// The error value passed to GDScript as a `Variant`.
43+
pub error: E,
44+
}
45+
46+
impl ErrorAsVariant<GString> {
47+
/// Creates an `ErrorAsVariant` with a string error message.
48+
pub fn new(error_message: impl Into<GString>) -> Self {
49+
Self {
50+
error: error_message.into(),
51+
}
52+
}
53+
}
4254

4355
impl<T: ToGodot, E: ToGodot> ErrorToGodot<T> for ErrorAsVariant<E> {
4456
type Mapped = Variant;
4557

4658
fn result_to_godot(result: Result<&T, &Self>) -> Result<Variant, String> {
4759
match result {
4860
Ok(val) => Ok(val.to_variant()),
49-
Err(e) => Ok(e.0.to_variant()),
61+
Err(e) => Ok(e.error.to_variant()),
5062
}
5163
}
5264
}
@@ -115,7 +127,7 @@ impl<T: Element> ErrorToGodot<T> for ErrorAsEmptyArray {
115127
/// #[func]
116128
/// fn try_parse(&self, input: GString) -> Result<i64, ErrorAsDictionary<GString>> {
117129
/// input.to_string().parse::<i64>()
118-
/// .map_err(|e| ErrorAsDictionary(GString::from(e.to_string())))
130+
/// .map_err(|e| ErrorAsDictionary::new(&e.to_string()))
119131
/// }
120132
/// }
121133
/// ```
@@ -128,7 +140,19 @@ impl<T: Element> ErrorToGodot<T> for ErrorAsEmptyArray {
128140
/// else:
129141
/// print("Parse failed: ", result["err"])
130142
/// ```
131-
pub struct ErrorAsDictionary<E: ToGodot>(pub E);
143+
pub struct ErrorAsDictionary<E: ToGodot> {
144+
/// The error value stored under `"err"` in the returned dictionary.
145+
pub error: E,
146+
}
147+
148+
impl ErrorAsDictionary<GString> {
149+
/// Creates an `ErrorAsDictionary` with a string error message.
150+
pub fn new(error_message: impl Into<GString>) -> Self {
151+
Self {
152+
error: error_message.into(),
153+
}
154+
}
155+
}
132156

133157
impl<T: ToGodot, E: ToGodot> ErrorToGodot<T> for ErrorAsDictionary<E> {
134158
type Mapped = Dictionary<GString, Variant>;
@@ -137,7 +161,7 @@ impl<T: ToGodot, E: ToGodot> ErrorToGodot<T> for ErrorAsDictionary<E> {
137161
let mut d = Dictionary::new();
138162
match result {
139163
Ok(val) => d.set("ok", &val.to_variant()),
140-
Err(e) => d.set("err", &e.0.to_variant()),
164+
Err(e) => d.set("err", &e.error.to_variant()),
141165
}
142166
Ok(d)
143167
}

itest/rust/src/register_tests/func_result_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl FuncResulter {
105105

106106
#[func]
107107
fn variant_err(&self) -> Result<i64, ErrorAsVariant<GString>> {
108-
Err(ErrorAsVariant(GString::from("variant error")))
108+
Err(ErrorAsVariant::new("variant error"))
109109
}
110110

111111
// ------------------------------------------------------------------------------------------------------------------------------------------
@@ -131,7 +131,7 @@ impl FuncResulter {
131131

132132
#[func]
133133
fn dict_err(&self) -> Result<i64, ErrorAsDictionary<GString>> {
134-
Err(ErrorAsDictionary(GString::from("dict error")))
134+
Err(ErrorAsDictionary::new("dict error"))
135135
}
136136
}
137137

0 commit comments

Comments
 (0)