diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index af6996613bfc..0b30a7f87982 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -217,17 +217,16 @@ fn execute(opts: &Options) -> FmtResult { // write_mode is always Plain for Stdin. config.write_mode = WriteMode::Plain; + // parse file_lines + if let Some(ref file_lines) = matches.opt_str("file-lines") { + config.file_lines = try!(file_lines.parse()); + } + Ok(run(Input::Text(input), &config)) } - Operation::Format { - mut files, - config_path, - } => { + Operation::Format { files, config_path } => { let options = try!(CliOptions::from_matches(&matches)); - // Add any additional files that were specified via `--file-lines`. - files.extend(options.file_lines.files().cloned().map(PathBuf::from)); - let mut config = Config::default(); let mut path = None; // Load the config path file if provided @@ -345,9 +344,8 @@ fn determine_operation(matches: &Matches) -> FmtResult { Some(dir) }); - // if no file argument is supplied and `--file-lines` is not specified, read from stdin - if matches.free.is_empty() && !matches.opt_present("file-lines") { - + // if no file argument is supplied, read from stdin + if matches.free.is_empty() { let mut buffer = String::new(); try!(io::stdin().read_to_string(&mut buffer)); @@ -357,7 +355,6 @@ fn determine_operation(matches: &Matches) -> FmtResult { }); } - // We append files from `--file-lines` later in `execute()`. let files: Vec<_> = matches.free.iter().map(PathBuf::from).collect(); Ok(Operation::Format { diff --git a/src/file_lines.rs b/src/file_lines.rs index 7e2ba13c8b05..e68751eb8875 100644 --- a/src/file_lines.rs +++ b/src/file_lines.rs @@ -162,6 +162,10 @@ impl<'a> iter::Iterator for Files<'a> { } fn canonicalize_path_string(s: &str) -> Result { + if s == "stdin" { + return Ok(s.to_string()); + } + match path::PathBuf::from(s).canonicalize() { Ok(canonicalized) => canonicalized.to_str().map(|s| s.to_string()).ok_or(()), _ => Err(()),