1
Fork 0

Simplify make_input

This commit is contained in:
bjorn3 2021-05-02 17:57:04 +02:00
parent 4a6cfc6788
commit b25292473a

View file

@ -235,21 +235,15 @@ fn run_compiler(
registry: diagnostics_registry(), registry: diagnostics_registry(),
}; };
match make_input(&matches.free) { match make_input(config.opts.error_format, &matches.free) {
Some((input, input_file_path, input_err)) => { Err(ErrorReported) => return Err(ErrorReported),
if let Some(err) = input_err { Ok(Some((input, input_file_path))) => {
// Immediately stop compilation if there was an issue reading
// the input (for example if the input stream is not UTF-8).
early_error_no_abort(config.opts.error_format, &err.to_string());
return Err(ErrorReported);
}
config.input = input; config.input = input;
config.input_path = input_file_path; config.input_path = input_file_path;
callbacks.config(&mut config); callbacks.config(&mut config);
} }
None => match matches.free.len() { Ok(None) => match matches.free.len() {
0 => { 0 => {
callbacks.config(&mut config); callbacks.config(&mut config);
interface::run_compiler(config, |compiler| { interface::run_compiler(config, |compiler| {
@ -469,19 +463,23 @@ fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<PathBuf>)
} }
// Extract input (string or file and optional path) from matches. // Extract input (string or file and optional path) from matches.
fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>, Option<io::Error>)> { fn make_input(
error_format: ErrorOutputType,
free_matches: &[String],
) -> Result<Option<(Input, Option<PathBuf>)>, ErrorReported> {
if free_matches.len() == 1 { if free_matches.len() == 1 {
let ifile = &free_matches[0]; let ifile = &free_matches[0];
if ifile == "-" { if ifile == "-" {
let mut src = String::new(); let mut src = String::new();
let err = if io::stdin().read_to_string(&mut src).is_err() { if io::stdin().read_to_string(&mut src).is_err() {
Some(io::Error::new( // Immediately stop compilation if there was an issue reading
io::ErrorKind::InvalidData, // the input (for example if the input stream is not UTF-8).
early_error_no_abort(
error_format,
"couldn't read from stdin, as it did not contain valid UTF-8", "couldn't read from stdin, as it did not contain valid UTF-8",
)) );
} else { return Err(ErrorReported);
None }
};
if let Ok(path) = env::var("UNSTABLE_RUSTDOC_TEST_PATH") { if let Ok(path) = env::var("UNSTABLE_RUSTDOC_TEST_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 \
@ -490,14 +488,15 @@ fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>, Option
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); let file_name = FileName::doc_test_source_code(PathBuf::from(path), line);
return Some((Input::Str { name: file_name, input: src }, None, err)); Ok(Some((Input::Str { name: file_name, input: src }, None)))
} else {
Ok(Some((Input::Str { name: FileName::anon_source_code(&src), input: src }, None)))
} }
Some((Input::Str { name: FileName::anon_source_code(&src), input: src }, None, err))
} else { } else {
Some((Input::File(PathBuf::from(ifile)), Some(PathBuf::from(ifile)), None)) Ok(Some((Input::File(PathBuf::from(ifile)), Some(PathBuf::from(ifile)))))
} }
} else { } else {
None Ok(None)
} }
} }