@@ -8,14 +8,16 @@ extern crate serde_derive;
88mod config;
99mod constants;
1010mod file_location;
11- mod operation_result;
1211mod page_index;
12+ mod operation_result;
13+ mod program_error;
1314
1415use std:: fs;
1516use std:: env;
1617use std:: path:: { Path } ;
1718
1819use crate :: operation_result:: * ;
20+ use crate :: program_error:: * ;
1921use crate :: page_index:: * ;
2022use crate :: config:: * ;
2123use crate :: file_location:: * ;
@@ -24,21 +26,29 @@ use toml::Value;
2426use walkdir:: { DirEntry , WalkDir } ;
2527use yaml_rust:: { YamlLoader } ;
2628
27- fn main ( ) -> Result < ( ) , std:: io:: Error > {
29+
30+ fn main ( ) -> Result < ( ) , ProgramError > {
2831 let args: Vec < String > = env:: args ( ) . collect ( ) ;
2932 let config = Config :: new ( & args) ;
3033
3134 println ! ( "Scanning {0}" , & config. scan_path) ;
3235 let index = traverse_files ( & Path :: new ( & config. scan_path ) ) ;
33- let index = serde_json:: to_string ( & index) . expect ( "Unable to serialize page index" ) ;
36+ let error_count: usize = index. iter ( ) . filter ( |e| e. is_err ( ) ) . count ( ) ;
37+ let index: Vec < PageIndex > = index. into_iter ( ) . filter_map ( |a| a. ok ( ) ) . collect ( ) ;
38+ let index = serde_json:: to_string ( & index) ?;
3439
3540 println ! ( "Writing index to {0}" , & config. index_path) ;
36- fs:: create_dir_all ( Path :: new ( & config. index_path ) . with_file_name ( constants:: EMPTY_STRING ) ) . expect ( "Error writing index" ) ;
41+ fs:: create_dir_all ( Path :: new ( & config. index_path ) . with_file_name ( constants:: EMPTY_STRING ) ) ? ;
3742
38- fs:: write ( config. index_path , index)
43+ fs:: write ( config. index_path , index) ?;
44+ if error_count > 0 {
45+ Err ( ProgramError :: MetaError ( MetaError :: new ( error_count, "Failed to process all content files" ) ) )
46+ } else {
47+ Ok ( ( ) )
48+ }
3949}
4050
41- fn traverse_files ( content_dir_path : & Path ) -> Vec < PageIndex > {
51+ fn traverse_files ( content_dir_path : & Path ) -> Vec < Result < PageIndex , OperationResult > > {
4252 let mut index = Vec :: new ( ) ;
4353 for entry in WalkDir :: new ( content_dir_path)
4454 . into_iter ( )
@@ -48,12 +58,13 @@ fn traverse_files(content_dir_path: &Path) -> Vec<PageIndex> {
4858 if file_location. is_err ( ) {
4959 continue ;
5060 }
51-
52- match process_file ( & file_location. unwrap ( ) ) {
53- Ok ( page) => index. push ( page) ,
54- Err ( OperationResult :: Parse ( ref err) ) => println ! ( "{}" , err) ,
55- Err ( OperationResult :: Io ( ref err) ) => println ! ( "{}" , err) ,
56- _ => ( )
61+ let process_result = process_file ( & file_location. unwrap ( ) ) ;
62+ match process_result {
63+ Err ( OperationResult :: Skip ( ref err) ) => println ! ( "{}" , err) , // Skips don't need to be handled
64+ Err ( OperationResult :: Path ( ref err) ) => { println ! ( "{}" , err) ; index. push ( process_result) ; } ,
65+ Err ( OperationResult :: Parse ( ref err) ) => { println ! ( "{}" , err) ; index. push ( process_result) ; } ,
66+ Err ( OperationResult :: Io ( ref err) ) => { println ! ( "{}" , err) ; index. push ( process_result) ; } ,
67+ Ok ( _) => index. push ( process_result)
5768 }
5869 } else if let Some ( io_error) = entry. unwrap_err ( ) . into_io_error ( ) {
5970 println ! ( "Failed {}" , io_error) ;
@@ -151,7 +162,7 @@ fn process_yaml_front_matter(contents: &str, file_location: &FileLocation) -> Re
151162 return Err ( OperationResult :: Parse ( ParseError :: new ( & file_location. absolute_path , "Could not split on YAML fence." ) ) )
152163 }
153164
154- let front_matter = split_content[ length - 2 ] . trim ( ) ;
165+ let front_matter = split_content[ 1 ] . trim ( ) ;
155166 let front_matter = YamlLoader :: load_from_str ( front_matter)
156167 . map_err ( |_| ParseError :: new ( & file_location. absolute_path , "Failed to get front matter." ) ) ?;
157168 let front_matter = front_matter. first ( )
0 commit comments