From 9ead47151e22f68fcbd0fe5194cbd09cd2482680 Mon Sep 17 00:00:00 2001 From: Simon Bernier St-Pierre Date: Fri, 31 Jul 2015 19:21:44 -0400 Subject: [PATCH] Add project-specific configuration file support --- Cargo.toml | 1 - build.rs | 25 ---------------------- src/bin/rustfmt.rs | 53 +++++++++++++++++++++++++++++++++++++--------- src/config.rs | 27 +++++++++++++++++++++++ tests/system.rs | 14 ++++++------ 5 files changed, 78 insertions(+), 42 deletions(-) delete mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index 57c47fec54cd..a71d4abae868 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/build.rs b/build.rs deleted file mode 100644 index 3e6d051d9b1d..000000000000 --- a/build.rs +++ /dev/null @@ -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 or the MIT license -// , 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(); -} diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index 6ec0f9556c8b..2f61e788ed22 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -7,30 +7,63 @@ // , 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 { + 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); } diff --git a/src/config.rs b/src/config.rs index 1af346265ded..02ee8e637893 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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, + } + } + +} diff --git a/tests/system.rs b/tests/system.rs index 3f63d2245769..19e98a648892 100644 --- a/tests/system.rs +++ b/tests/system.rs @@ -121,12 +121,14 @@ pub fn idempotent_check(filename: String) -> Result<(), HashMap> // Reads test config file from comments and reads its contents. fn get_config(config_file: Option<&str>) -> Box { - 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();