2016-02-21 19:11:32 +00:00
|
|
|
use std::{fmt, fs, io};
|
|
|
|
use std::io::Read;
|
|
|
|
use syntax::{ast, codemap, ptr};
|
|
|
|
use syntax::parse::token;
|
|
|
|
use toml;
|
|
|
|
|
|
|
|
/// Get the configuration file from arguments.
|
|
|
|
pub fn conf_file(args: &[ptr::P<ast::MetaItem>]) -> Result<Option<token::InternedString>, (&'static str, codemap::Span)> {
|
|
|
|
for arg in args {
|
|
|
|
match arg.node {
|
|
|
|
ast::MetaItemKind::Word(ref name) | ast::MetaItemKind::List(ref name, _) => {
|
|
|
|
if name == &"conf_file" {
|
|
|
|
return Err(("`conf_file` must be a named value", arg.span));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::MetaItemKind::NameValue(ref name, ref value) => {
|
|
|
|
if name == &"conf_file" {
|
|
|
|
return if let ast::LitKind::Str(ref file, _) = value.node {
|
|
|
|
Ok(Some(file.clone()))
|
|
|
|
} else {
|
|
|
|
Err(("`conf_file` value must be a string", value.span))
|
2016-02-29 16:48:20 +00:00
|
|
|
};
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Error from reading a configuration file.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ConfError {
|
|
|
|
IoError(io::Error),
|
|
|
|
TomlError(Vec<toml::ParserError>),
|
|
|
|
TypeError(&'static str, &'static str, &'static str),
|
|
|
|
UnknownKey(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ConfError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
match *self {
|
2016-02-29 16:48:20 +00:00
|
|
|
ConfError::IoError(ref err) => err.fmt(f),
|
2016-02-21 19:11:32 +00:00
|
|
|
ConfError::TomlError(ref errs) => {
|
|
|
|
let mut first = true;
|
|
|
|
for err in errs {
|
|
|
|
if !first {
|
|
|
|
try!(", ".fmt(f));
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try!(err.fmt(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
ConfError::TypeError(ref key, ref expected, ref got) => {
|
|
|
|
write!(f, "`{}` is expected to be a `{}` but is a `{}`", key, expected, got)
|
|
|
|
}
|
2016-02-29 16:48:20 +00:00
|
|
|
ConfError::UnknownKey(ref key) => write!(f, "unknown key `{}`", key),
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for ConfError {
|
|
|
|
fn from(e: io::Error) -> Self {
|
|
|
|
ConfError::IoError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! define_Conf {
|
2016-02-22 13:25:51 +00:00
|
|
|
($(#[$doc: meta] ($toml_name: tt, $rust_name: ident, $default: expr => $($ty: tt)+),)+) => {
|
2016-02-21 19:11:32 +00:00
|
|
|
/// Type used to store lint configuration.
|
|
|
|
pub struct Conf {
|
2016-02-22 13:25:51 +00:00
|
|
|
$(#[$doc] pub $rust_name: define_Conf!(TY $($ty)+),)+
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Conf {
|
|
|
|
fn default() -> Conf {
|
|
|
|
Conf {
|
2016-02-22 13:25:51 +00:00
|
|
|
$($rust_name: define_Conf!(DEFAULT $($ty)+, $default),)+
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Conf {
|
|
|
|
/// Set the property `name` (which must be the `toml` name) to the given value
|
|
|
|
#[allow(cast_sign_loss)]
|
|
|
|
fn set(&mut self, name: String, value: toml::Value) -> Result<(), ConfError> {
|
|
|
|
match name.as_str() {
|
|
|
|
$(
|
|
|
|
define_Conf!(PAT $toml_name) => {
|
2016-02-22 13:25:51 +00:00
|
|
|
if let Some(value) = define_Conf!(CONV $($ty)+, value) {
|
2016-02-21 19:11:32 +00:00
|
|
|
self.$rust_name = value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Err(ConfError::TypeError(define_Conf!(EXPR $toml_name),
|
2016-02-22 13:25:51 +00:00
|
|
|
stringify!($($ty)+),
|
2016-02-21 19:11:32 +00:00
|
|
|
value.type_str()));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)+
|
2016-03-06 13:40:25 +00:00
|
|
|
"third-party" => {
|
|
|
|
// for external tools such as clippy-service
|
|
|
|
return Ok(());
|
|
|
|
}
|
2016-02-21 19:11:32 +00:00
|
|
|
_ => {
|
|
|
|
return Err(ConfError::UnknownKey(name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// hack to convert tts
|
|
|
|
(PAT $pat: pat) => { $pat };
|
|
|
|
(EXPR $e: expr) => { $e };
|
2016-02-22 13:25:51 +00:00
|
|
|
(TY $ty: ty) => { $ty };
|
2016-02-21 19:11:32 +00:00
|
|
|
|
|
|
|
// how to read the value?
|
|
|
|
(CONV i64, $value: expr) => { $value.as_integer() };
|
|
|
|
(CONV u64, $value: expr) => { $value.as_integer().iter().filter_map(|&i| if i >= 0 { Some(i as u64) } else { None }).next() };
|
|
|
|
(CONV String, $value: expr) => { $value.as_str().map(Into::into) };
|
2016-02-22 13:25:51 +00:00
|
|
|
(CONV Vec<String>, $value: expr) => {{
|
2016-02-21 19:11:32 +00:00
|
|
|
let slice = $value.as_slice();
|
|
|
|
|
|
|
|
if let Some(slice) = slice {
|
|
|
|
if slice.iter().any(|v| v.as_str().is_none()) {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Some(slice.iter().map(|v| v.as_str().unwrap_or_else(|| unreachable!()).to_owned()).collect())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
2016-02-22 13:25:51 +00:00
|
|
|
// provide a nicer syntax to declare the default value of `Vec<String>` variables
|
|
|
|
(DEFAULT Vec<String>, $e: expr) => { $e.iter().map(|&e| e.to_owned()).collect() };
|
|
|
|
(DEFAULT $ty: ty, $e: expr) => { $e };
|
|
|
|
}
|
2016-02-21 19:11:32 +00:00
|
|
|
|
|
|
|
define_Conf! {
|
2016-02-22 13:25:51 +00:00
|
|
|
/// Lint: BLACKLISTED_NAME. The list of blacklisted names to lint about
|
|
|
|
("blacklisted-names", blacklisted_names, ["foo", "bar", "baz"] => Vec<String>),
|
|
|
|
/// Lint: CYCLOMATIC_COMPLEXITY. The maximum cyclomatic complexity a function can have
|
|
|
|
("cyclomatic-complexity-threshold", cyclomatic_complexity_threshold, 25 => u64),
|
|
|
|
/// Lint: TOO_MANY_ARGUMENTS. The maximum number of argument a function or method can have
|
2016-03-08 23:48:10 +00:00
|
|
|
("too-many-arguments-threshold", too_many_arguments_threshold, 7 => u64),
|
2016-02-22 13:25:51 +00:00
|
|
|
/// Lint: TYPE_COMPLEXITY. The maximum complexity a type can have
|
|
|
|
("type-complexity-threshold", type_complexity_threshold, 250 => u64),
|
2016-03-14 13:56:44 +00:00
|
|
|
/// Lint: MANY_SINGLE_CHAR_NAMES. The maximum number of single char bindings a scope may have
|
|
|
|
("single-char-binding-names-threshold", max_single_char_names, 5 => u64),
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Read the `toml` configuration file. The function will ignore “File not found” errors iif
|
|
|
|
/// `!must_exist`, in which case, it will return the default configuration.
|
2016-03-06 14:48:56 +00:00
|
|
|
/// In case of error, the function tries to continue as much as possible.
|
|
|
|
pub fn read_conf(path: &str, must_exist: bool) -> (Conf, Vec<ConfError>) {
|
2016-02-21 19:11:32 +00:00
|
|
|
let mut conf = Conf::default();
|
2016-03-06 14:48:56 +00:00
|
|
|
let mut errors = Vec::new();
|
2016-02-21 19:11:32 +00:00
|
|
|
|
|
|
|
let file = match fs::File::open(path) {
|
|
|
|
Ok(mut file) => {
|
|
|
|
let mut buf = String::new();
|
2016-03-06 14:48:56 +00:00
|
|
|
|
|
|
|
if let Err(err) = file.read_to_string(&mut buf) {
|
|
|
|
errors.push(err.into());
|
|
|
|
return (conf, errors);
|
|
|
|
}
|
|
|
|
|
2016-02-21 19:11:32 +00:00
|
|
|
buf
|
|
|
|
}
|
|
|
|
Err(ref err) if !must_exist && err.kind() == io::ErrorKind::NotFound => {
|
2016-03-06 14:48:56 +00:00
|
|
|
return (conf, errors);
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
Err(err) => {
|
2016-03-06 14:48:56 +00:00
|
|
|
errors.push(err.into());
|
|
|
|
return (conf, errors);
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut parser = toml::Parser::new(&file);
|
|
|
|
let toml = if let Some(toml) = parser.parse() {
|
|
|
|
toml
|
2016-02-29 16:48:20 +00:00
|
|
|
} else {
|
2016-03-06 14:48:56 +00:00
|
|
|
errors.push(ConfError::TomlError(parser.errors));
|
|
|
|
return (conf, errors);
|
2016-02-21 19:11:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (key, value) in toml {
|
2016-03-06 14:48:56 +00:00
|
|
|
if let Err(err) = conf.set(key, value) {
|
|
|
|
errors.push(err);
|
|
|
|
}
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 14:48:56 +00:00
|
|
|
(conf, errors)
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|