cleanup make_input

This commit is contained in:
Orion Gonzalez 2024-08-27 18:46:44 +02:00
parent ddcb073c53
commit b218623ea0

View File

@ -481,36 +481,43 @@ fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<OutFileNa
(odir, ofile) (odir, ofile)
} }
// Extract input (string or file and optional path) from matches. /// Extract input (string or file and optional path) from matches.
/// This handles reading from stdin if `-` is provided.
fn make_input( fn make_input(
early_dcx: &EarlyDiagCtxt, early_dcx: &EarlyDiagCtxt,
free_matches: &[String], free_matches: &[String],
) -> Result<Option<Input>, ErrorGuaranteed> { ) -> Result<Option<Input>, ErrorGuaranteed> {
let [ifile] = free_matches else { return Ok(None) }; let [input_file] = free_matches else { return Ok(None) };
if ifile == "-" {
let mut src = String::new(); if input_file != "-" {
if io::stdin().read_to_string(&mut src).is_err() { // Normal `Input::File`
// Immediately stop compilation if there was an issue reading return Ok(Some(Input::File(PathBuf::from(input_file))));
// the input (for example if the input stream is not UTF-8). }
let reported =
early_dcx.early_err("couldn't read from stdin, as it did not contain valid UTF-8"); // read from stdin as `Input::Str`
return Err(reported); let mut input = String::new();
} if io::stdin().read_to_string(&mut input).is_err() {
if let Ok(path) = env::var("UNSTABLE_RUSTDOC_TEST_PATH") { // Immediately stop compilation if there was an issue reading
// the input (for example if the input stream is not UTF-8).
let reported =
early_dcx.early_err("couldn't read from stdin, as it did not contain valid UTF-8");
return Err(reported);
}
let name = match env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
Ok(path) => {
let line = env::var("UNSTABLE_RUSTDOC_TEST_LINE").expect( let line = env::var("UNSTABLE_RUSTDOC_TEST_LINE").expect(
"when UNSTABLE_RUSTDOC_TEST_PATH is set \ "when UNSTABLE_RUSTDOC_TEST_PATH is set \
UNSTABLE_RUSTDOC_TEST_LINE also needs to be set", UNSTABLE_RUSTDOC_TEST_LINE also needs to be set",
); );
let line = isize::from_str_radix(&line, 10) let line = isize::from_str_radix(&line, 10)
.expect("UNSTABLE_RUSTDOC_TEST_LINE needs to be an number"); .expect("UNSTABLE_RUSTDOC_TEST_LINE needs to be an number");
let file_name = FileName::doc_test_source_code(PathBuf::from(path), line); FileName::doc_test_source_code(PathBuf::from(path), line)
Ok(Some(Input::Str { name: file_name, input: src }))
} else {
Ok(Some(Input::Str { name: FileName::anon_source_code(&src), input: src }))
} }
} else { Err(_) => FileName::anon_source_code(&input),
Ok(Some(Input::File(PathBuf::from(ifile)))) };
}
Ok(Some(Input::Str { name, input }))
} }
/// Whether to stop or continue compilation. /// Whether to stop or continue compilation.