diff --git a/src/packs/file_utils.rs b/src/packs/file_utils.rs index 0198da7b..8e5c77f1 100644 --- a/src/packs/file_utils.rs +++ b/src/packs/file_utils.rs @@ -132,22 +132,13 @@ pub(crate) fn file_content_digest(file: &Path) -> anyhow::Result { pub fn file_read_contents( path: &Path, configuration: &Configuration, -) -> String { +) -> anyhow::Result { if is_stdin_file(path, configuration) { - io::read_to_string(io::stdin()).unwrap_or_else(|_| { - panic!( - "Failed to read contents of {} from stdin", - path.to_string_lossy() - ) - }) + Ok(io::read_to_string(io::stdin()) + .context(format!("Failed to read contents of {} from stdin", path.to_string_lossy()))?) } else { - fs::read_to_string(path).unwrap_or_else(|_| { - println!( - "Failed to read contents of {} – skipping this file", - path.to_string_lossy() - ); - "".to_string() - }) + fs::read_to_string(path) + .context(format!("Failed to read contents of {}", path.to_string_lossy())) } } diff --git a/src/packs/parsing/erb/experimental/parser.rs b/src/packs/parsing/erb/experimental/parser.rs index 01d3aaa1..62f9bd10 100644 --- a/src/packs/parsing/erb/experimental/parser.rs +++ b/src/packs/parsing/erb/experimental/parser.rs @@ -10,9 +10,9 @@ use crate::packs::parsing::ruby::experimental::parser::process_from_contents as pub(crate) fn process_from_path( path: &Path, configuration: &Configuration, -) -> ProcessedFile { - let contents = file_read_contents(path, configuration); - process_from_contents(contents, path, configuration) +) -> anyhow::Result { + let contents = file_read_contents(path, configuration)?; + Ok(process_from_contents(contents, path, configuration)) } pub(crate) fn process_from_contents( diff --git a/src/packs/parsing/erb/packwerk/parser.rs b/src/packs/parsing/erb/packwerk/parser.rs index 11306218..04d32c04 100644 --- a/src/packs/parsing/erb/packwerk/parser.rs +++ b/src/packs/parsing/erb/packwerk/parser.rs @@ -10,9 +10,9 @@ use crate::packs::parsing::ruby::packwerk::parser::process_from_contents as proc pub(crate) fn process_from_path( path: &Path, configuration: &Configuration, -) -> ProcessedFile { - let contents = file_read_contents(path, configuration); - process_from_contents(contents, path, configuration) +) -> anyhow::Result { + let contents = file_read_contents(path, configuration)?; + Ok(process_from_contents(contents, path, configuration)) } pub(crate) fn process_from_contents( diff --git a/src/packs/parsing/mod.rs b/src/packs/parsing/mod.rs index b3cb0cdc..43e09929 100644 --- a/src/packs/parsing/mod.rs +++ b/src/packs/parsing/mod.rs @@ -23,7 +23,7 @@ use super::{ pub fn process_file( path: &Path, configuration: &Configuration, -) -> ProcessedFile { +) -> anyhow::Result { if configuration.print_files { println!("Started processing {}", path.display()); } @@ -49,11 +49,11 @@ pub fn process_file( } else { // Later, we can perhaps have this error, since in theory the Configuration.intersect // method should make sure we never get any files we can't handle. - ProcessedFile { + Ok(ProcessedFile { absolute_path: path.to_path_buf(), unresolved_references: vec![], definitions: vec![], // TODO - } + }) }; if configuration.print_files { @@ -93,7 +93,7 @@ pub fn process_files_with_cache( .par_iter() .map(|absolute_path| -> anyhow::Result { if is_stdin_file(absolute_path, configuration) { - Ok(process_file(absolute_path, configuration)) + process_file(absolute_path, configuration) } else { match cache.get(absolute_path)? { CacheResult::Processed(processed_file) => { @@ -101,7 +101,7 @@ pub fn process_files_with_cache( } CacheResult::Miss(empty_cache_entry) => { let processed_file = - process_file(absolute_path, configuration); + process_file(absolute_path, configuration)?; cache.write(&empty_cache_entry, &processed_file); Ok(processed_file) } diff --git a/src/packs/parsing/ruby/experimental/parser.rs b/src/packs/parsing/ruby/experimental/parser.rs index 56c4780b..4a22e875 100644 --- a/src/packs/parsing/ruby/experimental/parser.rs +++ b/src/packs/parsing/ruby/experimental/parser.rs @@ -182,9 +182,9 @@ impl<'a> Visitor for ReferenceCollector<'a> { pub(crate) fn process_from_path( path: &Path, configuration: &Configuration, -) -> ProcessedFile { - let contents = file_read_contents(path, configuration); - process_from_contents(contents, path, configuration) +) -> anyhow::Result { + let contents = file_read_contents(path, configuration)?; + Ok(process_from_contents(contents, path, configuration)) } pub(crate) fn process_from_contents( diff --git a/src/packs/parsing/ruby/packwerk/parser.rs b/src/packs/parsing/ruby/packwerk/parser.rs index 7c27361c..7e271d22 100644 --- a/src/packs/parsing/ruby/packwerk/parser.rs +++ b/src/packs/parsing/ruby/packwerk/parser.rs @@ -203,9 +203,9 @@ impl<'a> Visitor for ReferenceCollector<'a> { pub(crate) fn process_from_path( path: &Path, configuration: &Configuration, -) -> ProcessedFile { - let contents = file_read_contents(path, configuration); - process_from_contents(contents, path, configuration) +) -> anyhow::Result { + let contents = file_read_contents(path, configuration)?; + Ok(process_from_contents(contents, path, configuration)) } pub(crate) fn process_from_contents(