6
6
*/
7
7
8
8
use std:: error:: Error ;
9
+ use std:: fmt;
9
10
10
11
use crate :: gen:: classes:: FileAccess ;
11
12
use crate :: global:: Error as GodotError ;
12
- use crate :: obj:: { Gd , NotUniqueError } ;
13
+ use crate :: obj:: Gd ;
13
14
14
15
/// Error that can occur while using `gdext` IO utilities.
15
16
#[ derive( Debug ) ]
16
17
pub struct IoError {
17
18
data : ErrorData ,
18
19
}
19
20
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 {
22
23
match & self . data {
23
24
ErrorData :: Load ( err) => err. fmt ( f) ,
24
25
ErrorData :: Save ( err) => err. fmt ( f) ,
@@ -29,14 +30,12 @@ impl std::fmt::Display for IoError {
29
30
30
31
impl Error for IoError {
31
32
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) ,
38
38
}
39
- None
40
39
}
41
40
}
42
41
@@ -87,23 +86,27 @@ impl IoError {
87
86
88
87
match file_access. try_to_unique ( ) {
89
88
Ok ( gd) => Ok ( gd) ,
90
- Err ( ( _ , err ) ) => Err ( Self {
89
+ Err ( ( _drop , ref_count ) ) => Err ( Self {
91
90
data : ErrorData :: GFile ( GFileError {
92
- kind : GFileErrorKind :: NotUniqueRef ( err ) ,
91
+ kind : GFileErrorKind :: NotUniqueRef { ref_count } ,
93
92
path : path. to_string ( ) ,
94
93
} ) ,
95
94
} ) ,
96
95
}
97
96
}
98
97
}
99
98
99
+ // ----------------------------------------------------------------------------------------------------------------------------------------------
100
+
100
101
#[ derive( Debug ) ]
101
102
enum ErrorData {
102
103
Load ( LoaderError ) ,
103
104
Save ( SaverError ) ,
104
105
GFile ( GFileError ) ,
105
106
}
106
107
108
+ // ----------------------------------------------------------------------------------------------------------------------------------------------
109
+
107
110
#[ derive( Debug ) ]
108
111
struct LoaderError {
109
112
kind : LoaderErrorKind ,
@@ -117,8 +120,10 @@ enum LoaderErrorKind {
117
120
Cast ,
118
121
}
119
122
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 {
122
127
let class = & self . class ;
123
128
let path = & self . path ;
124
129
@@ -135,15 +140,19 @@ impl std::fmt::Display for LoaderError {
135
140
}
136
141
}
137
142
143
+ // ----------------------------------------------------------------------------------------------------------------------------------------------
144
+
138
145
#[ derive( Debug ) ]
139
146
struct SaverError {
140
147
class : String ,
141
148
path : String ,
142
149
godot_error : GodotError ,
143
150
}
144
151
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 {
147
156
let class = & self . class ;
148
157
let path = & self . path ;
149
158
let godot_error = & self . godot_error ;
@@ -152,6 +161,8 @@ impl std::fmt::Display for SaverError {
152
161
}
153
162
}
154
163
164
+ // ----------------------------------------------------------------------------------------------------------------------------------------------
165
+
155
166
#[ derive( Debug ) ]
156
167
struct GFileError {
157
168
kind : GFileErrorKind ,
@@ -160,17 +171,22 @@ struct GFileError {
160
171
161
172
#[ derive( Debug ) ]
162
173
enum GFileErrorKind {
163
- NotUniqueRef ( NotUniqueError ) ,
174
+ NotUniqueRef { ref_count : usize } ,
164
175
NotOpen ,
165
176
}
166
177
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 {
169
182
let path = & self . path ;
170
183
171
184
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
+ )
174
190
}
175
191
GFileErrorKind :: NotOpen => write ! ( f, "access to file '{path}' is not open" ) ,
176
192
}
0 commit comments