Skip to content

Commit 4fdf859

Browse files
committed
Merge pull request #897 from matklad/refactor-run
Refactor run family of functions
2 parents 6e393b3 + c29ee66 commit 4fdf859

File tree

5 files changed

+54
-50
lines changed

5 files changed

+54
-50
lines changed

src/bin/rustfmt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extern crate toml;
1717
extern crate env_logger;
1818
extern crate getopts;
1919

20-
use rustfmt::{run, run_from_stdin};
20+
use rustfmt::{run, Input};
2121
use rustfmt::config::{Config, WriteMode};
2222

2323
use std::env;
@@ -197,7 +197,7 @@ fn execute() -> i32 {
197197
// write_mode is always Plain for Stdin.
198198
config.write_mode = WriteMode::Plain;
199199

200-
run_from_stdin(input, &config);
200+
run(Input::Text(input), &config);
201201
0
202202
}
203203
Operation::Format { files, config_path } => {
@@ -233,7 +233,7 @@ fn execute() -> i32 {
233233
print_usage(&opts, &e);
234234
return 1;
235235
}
236-
run(&file, &config);
236+
run(Input::File(file), &config);
237237
}
238238
0
239239
}

src/filemap.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ pub fn append_newlines(file_map: &mut FileMap) {
3131
}
3232
}
3333

34-
pub fn write_all_files<T>(file_map: &FileMap, mut out: T, config: &Config) -> Result<(), io::Error>
34+
pub fn write_all_files<T>(file_map: &FileMap, out: &mut T, config: &Config) -> Result<(), io::Error>
3535
where T: Write
3636
{
37-
output_header(&mut out, config.write_mode).ok();
37+
output_header(out, config.write_mode).ok();
3838
for filename in file_map.keys() {
39-
try!(write_file(&file_map[filename], filename, &mut out, config));
39+
try!(write_file(&file_map[filename], filename, out, config));
4040
}
41-
output_footer(&mut out, config.write_mode).ok();
41+
output_footer(out, config.write_mode).ok();
4242

4343
Ok(())
4444
}
@@ -80,11 +80,11 @@ pub fn write_system_newlines<T>(writer: T,
8080
}
8181
}
8282

83-
pub fn write_file<T>(text: &StringBuffer,
84-
filename: &str,
85-
out: &mut T,
86-
config: &Config)
87-
-> Result<Option<String>, io::Error>
83+
fn write_file<T>(text: &StringBuffer,
84+
filename: &str,
85+
out: &mut T,
86+
config: &Config)
87+
-> Result<Option<String>, io::Error>
8888
where T: Write
8989
{
9090

src/lib.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ use syntax::parse::{self, ParseSess};
3333

3434
use std::io::stdout;
3535
use std::ops::{Add, Sub};
36-
use std::path::Path;
36+
use std::path::{Path, PathBuf};
3737
use std::rc::Rc;
3838
use std::collections::HashMap;
3939
use std::fmt;
4040

4141
use issues::{BadIssueSeeker, Issue};
4242
use filemap::FileMap;
4343
use visitor::FmtVisitor;
44-
use config::Config;
44+
use config::{Config, WriteMode};
4545

4646
#[macro_use]
4747
mod utils;
@@ -287,7 +287,7 @@ fn fmt_ast(krate: &ast::Crate,
287287
// Formatting done on a char by char or line by line basis.
288288
// TODO(#209) warn on bad license
289289
// TODO(#20) other stuff for parity with make tidy
290-
pub fn fmt_lines(file_map: &mut FileMap, config: &Config) -> FormatReport {
290+
fn format_lines(file_map: &mut FileMap, config: &Config) -> FormatReport {
291291
let mut truncate_todo = Vec::new();
292292
let mut report = FormatReport { file_error_map: HashMap::new() };
293293

@@ -367,7 +367,7 @@ pub fn fmt_lines(file_map: &mut FileMap, config: &Config) -> FormatReport {
367367
report
368368
}
369369

370-
pub fn format_string(input: String, config: &Config) -> FileMap {
370+
fn format_string(input: String, config: &Config) -> FileMap {
371371
let path = "stdin";
372372
let codemap = Rc::new(CodeMap::new());
373373

@@ -403,7 +403,7 @@ pub fn format_string(input: String, config: &Config) -> FileMap {
403403
file_map
404404
}
405405

406-
pub fn format(file: &Path, config: &Config) -> FileMap {
406+
fn format_file(file: &Path, config: &Config) -> FileMap {
407407
let codemap = Rc::new(CodeMap::new());
408408

409409
let tty_handler = Handler::with_tty_emitter(ColorConfig::Auto,
@@ -428,27 +428,35 @@ pub fn format(file: &Path, config: &Config) -> FileMap {
428428
file_map
429429
}
430430

431-
pub fn run(file: &Path, config: &Config) {
432-
let mut result = format(file, config);
431+
pub fn format_input(input: Input, config: &Config) -> (FileMap, FormatReport) {
432+
let mut file_map = match input {
433+
Input::File(ref file) => format_file(file, config),
434+
Input::Text(text) => format_string(text, config),
435+
};
433436

434-
print!("{}", fmt_lines(&mut result, config));
435-
let out = stdout();
436-
let write_result = filemap::write_all_files(&result, out, config);
437+
let report = format_lines(&mut file_map, config);
438+
(file_map, report)
439+
}
437440

438-
if let Err(msg) = write_result {
439-
println!("Error writing files: {}", msg);
440-
}
441+
pub enum Input {
442+
File(PathBuf),
443+
Text(String),
441444
}
442445

443-
// Similar to run, but takes an input String instead of a file to format
444-
pub fn run_from_stdin(input: String, config: &Config) {
445-
let mut result = format_string(input, config);
446-
fmt_lines(&mut result, config);
446+
pub fn run(input: Input, config: &Config) {
447+
let (file_map, report) = format_input(input, config);
448+
449+
let ignore_errors = config.write_mode == WriteMode::Plain;
450+
if !ignore_errors {
451+
print!("{}", report);
452+
}
447453

448454
let mut out = stdout();
449-
let write_result = filemap::write_file(&result["stdin"], "stdin", &mut out, config);
455+
let write_result = filemap::write_all_files(&file_map, &mut out, config);
450456

451457
if let Err(msg) = write_result {
452-
panic!("Error writing to stdout: {}", msg);
458+
if !ignore_errors {
459+
println!("Error writing files: {}", msg);
460+
}
453461
}
454462
}

tests/system.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern crate term;
1616
use std::collections::HashMap;
1717
use std::fs;
1818
use std::io::{self, Read, BufRead, BufReader};
19-
use std::path::Path;
19+
use std::path::{Path, PathBuf};
2020

2121
use rustfmt::*;
2222
use rustfmt::filemap::{write_system_newlines, FileMap};
@@ -74,12 +74,8 @@ fn checkstyle_test() {
7474
// Helper function for comparing the results of rustfmt
7575
// to a known output file generated by one of the write modes.
7676
fn assert_output(source: &str, expected_filename: &str, write_mode: Option<WriteMode>) {
77-
let file_map = run_rustfmt(source.to_string(), write_mode);
78-
79-
let mut config = read_config(&source);
80-
if let Some(write_mode) = write_mode {
81-
config.write_mode = write_mode;
82-
}
77+
let config = read_config(&source, write_mode);
78+
let (file_map, _report) = format_file(source, &config);
8379

8480
// Populate output by writing to a vec.
8581
let mut out = vec![];
@@ -180,7 +176,7 @@ fn print_mismatches(result: HashMap<String, Vec<Mismatch>>) {
180176
assert!(t.reset().unwrap());
181177
}
182178

183-
fn read_config(filename: &str) -> Config {
179+
fn read_config(filename: &str, write_mode: Option<WriteMode>) -> Config {
184180
let sig_comments = read_significant_comments(&filename);
185181
let mut config = get_config(sig_comments.get("config").map(|x| &(*x)[..]));
186182

@@ -192,25 +188,25 @@ fn read_config(filename: &str) -> Config {
192188

193189
// Don't generate warnings for to-do items.
194190
config.report_todo = ReportTactic::Never;
191+
192+
if let Some(mode) = write_mode {
193+
config.write_mode = mode
194+
}
195+
195196
config
196197
}
197198

198-
// Simulate run()
199-
fn run_rustfmt(filename: String, write_mode: Option<WriteMode>) -> FileMap {
200-
let mut config = read_config(&filename);
201-
if let Some(write_mode) = write_mode {
202-
config.write_mode = write_mode;
203-
}
204-
format(Path::new(&filename), &config)
199+
fn format_file<P: Into<PathBuf>>(filename: P, config: &Config) -> (FileMap, FormatReport) {
200+
let input = Input::File(filename.into());
201+
format_input(input, &config)
205202
}
206203

207204
pub fn idempotent_check(filename: String,
208205
write_mode: Option<WriteMode>)
209206
-> Result<FormatReport, HashMap<String, Vec<Mismatch>>> {
210207
let sig_comments = read_significant_comments(&filename);
211-
let config = read_config(&filename);
212-
let mut file_map = run_rustfmt(filename, write_mode);
213-
let format_report = fmt_lines(&mut file_map, &config);
208+
let config = read_config(&filename, write_mode);
209+
let (file_map, format_report) = format_file(filename, &config);
214210

215211
let mut write_result = HashMap::new();
216212
for (filename, text) in file_map.iter() {

tests/writemode/checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<checkstyle version="4.3"><file name="tests/source/fn-single-line.rs"><error line="1" severity="warning" message="Should be `fn foo_expr() { 1 }`" /><error line="1" severity="warning" message="Should be `fn foo_stmt() { foo(); }`" /><error line="1" severity="warning" message="Should be `fn foo_decl_local() { let z = 5; }`" /><error line="1" severity="warning" message="Should be `fn foo_decl_item(x: &amp;mut i32) { x = 3; }`" /><error line="1" severity="warning" message="Should be `fn empty() {}`" /><error line="1" severity="warning" message="Should be `fn foo_return() -&gt; String { &quot;yay&quot; }`" /><error line="1" severity="warning" message="Should be `fn foo_where() -&gt; T`" /><error line="1" severity="warning" message="Should be ` where T: Sync`" /><error line="1" severity="warning" message="Should be `{`" /><error line="50" severity="warning" message="Should be `fn lots_of_space() { 1 }`" /><error line="57" severity="warning" message="Should be ` fn dummy(&amp;self) {}`" /><error line="57" severity="warning" message="Should be `trait CoolerTypes {`" /><error line="57" severity="warning" message="Should be ` fn dummy(&amp;self) {}`" /><error line="57" severity="warning" message="Should be `fn Foo&lt;T&gt;() where T: Bar {}`" /><error line="57" severity="warning" message="Should be ``" /></file></checkstyle>
2+
<checkstyle version="4.3"><file name="tests/source/fn-single-line.rs"><error line="1" severity="warning" message="Should be `fn foo_expr() { 1 }`" /><error line="1" severity="warning" message="Should be `fn foo_stmt() { foo(); }`" /><error line="1" severity="warning" message="Should be `fn foo_decl_local() { let z = 5; }`" /><error line="1" severity="warning" message="Should be `fn foo_decl_item(x: &amp;mut i32) { x = 3; }`" /><error line="1" severity="warning" message="Should be `fn empty() {}`" /><error line="1" severity="warning" message="Should be `fn foo_return() -&gt; String { &quot;yay&quot; }`" /><error line="1" severity="warning" message="Should be `fn foo_where() -&gt; T`" /><error line="1" severity="warning" message="Should be ` where T: Sync`" /><error line="1" severity="warning" message="Should be `{`" /><error line="50" severity="warning" message="Should be `fn lots_of_space() { 1 }`" /><error line="57" severity="warning" message="Should be ` fn dummy(&amp;self) {}`" /><error line="57" severity="warning" message="Should be `trait CoolerTypes {`" /><error line="57" severity="warning" message="Should be ` fn dummy(&amp;self) {}`" /><error line="57" severity="warning" message="Should be `fn Foo&lt;T&gt;() where T: Bar {}`" /></file></checkstyle>

0 commit comments

Comments
 (0)