Skip to content

Commit 0957f31

Browse files
committed
Merge pull request #165 from SBSTP/config
Add project-specific configuration file support.
2 parents b647416 + 99b0aa9 commit 0957f31

File tree

6 files changed

+78
-60
lines changed

6 files changed

+78
-60
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ description = "Tool to find and fix Rust formatting issues"
77
repository = "https://github.com/nick29581/rustfmt"
88
readme = "README.md"
99
license = "Apache-2.0/MIT"
10-
build = "build.rs"
1110

1211
[dependencies.strings]
1312
strings = "0.0.1"

build.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/bin/rustfmt.rs

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,63 @@
77
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
10-
10+
#![feature(path_ext)]
11+
#![feature(rustc_private)]
1112
#![cfg(not(test))]
1213
#![feature(result_expect)]
1314

15+
#[macro_use]
16+
extern crate log;
1417
extern crate rustfmt;
18+
extern crate toml;
1519

1620
use rustfmt::{WriteMode, run};
1721
use rustfmt::config::Config;
1822

19-
use std::fs::File;
20-
use std::io::Read;
23+
use std::env;
24+
use std::fs::{File, PathExt};
25+
use std::io::{self, Read};
26+
use std::path::PathBuf;
2127
use std::str::FromStr;
2228

29+
// Try to find a project file in the current directory and its parents.
30+
fn lookup_project_file() -> io::Result<PathBuf> {
31+
let mut current = try!(env::current_dir());
32+
loop {
33+
let config_file = current.join("rustfmt.toml");
34+
if config_file.exists() {
35+
return Ok(config_file);
36+
} else {
37+
current = match current.parent() {
38+
// if the current directory has no parent, we're done searching
39+
None => return Err(io::Error::new(io::ErrorKind::NotFound, "config not found")),
40+
Some(path) => path.to_path_buf(),
41+
};
42+
}
43+
}
44+
}
45+
46+
// Try to find a project file. If it's found, read it.
47+
fn lookup_and_read_project_file() -> io::Result<(PathBuf, String)> {
48+
let path = try!(lookup_project_file());
49+
let mut file = try!(File::open(&path));
50+
let mut toml = String::new();
51+
try!(file.read_to_string(&mut toml));
52+
Ok((path, toml))
53+
}
54+
2355
fn main() {
24-
let mut def_config_file = File::open("default.toml").unwrap_or_else(|e| {
25-
panic!("Unable to open configuration file [default.toml] {}",e)
26-
});
27-
let mut def_config = String::new();
28-
def_config_file.read_to_string(&mut def_config).unwrap();
29-
let config = Box::new(Config::from_toml(&def_config));
3056
let (args, write_mode) = determine_params(std::env::args());
3157

32-
run(args, write_mode, config);
58+
let config = match lookup_and_read_project_file() {
59+
Ok((path, toml)) => {
60+
println!("Project config file: {}", path.display());
61+
Config::from_toml(&toml)
62+
}
63+
Err(_) => Default::default(),
64+
};
3365

66+
run(args, write_mode, Box::new(config));
3467
std::process::exit(0);
3568
}
3669

src/config.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,30 @@ create_config! {
8181
closure_indent_style: BlockIndentStyle,
8282
single_line_if_else: bool,
8383
}
84+
85+
impl Default for Config {
86+
87+
fn default() -> Config {
88+
Config {
89+
max_width: 100,
90+
ideal_width: 80,
91+
leeway: 5,
92+
tab_spaces: 4,
93+
newline_style: NewlineStyle::Unix,
94+
fn_brace_style: BraceStyle::SameLineWhere,
95+
fn_return_indent: ReturnIndent::WithArgs,
96+
fn_args_paren_newline: true,
97+
struct_trailing_comma: SeparatorTactic::Vertical,
98+
struct_lit_trailing_comma: SeparatorTactic::Vertical,
99+
struct_lit_style: StructLitStyle::BlockIndent,
100+
enum_trailing_comma: true,
101+
report_todo: ReportTactic::Always,
102+
report_fixme: ReportTactic::Never,
103+
reorder_imports: false,
104+
expr_indent_style: BlockIndentStyle::Tabbed,
105+
closure_indent_style: BlockIndentStyle::Visual,
106+
single_line_if_else: false,
107+
}
108+
}
109+
110+
}

src/default.toml

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/system.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,14 @@ pub fn idempotent_check(filename: String) -> Result<(), HashMap<String, String>>
121121

122122
// Reads test config file from comments and reads its contents.
123123
fn get_config(config_file: Option<&str>) -> Box<Config> {
124-
let config_file_name = config_file.map(|file_name| {
125-
let mut full_path = "tests/config/".to_owned();
126-
full_path.push_str(&file_name);
127-
full_path
128-
})
129-
.unwrap_or("default.toml".to_owned());
124+
let config_file_name = match config_file {
125+
None => return Box::new(Default::default()),
126+
Some(file_name) => {
127+
let mut full_path = "tests/config/".to_owned();
128+
full_path.push_str(&file_name);
129+
full_path
130+
}
131+
};
130132

131133
let mut def_config_file = fs::File::open(config_file_name).ok().expect("Couldn't open config.");
132134
let mut def_config = String::new();

0 commit comments

Comments
 (0)