@@ -16,9 +16,9 @@ pub struct File {
16
16
}
17
17
18
18
#[ derive( Clone ) ]
19
- pub struct FileAttr {
20
- size : u64 ,
21
- is_dir : bool ,
19
+ pub enum FileAttr {
20
+ Dir ,
21
+ File { size : u64 } ,
22
22
}
23
23
24
24
pub struct ReadDir ( !) ;
@@ -54,23 +54,24 @@ pub struct DirBuilder {}
54
54
impl FileAttr {
55
55
/// Creates a FileAttr by getting data from an opened file.
56
56
fn from_fd ( fd : * mut vex_sdk:: FIL ) -> io:: Result < Self > {
57
- let size = unsafe { vex_sdk:: vexFileSize ( fd) } ;
58
-
59
- if size >= 0 {
60
- Ok ( Self { size : size as u64 , is_dir : false } )
57
+ // `vexFileSize` returns -1 upon error, so u64::try_from will fail on error.
58
+ if let Some ( size) = u64:: try_from ( unsafe { vex_sdk:: vexFileSize ( fd) } ) {
59
+ Ok ( Self :: File { size } )
61
60
} else {
62
61
Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , "Failed to get file size" ) )
63
62
}
64
63
}
65
64
66
65
fn from_path ( path : & Path ) -> io:: Result < Self > {
66
+ // vexFileStatus returns 3 if the given path is a directory.
67
+ const FILE_STATUS_DIR : i32 = 3 ;
68
+
67
69
run_path_with_cstr ( path, & |c_path| {
68
70
let file_type = unsafe { vex_sdk:: vexFileStatus ( c_path. as_ptr ( ) ) } ;
69
- let is_dir = file_type == 3 ;
70
71
71
72
// We can't get the size if its a directory because we cant open it as a file
72
- if is_dir {
73
- Ok ( Self { size : 0 , is_dir : true } )
73
+ if file_type == FILE_STATUS_DIR {
74
+ Ok ( Self :: Dir )
74
75
} else {
75
76
let mut opts = OpenOptions :: new ( ) ;
76
77
opts. read ( true ) ;
@@ -82,15 +83,18 @@ impl FileAttr {
82
83
}
83
84
84
85
pub fn size ( & self ) -> u64 {
85
- self . size
86
+ match self {
87
+ Self :: File { size } => size,
88
+ Self :: Dir => 0 ,
89
+ }
86
90
}
87
91
88
92
pub fn perm ( & self ) -> FilePermissions {
89
93
FilePermissions { }
90
94
}
91
95
92
96
pub fn file_type ( & self ) -> FileType {
93
- FileType { is_dir : self . is_dir }
97
+ self == FileAttr :: Dir
94
98
}
95
99
96
100
pub fn modified ( & self ) -> io:: Result < SystemTime > {
@@ -156,7 +160,7 @@ impl DirEntry {
156
160
}
157
161
158
162
pub fn file_name ( & self ) -> OsString {
159
- self . path . file_name ( ) . unwrap_or ( crate :: ffi :: OsStr :: new ( "" ) ) . to_os_string ( )
163
+ self . path . file_name ( ) . unwrap_or_default ( )
160
164
}
161
165
162
166
pub fn metadata ( & self ) -> io:: Result < FileAttr > {
@@ -203,29 +207,34 @@ impl OpenOptions {
203
207
impl File {
204
208
pub fn open ( path : & Path , opts : & OpenOptions ) -> io:: Result < File > {
205
209
run_path_with_cstr ( path, & |path| {
206
- let file = match (
207
- opts. read ,
208
- opts. write ,
209
- opts. append ,
210
- opts. truncate ,
211
- opts. create ,
212
- opts. create_new ,
213
- ) {
210
+ let file = match opts {
214
211
// read + write - unsupported
215
- ( true , true , _ , _ , _ , _ ) => {
212
+ OpenOptions { read : true , write : true , .. } => {
216
213
return Err ( io:: Error :: new (
217
214
io:: ErrorKind :: InvalidInput ,
218
215
"Opening files with read and write access is unsupported on this target" ,
219
216
) ) ;
220
217
}
221
218
222
219
// read
223
- ( true , false , _, false , false , false ) => unsafe {
224
- vex_sdk:: vexFileOpen ( path. as_ptr ( ) , c"" . as_ptr ( ) )
225
- } ,
220
+ OpenOptions {
221
+ read : true ,
222
+ write : false ,
223
+ append : _,
224
+ truncate : false ,
225
+ create : false ,
226
+ create_new : false ,
227
+ } => unsafe { vex_sdk:: vexFileOpen ( path. as_ptr ( ) , c"" . as_ptr ( ) ) } ,
226
228
227
229
// append
228
- ( false , _, true , false , create, create_new) => unsafe {
230
+ OpenOptions {
231
+ read : false ,
232
+ write : _,
233
+ append : true ,
234
+ truncate : false ,
235
+ create,
236
+ create_new,
237
+ } => unsafe {
229
238
if create_new {
230
239
if vex_sdk:: vexFileStatus ( path. as_ptr ( ) ) != 0 {
231
240
return Err ( io:: Error :: new (
@@ -246,7 +255,14 @@ impl File {
246
255
} ,
247
256
248
257
// write
249
- ( false , true , false , truncate, create, create_new) => unsafe {
258
+ OpenOptions {
259
+ read : false ,
260
+ write : true ,
261
+ append : false ,
262
+ truncate,
263
+ create,
264
+ create_new,
265
+ } => unsafe {
250
266
if create_new {
251
267
if vex_sdk:: vexFileStatus ( path. as_ptr ( ) ) != 0 {
252
268
return Err ( io:: Error :: new (
0 commit comments