11use crate :: entities:: ImmutableFileNumber ;
22
33use std:: ffi:: OsStr ;
4+ use std:: io;
5+ use std:: num:: ParseIntError ;
46use std:: path:: { Path , PathBuf } ;
5-
7+ use thiserror :: Error ;
68use walkdir:: WalkDir ;
79
810fn is_immutable ( path : & Path ) -> bool {
@@ -16,18 +18,33 @@ pub struct ImmutableFile {
1618 pub number : ImmutableFileNumber ,
1719}
1820
21+ #[ derive( Error , Debug ) ]
22+ pub enum ImmutableFileCreationError {
23+ #[ error( "Couldn't extract the file stem for '{path:?}'" ) ]
24+ FileStemExtraction { path : PathBuf } ,
25+ #[ error( "Couldn't extract the filename as string for '{path:?}'" ) ]
26+ FileNameExtraction { path : PathBuf } ,
27+ #[ error( "Error while parsing immutable file number" ) ]
28+ FileNumberParsing ( #[ from] ParseIntError ) ,
29+ }
30+
31+ #[ derive( Error , Debug ) ]
32+ pub enum ImmutableFileListingError {
33+ #[ error( "metadata parsing failed" ) ]
34+ MetadataParsing ( #[ from] io:: Error ) ,
35+ #[ error( "immutable file creation error" ) ]
36+ ImmutableFileCreation ( #[ from] ImmutableFileCreationError ) ,
37+ }
38+
1939impl ImmutableFile {
20- pub fn new ( path : PathBuf ) -> Result < Self , String > {
40+ pub fn new ( path : PathBuf ) -> Result < Self , ImmutableFileCreationError > {
2141 let filename = path
2242 . file_stem ( )
23- . ok_or ( format ! ( "Couldn't extract the file stem for '{:?}'" , path) ) ?;
24- let filename = filename. to_str ( ) . ok_or ( format ! (
25- "Couldn't extract the filename as string for '{:?}'" ,
26- path
27- ) ) ?;
28- let immutable_file_number = filename
29- . parse :: < ImmutableFileNumber > ( )
30- . map_err ( |e| e. to_string ( ) ) ?;
43+ . ok_or ( ImmutableFileCreationError :: FileStemExtraction { path : path. clone ( ) } ) ?;
44+ let filename = filename
45+ . to_str ( )
46+ . ok_or ( ImmutableFileCreationError :: FileNameExtraction { path : path. clone ( ) } ) ?;
47+ let immutable_file_number = filename. parse :: < ImmutableFileNumber > ( ) ?;
3148
3249 Ok ( Self {
3350 path,
@@ -39,15 +56,17 @@ impl ImmutableFile {
3956 ///
4057 /// Important Note: It will skip the last chunk / primary / secondary trio since they're not yet
4158 /// complete.
42- pub fn list_completed_in_dir ( dir : & Path ) -> Result < Vec < ImmutableFile > , String > {
59+ pub fn list_completed_in_dir (
60+ dir : & Path ,
61+ ) -> Result < Vec < ImmutableFile > , ImmutableFileListingError > {
4362 let mut files: Vec < ImmutableFile > = vec ! [ ] ;
4463
4564 for path in WalkDir :: new ( dir)
4665 . into_iter ( )
4766 . filter_map ( |file| file. ok ( ) )
4867 . map ( |f| f. path ( ) . to_owned ( ) )
4968 {
50- let metadata = path. metadata ( ) . map_err ( |e| e . to_string ( ) ) ?;
69+ let metadata = path. metadata ( ) ?;
5170 if metadata. is_file ( ) && is_immutable ( & path) {
5271 let immutable_file = ImmutableFile :: new ( path) ?;
5372 files. push ( immutable_file) ;
0 commit comments