@@ -16,9 +16,9 @@ pub struct File {
1616}
1717
1818#[ derive( Clone ) ]
19- pub struct FileAttr {
20- size : u64 ,
21- is_dir : bool ,
19+ pub enum FileAttr {
20+ Dir ,
21+ File { size : u64 } ,
2222}
2323
2424pub struct ReadDir ( !) ;
@@ -54,23 +54,24 @@ pub struct DirBuilder {}
5454impl FileAttr {
5555 /// Creates a FileAttr by getting data from an opened file.
5656 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 } )
6160 } else {
6261 Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , "Failed to get file size" ) )
6362 }
6463 }
6564
6665 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+
6769 run_path_with_cstr ( path, & |c_path| {
6870 let file_type = unsafe { vex_sdk:: vexFileStatus ( c_path. as_ptr ( ) ) } ;
69- let is_dir = file_type == 3 ;
7071
7172 // 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 )
7475 } else {
7576 let mut opts = OpenOptions :: new ( ) ;
7677 opts. read ( true ) ;
@@ -82,15 +83,18 @@ impl FileAttr {
8283 }
8384
8485 pub fn size ( & self ) -> u64 {
85- self . size
86+ match self {
87+ Self :: File { size } => size,
88+ Self :: Dir => 0 ,
89+ }
8690 }
8791
8892 pub fn perm ( & self ) -> FilePermissions {
8993 FilePermissions { }
9094 }
9195
9296 pub fn file_type ( & self ) -> FileType {
93- FileType { is_dir : self . is_dir }
97+ self == FileAttr :: Dir
9498 }
9599
96100 pub fn modified ( & self ) -> io:: Result < SystemTime > {
@@ -156,7 +160,7 @@ impl DirEntry {
156160 }
157161
158162 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 ( )
160164 }
161165
162166 pub fn metadata ( & self ) -> io:: Result < FileAttr > {
@@ -203,29 +207,34 @@ impl OpenOptions {
203207impl File {
204208 pub fn open ( path : & Path , opts : & OpenOptions ) -> io:: Result < File > {
205209 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 {
214211 // read + write - unsupported
215- ( true , true , _ , _ , _ , _ ) => {
212+ OpenOptions { read : true , write : true , .. } => {
216213 return Err ( io:: Error :: new (
217214 io:: ErrorKind :: InvalidInput ,
218215 "Opening files with read and write access is unsupported on this target" ,
219216 ) ) ;
220217 }
221218
222219 // 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 ( ) ) } ,
226228
227229 // 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 {
229238 if create_new {
230239 if vex_sdk:: vexFileStatus ( path. as_ptr ( ) ) != 0 {
231240 return Err ( io:: Error :: new (
@@ -246,7 +255,14 @@ impl File {
246255 } ,
247256
248257 // 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 {
250266 if create_new {
251267 if vex_sdk:: vexFileStatus ( path. as_ptr ( ) ) != 0 {
252268 return Err ( io:: Error :: new (
0 commit comments