@@ -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
4355impl < 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
133157impl < 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 }
0 commit comments