Common input-handling idiom for various tooling bits
Brought up in https://github.com/amazon-ion/ion-cli/pull/54#discussion_r1227312405
In several of the subcommands we have the same input-handling pattern:
if let Some(input_file_names) = matches.get_many::<String>("input") {
// Input files were specified, run the logic on each of them in turn
for input_file in input_file_names {
let file = File::open(input_file.as_str())
.with_context(|| format!("Could not open file '{}'", &input_file))?;
let mut reader = ReaderBuilder::new()
.build(file)
.with_context(|| format!("Input file {} was not valid Ion.", &input_file))?;
// do something with the reader
}
} else {
// No input files were specified, run logic on STDIN.
let mut reader = ReaderBuilder::new()
.build(stdin().lock())
.with_context(|| "Input was not valid Ion.")?;
// do something with the reader
}
We ought to have shared code for this. We could solve it by building an input stream abstraction over a set of files, but then you run into places like #48 where it convenient or even necessary to have context about what input you are processing at the moment, so perhaps it would be best if the common idiom was some flavor iterator-of-inputs-with-source-metadata.
This must have been solved somewhere else before...
Examples (to/dump/inspect)
https://github.com/amazon-ion/ion-cli/blob/e7feb98d625056f2440a9dba5dcbb2308205bbef/src/bin/ion/commands/beta/to/json.rs#L49-L65
https://github.com/amazon-ion/ion-cli/blob/e7feb98d625056f2440a9dba5dcbb2308205bbef/src/bin/ion/commands/dump.rs#L68-L79
https://github.com/amazon-ion/ion-cli/blob/e7feb98d625056f2440a9dba5dcbb2308205bbef/src/bin/ion/commands/beta/inspect.rs#L118-L162