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) => {
46 ConfError::TomlError(ref errs) => {
59 ConfError::TypeError(ref key, ref expected, ref got) => {
60 write!(f, "`{}` is expected to be a `{}` but is a `{}`", key, expected, got)
62 ConfError::UnknownKey(ref key) => {
63 write!(f, "unknown key `{}`", key)
69 impl From<io::Error> for ConfError {
70 fn from(e: io::Error) -> Self {
75 macro_rules! define_Conf {
76 ($(($toml_name: tt, $rust_name: ident, $default: expr, $ty: ident),)+) => {
77 /// Type used to store lint configuration.
79 $(pub $rust_name: $ty,)+
82 impl Default for Conf {
83 fn default() -> Conf {
85 $($rust_name: $default,)+
91 /// Set the property `name` (which must be the `toml` name) to the given value
92 #[allow(cast_sign_loss)]
93 fn set(&mut self, name: String, value: toml::Value) -> Result<(), ConfError> {
96 define_Conf!(PAT $toml_name) => {
97 if let Some(value) = define_Conf!(CONV $ty, value) {
98 self.$rust_name = value;
101 return Err(ConfError::TypeError(define_Conf!(EXPR $toml_name),
108 return Err(ConfError::UnknownKey(name));
117 // hack to convert tts
118 (PAT $pat: pat) => { $pat };
119 (EXPR $e: expr) => { $e };
121 // how to read the value?
122 (CONV i64, $value: expr) => { $value.as_integer() };
123 (CONV u64, $value: expr) => { $value.as_integer().iter().filter_map(|&i| if i >= 0 { Some(i as u64) } else { None }).next() };
124 (CONV String, $value: expr) => { $value.as_str().map(Into::into) };
125 (CONV StringVec, $value: expr) => {{
126 let slice = $value.as_slice();
128 if let Some(slice) = slice {
129 if slice.iter().any(|v| v.as_str().is_none()) {
133 Some(slice.iter().map(|v| v.as_str().unwrap_or_else(|| unreachable!()).to_owned()).collect())
142 /// To keep the `define_Conf!` macro simple
143 pub type StringVec = Vec<String>;
146 ("blacklisted-names", blacklisted_names, vec!["foo".to_owned(), "bar".to_owned(), "baz".to_owned()], StringVec),
147 ("cyclomatic-complexity-threshold", cyclomatic_complexity_threshold, 25, u64),
148 ("too-many-arguments-threshold", too_many_arguments_threshold, 6, u64),
149 ("type-complexity-threshold", type_complexity_threshold, 250, u64),
152 /// Read the `toml` configuration file. The function will ignore “File not found” errors iif
153 /// `!must_exist`, in which case, it will return the default configuration.
154 pub fn read_conf(path: &str, must_exist: bool) -> Result<Conf, ConfError> {
155 let mut conf = Conf::default();
157 let file = match fs::File::open(path) {
159 let mut buf = String::new();
160 try!(file.read_to_string(&mut buf));
163 Err(ref err) if !must_exist && err.kind() == io::ErrorKind::NotFound => {
167 return Err(err.into());
171 let mut parser = toml::Parser::new(&file);
172 let toml = if let Some(toml) = parser.parse() {
176 return Err(ConfError::TomlError(parser.errors));
179 for (key, value) in toml {
180 try!(conf.set(key, value));