Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file_read_contents returning anyhow::Result #156

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions src/packs/file_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,17 @@ 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
Loading