Merge pull request #165 from SBSTP/config

Add project-specific configuration file support.
This commit is contained in:
cassiersg 2015-08-26 22:23:43 +02:00
commit 0957f3101e
6 changed files with 78 additions and 60 deletions

View File

@ -7,7 +7,6 @@ description = "Tool to find and fix Rust formatting issues"
repository = "https://github.com/nick29581/rustfmt"
readme = "README.md"
license = "Apache-2.0/MIT"
build = "build.rs"
[dependencies.strings]
strings = "0.0.1"

View File

@ -1,25 +0,0 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Build script. Just copies default.toml from the src to the target dir.
use std::env;
use std::path::{Path, PathBuf};
fn main() {
let in_file = Path::new("src/default.toml");
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let mut out_file = PathBuf::new();
out_file.push(manifest_dir);
out_file.push("default.toml");
std::fs::copy(in_file, out_file).unwrap();
}

View File

@ -7,30 +7,63 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(path_ext)]
#![feature(rustc_private)]
#![cfg(not(test))]
#![feature(result_expect)]
#[macro_use]
extern crate log;
extern crate rustfmt;
extern crate toml;
use rustfmt::{WriteMode, run};
use rustfmt::config::Config;
use std::fs::File;
use std::io::Read;
use std::env;
use std::fs::{File, PathExt};
use std::io::{self, Read};
use std::path::PathBuf;
use std::str::FromStr;
// Try to find a project file in the current directory and its parents.
fn lookup_project_file() -> io::Result<PathBuf> {
let mut current = try!(env::current_dir());
loop {
let config_file = current.join("rustfmt.toml");
if config_file.exists() {
return Ok(config_file);
} else {
current = match current.parent() {
// if the current directory has no parent, we're done searching
None => return Err(io::Error::new(io::ErrorKind::NotFound, "config not found")),
Some(path) => path.to_path_buf(),
};
}
}
}
// Try to find a project file. If it's found, read it.
fn lookup_and_read_project_file() -> io::Result<(PathBuf, String)> {
let path = try!(lookup_project_file());
let mut file = try!(File::open(&path));
let mut toml = String::new();
try!(file.read_to_string(&mut toml));
Ok((path, toml))
}
fn main() {
let mut def_config_file = File::open("default.toml").unwrap_or_else(|e| {
panic!("Unable to open configuration file [default.toml] {}",e)
});
let mut def_config = String::new();
def_config_file.read_to_string(&mut def_config).unwrap();
let config = Box::new(Config::from_toml(&def_config));
let (args, write_mode) = determine_params(std::env::args());
run(args, write_mode, config);
let config = match lookup_and_read_project_file() {
Ok((path, toml)) => {
println!("Project config file: {}", path.display());
Config::from_toml(&toml)
}
Err(_) => Default::default(),
};
run(args, write_mode, Box::new(config));
std::process::exit(0);
}

View File

@ -81,3 +81,30 @@ create_config! {
closure_indent_style: BlockIndentStyle,
single_line_if_else: bool,
}
impl Default for Config {
fn default() -> Config {
Config {
max_width: 100,
ideal_width: 80,
leeway: 5,
tab_spaces: 4,
newline_style: NewlineStyle::Unix,
fn_brace_style: BraceStyle::SameLineWhere,
fn_return_indent: ReturnIndent::WithArgs,
fn_args_paren_newline: true,
struct_trailing_comma: SeparatorTactic::Vertical,
struct_lit_trailing_comma: SeparatorTactic::Vertical,
struct_lit_style: StructLitStyle::BlockIndent,
enum_trailing_comma: true,
report_todo: ReportTactic::Always,
report_fixme: ReportTactic::Never,
reorder_imports: false,
expr_indent_style: BlockIndentStyle::Tabbed,
closure_indent_style: BlockIndentStyle::Visual,
single_line_if_else: false,
}
}
}

View File

@ -1,18 +0,0 @@
max_width = 100
ideal_width = 80
leeway = 5
tab_spaces = 4
newline_style = "Unix"
fn_brace_style = "SameLineWhere"
fn_return_indent = "WithArgs"
fn_args_paren_newline = true
struct_trailing_comma = "Vertical"
struct_lit_style = "BlockIndent"
struct_lit_trailing_comma = "Vertical"
enum_trailing_comma = true
report_todo = "Always"
report_fixme = "Never"
reorder_imports = false
expr_indent_style = "Tabbed"
closure_indent_style = "Visual"
single_line_if_else = false

View File

@ -121,12 +121,14 @@ pub fn idempotent_check(filename: String) -> Result<(), HashMap<String, String>>
// Reads test config file from comments and reads its contents.
fn get_config(config_file: Option<&str>) -> Box<Config> {
let config_file_name = config_file.map(|file_name| {
let mut full_path = "tests/config/".to_owned();
full_path.push_str(&file_name);
full_path
})
.unwrap_or("default.toml".to_owned());
let config_file_name = match config_file {
None => return Box::new(Default::default()),
Some(file_name) => {
let mut full_path = "tests/config/".to_owned();
full_path.push_str(&file_name);
full_path
}
};
let mut def_config_file = fs::File::open(config_file_name).ok().expect("Couldn't open config.");
let mut def_config = String::new();