66 */
77
88use std:: error:: Error ;
9+ use std:: fmt;
910
1011use crate :: gen:: classes:: FileAccess ;
1112use crate :: global:: Error as GodotError ;
12- use crate :: obj:: { Gd , NotUniqueError } ;
13+ use crate :: obj:: Gd ;
1314
1415/// Error that can occur while using `gdext` IO utilities.
1516#[ derive( Debug ) ]
1617pub struct IoError {
1718 data : ErrorData ,
1819}
1920
20- impl std :: fmt:: Display for IoError {
21- fn fmt ( & self , f : & mut std :: fmt:: Formatter < ' _ > ) -> std :: fmt:: Result {
21+ impl fmt:: Display for IoError {
22+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2223 match & self . data {
2324 ErrorData :: Load ( err) => err. fmt ( f) ,
2425 ErrorData :: Save ( err) => err. fmt ( f) ,
@@ -29,14 +30,12 @@ impl std::fmt::Display for IoError {
2930
3031impl Error for IoError {
3132 fn source ( & self ) -> Option < & ( dyn Error + ' static ) > {
32- if let ErrorData :: GFile ( GFileError {
33- kind : GFileErrorKind :: NotUniqueRef ( err) ,
34- ..
35- } ) = & self . data
36- {
37- return Some ( err) ;
33+ // Note: inner types are not public, but the dyn trait can be used.
34+ match & self . data {
35+ ErrorData :: Load ( err) => Some ( err) ,
36+ ErrorData :: Save ( err) => Some ( err) ,
37+ ErrorData :: GFile ( err) => Some ( err) ,
3838 }
39- None
4039 }
4140}
4241
@@ -87,23 +86,27 @@ impl IoError {
8786
8887 match file_access. try_to_unique ( ) {
8988 Ok ( gd) => Ok ( gd) ,
90- Err ( ( _ , err ) ) => Err ( Self {
89+ Err ( ( _drop , ref_count ) ) => Err ( Self {
9190 data : ErrorData :: GFile ( GFileError {
92- kind : GFileErrorKind :: NotUniqueRef ( err ) ,
91+ kind : GFileErrorKind :: NotUniqueRef { ref_count } ,
9392 path : path. to_string ( ) ,
9493 } ) ,
9594 } ) ,
9695 }
9796 }
9897}
9998
99+ // ----------------------------------------------------------------------------------------------------------------------------------------------
100+
100101#[ derive( Debug ) ]
101102enum ErrorData {
102103 Load ( LoaderError ) ,
103104 Save ( SaverError ) ,
104105 GFile ( GFileError ) ,
105106}
106107
108+ // ----------------------------------------------------------------------------------------------------------------------------------------------
109+
107110#[ derive( Debug ) ]
108111struct LoaderError {
109112 kind : LoaderErrorKind ,
@@ -117,8 +120,10 @@ enum LoaderErrorKind {
117120 Cast ,
118121}
119122
120- impl std:: fmt:: Display for LoaderError {
121- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
123+ impl Error for LoaderError { }
124+
125+ impl fmt:: Display for LoaderError {
126+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
122127 let class = & self . class ;
123128 let path = & self . path ;
124129
@@ -135,15 +140,19 @@ impl std::fmt::Display for LoaderError {
135140 }
136141}
137142
143+ // ----------------------------------------------------------------------------------------------------------------------------------------------
144+
138145#[ derive( Debug ) ]
139146struct SaverError {
140147 class : String ,
141148 path : String ,
142149 godot_error : GodotError ,
143150}
144151
145- impl std:: fmt:: Display for SaverError {
146- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
152+ impl Error for SaverError { }
153+
154+ impl fmt:: Display for SaverError {
155+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
147156 let class = & self . class ;
148157 let path = & self . path ;
149158 let godot_error = & self . godot_error ;
@@ -152,6 +161,8 @@ impl std::fmt::Display for SaverError {
152161 }
153162}
154163
164+ // ----------------------------------------------------------------------------------------------------------------------------------------------
165+
155166#[ derive( Debug ) ]
156167struct GFileError {
157168 kind : GFileErrorKind ,
@@ -160,17 +171,22 @@ struct GFileError {
160171
161172#[ derive( Debug ) ]
162173enum GFileErrorKind {
163- NotUniqueRef ( NotUniqueError ) ,
174+ NotUniqueRef { ref_count : usize } ,
164175 NotOpen ,
165176}
166177
167- impl std:: fmt:: Display for GFileError {
168- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
178+ impl Error for GFileError { }
179+
180+ impl fmt:: Display for GFileError {
181+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
169182 let path = & self . path ;
170183
171184 match & self . kind {
172- GFileErrorKind :: NotUniqueRef ( err) => {
173- write ! ( f, "access to file '{path}' is not unique: '{err}'" )
185+ GFileErrorKind :: NotUniqueRef { ref_count } => {
186+ write ! (
187+ f,
188+ "Gd<FileAccess> for '{path}' is not unique (ref-count {ref_count})"
189+ )
174190 }
175191 GFileErrorKind :: NotOpen => write ! ( f, "access to file '{path}' is not open" ) ,
176192 }
0 commit comments