1 use std::{fmt, fs, io};
3 use syntax::{ast, codemap, ptr};
4 use syntax::parse::token;
7 /// Get the configuration file from arguments.
8 pub fn conf_file(args: &[ptr::P<ast::MetaItem>]) -> Result<Option<token::InternedString>, (&'static str, codemap::Span)> {
11 ast::MetaItemKind::Word(ref name) | ast::MetaItemKind::List(ref name, _) => {
12 if name == &"conf_file" {
13 return Err(("`conf_file` must be a named value", arg.span));
16 ast::MetaItemKind::NameValue(ref name, ref value) => {
17 if name == &"conf_file" {
18 return if let ast::LitKind::Str(ref file, _) = value.node {
19 Ok(Some(file.clone()))
21 Err(("`conf_file` value must be a string", value.span))
31 /// Error from reading a configuration file.
35 TomlError(Vec<toml::ParserError>),
36 TypeError(&'static str, &'static str, &'static str),
40 impl fmt::Display for ConfError {
41 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
43 ConfError::IoError(ref err) => err.fmt(f),
44 ConfError::TomlError(ref errs) => {
57 ConfError::TypeError(ref key, ref expected, ref got) => {
58 write!(f, "`{}` is expected to be a `{}` but is a `{}`", key, expected, got)
60 ConfError::UnknownKey(ref key) => write!(f, "unknown key `{}`", key),
65 impl From<io::Error> for ConfError {
66 fn from(e: io::Error) -> Self {
71 macro_rules! define_Conf {
72 ($(#[$doc: meta] ($toml_name: tt, $rust_name: ident, $default: expr => $($ty: tt)+),)+) => {
73 /// Type used to store lint configuration.
75 $(#[$doc] pub $rust_name: define_Conf!(TY $($ty)+),)+
78 impl Default for Conf {
79 fn default() -> Conf {
81 $($rust_name: define_Conf!(DEFAULT $($ty)+, $default),)+
87 /// Set the property `name` (which must be the `toml` name) to the given value
88 #[allow(cast_sign_loss)]
89 fn set(&mut self, name: String, value: toml::Value) -> Result<(), ConfError> {
92 define_Conf!(PAT $toml_name) => {
93 if let Some(value) = define_Conf!(CONV $($ty)+, value) {
94 self.$rust_name = value;
97 return Err(ConfError::TypeError(define_Conf!(EXPR $toml_name),
104 // for external tools such as clippy-service
108 return Err(ConfError::UnknownKey(name));
117 // hack to convert tts
118 (PAT $pat: pat) => { $pat };
119 (EXPR $e: expr) => { $e };
120 (TY $ty: ty) => { $ty };
122 // how to read the value?
123 (CONV i64, $value: expr) => { $value.as_integer() };
124 (CONV u64, $value: expr) => { $value.as_integer().iter().filter_map(|&i| if i >= 0 { Some(i as u64) } else { None }).next() };
125 (CONV String, $value: expr) => { $value.as_str().map(Into::into) };
126 (CONV Vec<String>, $value: expr) => {{
127 let slice = $value.as_slice();
129 if let Some(slice) = slice {
130 if slice.iter().any(|v| v.as_str().is_none()) {
134 Some(slice.iter().map(|v| v.as_str().unwrap_or_else(|| unreachable!()).to_owned()).collect())
142 // provide a nicer syntax to declare the default value of `Vec<String>` variables
143 (DEFAULT Vec<String>, $e: expr) => { $e.iter().map(|&e| e.to_owned()).collect() };
144 (DEFAULT $ty: ty, $e: expr) => { $e };
148 /// Lint: BLACKLISTED_NAME. The list of blacklisted names to lint about
149 ("blacklisted-names", blacklisted_names, ["foo", "bar", "baz"] => Vec<String>),
150 /// Lint: CYCLOMATIC_COMPLEXITY. The maximum cyclomatic complexity a function can have
151 ("cyclomatic-complexity-threshold", cyclomatic_complexity_threshold, 25 => u64),
152 /// Lint: TOO_MANY_ARGUMENTS. The maximum number of argument a function or method can have
153 ("too-many-arguments-threshold", too_many_arguments_threshold, 6 => u64),
154 /// Lint: TYPE_COMPLEXITY. The maximum complexity a type can have
155 ("type-complexity-threshold", type_complexity_threshold, 250 => u64),
158 /// Read the `toml` configuration file. The function will ignore “File not found” errors iif
159 /// `!must_exist`, in which case, it will return the default configuration.
160 pub fn read_conf(path: &str, must_exist: bool) -> Result<Conf, ConfError> {
161 let mut conf = Conf::default();
163 let file = match fs::File::open(path) {
165 let mut buf = String::new();
166 try!(file.read_to_string(&mut buf));
169 Err(ref err) if !must_exist && err.kind() == io::ErrorKind::NotFound => {
173 return Err(err.into());
177 let mut parser = toml::Parser::new(&file);
178 let toml = if let Some(toml) = parser.parse() {
181 return Err(ConfError::TomlError(parser.errors));
184 for (key, value) in toml {
185 try!(conf.set(key, value));