@@ -22,37 +22,54 @@ pub struct ConvertError {
2222}
2323
2424impl 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+
6689impl 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]
93115pub ( 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 ) ]
114135pub ( crate ) enum FromGodotError {
115136 BadArrayType {
116137 expected : array_inner:: TypeInfo ,
@@ -121,6 +142,14 @@ pub(crate) enum FromGodotError {
121142}
122143
123144impl 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]
169192pub ( crate ) enum FromFfiError {
170193 NullRawGd ,
@@ -178,6 +201,14 @@ pub(crate) enum FromFfiError {
178201}
179202
180203impl 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 ) ]
205229pub ( 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
220241impl 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- }
0 commit comments