Skip to content

Commit

Permalink
file_read_contents returning anyhow::Result
Browse files Browse the repository at this point in the history
Co-authored-by: Joey Schoblaska <[email protected]>
  • Loading branch information
perryqh and schoblaska committed Feb 28, 2024
1 parent ca3e11d commit 48a759f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 31 deletions.
19 changes: 5 additions & 14 deletions src/packs/file_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,13 @@ pub(crate) fn file_content_digest(file: &Path) -> anyhow::Result<String> {
pub fn file_read_contents(
path: &Path,
configuration: &Configuration,
) -> String {
) -> anyhow::Result<String> {
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()))
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/packs/parsing/erb/experimental/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProcessedFile> {
let contents = file_read_contents(path, configuration)?;
Ok(process_from_contents(contents, path, configuration))
}

pub(crate) fn process_from_contents(
Expand Down
6 changes: 3 additions & 3 deletions src/packs/parsing/erb/packwerk/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProcessedFile> {
let contents = file_read_contents(path, configuration)?;
Ok(process_from_contents(contents, path, configuration))
}

pub(crate) fn process_from_contents(
Expand Down
10 changes: 5 additions & 5 deletions src/packs/parsing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use super::{
pub fn process_file(
path: &Path,
configuration: &Configuration,
) -> ProcessedFile {
) -> anyhow::Result<ProcessedFile> {
if configuration.print_files {
println!("Started processing {}", path.display());
}
Expand All @@ -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 {
Expand Down Expand Up @@ -93,15 +93,15 @@ pub fn process_files_with_cache(
.par_iter()
.map(|absolute_path| -> anyhow::Result<ProcessedFile> {
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) => {
Ok(processed_file)
}
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)
}
Expand Down
6 changes: 3 additions & 3 deletions src/packs/parsing/ruby/experimental/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProcessedFile> {
let contents = file_read_contents(path, configuration)?;
Ok(process_from_contents(contents, path, configuration))
}

pub(crate) fn process_from_contents(
Expand Down
6 changes: 3 additions & 3 deletions src/packs/parsing/ruby/packwerk/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProcessedFile> {
let contents = file_read_contents(path, configuration)?;
Ok(process_from_contents(contents, path, configuration))
}

pub(crate) fn process_from_contents(
Expand Down

0 comments on commit 48a759f

Please sign in to comment.