rust/src/bin/rustfmt.rs

180 lines
5.4 KiB
Rust
Raw Normal View History

// 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.
2015-05-03 17:12:39 -05:00
#![cfg(not(test))]
#[macro_use]
extern crate log;
extern crate rustfmt;
extern crate toml;
extern crate env_logger;
extern crate getopts;
use rustfmt::{WriteMode, run, run_from_stdin};
use rustfmt::config::Config;
use std::env;
use std::fs::{self, File};
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use getopts::Options;
2015-05-23 00:02:59 -05:00
/// Rustfmt operations.
enum Operation {
/// Format a file and its child modules.
Format(PathBuf, WriteMode),
/// Print the help message.
Help,
/// Invalid program input, including reason.
InvalidInput(String),
/// No file specified, read from stdin
Stdin(String, WriteMode),
}
/// Try to find a project file in the input file directory and its parents.
fn lookup_project_file(input_file: &Path) -> io::Result<PathBuf> {
let mut current = if input_file.is_relative() {
try!(env::current_dir()).join(input_file)
} else {
input_file.to_path_buf()
};
// FIXME: We should canonize path to properly handle its parents,
// but `canonicalize` function is unstable now (recently added API)
// current = try!(fs::canonicalize(current));
loop {
// If the current directory has no parent, we're done searching.
if !current.pop() {
return Err(io::Error::new(io::ErrorKind::NotFound, "Config not found"));
}
let config_file = current.join("rustfmt.toml");
if fs::metadata(&config_file).is_ok() {
return Ok(config_file);
}
}
}
/// Try to find a project file. If it's found, read it.
fn lookup_and_read_project_file(input_file: &Path) -> io::Result<(PathBuf, String)> {
let path = try!(lookup_project_file(input_file));
let mut file = try!(File::open(&path));
let mut toml = String::new();
try!(file.read_to_string(&mut toml));
Ok((path, toml))
}
fn execute() -> i32 {
let mut opts = Options::new();
opts.optflag("h", "help", "show this message");
opts.optopt("",
"write-mode",
2015-11-03 02:16:33 -06:00
"mode to write in (not usable when piping from stdin)",
"[replace|overwrite|display|diff|coverage]");
let operation = determine_operation(&opts, env::args().skip(1));
2015-05-23 00:02:59 -05:00
match operation {
Operation::InvalidInput(reason) => {
print_usage(&opts, &reason);
1
}
Operation::Help => {
print_usage(&opts, "");
0
}
Operation::Stdin(input, write_mode) => {
// try to read config from local directory
let config = match lookup_and_read_project_file(&Path::new(".")) {
2015-11-04 17:14:55 -06:00
Ok((_, toml)) => {
Config::from_toml(&toml)
}
Err(_) => Default::default(),
};
run_from_stdin(input, write_mode, &config);
0
}
Operation::Format(file, write_mode) => {
let config = match lookup_and_read_project_file(&file) {
Ok((path, toml)) => {
println!("Using rustfmt config file: {}", path.display());
Config::from_toml(&toml)
}
Err(_) => Default::default(),
};
2015-05-23 00:02:59 -05:00
run(&file, write_mode, &config);
0
}
}
}
2015-08-18 14:10:30 -05:00
fn main() {
let _ = env_logger::init();
let exit_code = execute();
// Make sure standard output is flushed before we exit.
std::io::stdout().flush().unwrap();
// Exit with given exit code.
//
// NOTE: This immediately terminates the process without doing any cleanup,
// so make sure to finish all necessary cleanup before this is called.
std::process::exit(exit_code);
}
fn print_usage(opts: &Options, reason: &str) {
let reason = format!("{}\nusage: {} [options] <file>",
reason,
env::current_exe().unwrap().display());
println!("{}", opts.usage(&reason));
2015-09-28 18:38:19 -05:00
Config::print_docs();
}
fn determine_operation<I>(opts: &Options, args: I) -> Operation
2015-08-18 14:10:30 -05:00
where I: Iterator<Item = String>
{
let matches = match opts.parse(args) {
Ok(m) => m,
Err(e) => return Operation::InvalidInput(e.to_string()),
};
if matches.opt_present("h") {
return Operation::Help;
}
// if no file argument is supplied, read from stdin
2015-11-03 02:16:33 -06:00
if matches.free.len() == 0 {
let mut buffer = String::new();
match io::stdin().read_to_string(&mut buffer) {
Ok(..) => (),
Err(e) => return Operation::InvalidInput(e.to_string()),
}
2015-11-03 02:16:33 -06:00
// WriteMode is always plain for Stdin
return Operation::Stdin(buffer, WriteMode::Plain);
}
let write_mode = match matches.opt_str("write-mode") {
Some(mode) => {
match mode.parse() {
Ok(mode) => mode,
Err(..) => return Operation::InvalidInput("Unrecognized write mode".into()),
}
2015-08-18 14:10:30 -05:00
}
None => WriteMode::Replace,
};
Operation::Format(PathBuf::from(&matches.free[0]), write_mode)
2015-08-18 14:10:30 -05:00
}