]> git.lizzy.rs Git - rust.git/commitdiff
Add configuration variables to wiki
authormcarton <cartonmartin+git@gmail.com>
Mon, 22 Feb 2016 13:25:51 +0000 (14:25 +0100)
committermcarton <cartonmartin+git@gmail.com>
Sat, 12 Mar 2016 13:50:46 +0000 (14:50 +0100)
src/conf.rs
src/lib.rs
util/update_wiki.py

index e275bd701bd5380eabd7adb676c72f70fa62a49b..3b2a1dafad4bb0cd1532b56b3b899d1e639efd1a 100644 (file)
@@ -73,16 +73,16 @@ fn from(e: io::Error) -> Self {
 }
 
 macro_rules! define_Conf {
-    ($(($toml_name: tt, $rust_name: ident, $default: expr, $ty: ident),)+) => {
+    ($(#[$doc: meta] ($toml_name: tt, $rust_name: ident, $default: expr => $($ty: tt)+),)+) => {
         /// Type used to store lint configuration.
         pub struct Conf {
-            $(pub $rust_name: $ty,)+
+            $(#[$doc] pub $rust_name: define_Conf!(TY $($ty)+),)+
         }
 
         impl Default for Conf {
             fn default() -> Conf {
                 Conf {
-                    $($rust_name: $default,)+
+                    $($rust_name: define_Conf!(DEFAULT $($ty)+, $default),)+
                 }
             }
         }
@@ -94,12 +94,12 @@ fn set(&mut self, name: String, value: toml::Value) -> Result<(), ConfError> {
                 match name.as_str() {
                     $(
                         define_Conf!(PAT $toml_name) => {
-                            if let Some(value) = define_Conf!(CONV $ty, value) {
+                            if let Some(value) = define_Conf!(CONV $($ty)+, value) {
                                 self.$rust_name = value;
                             }
                             else {
                                 return Err(ConfError::TypeError(define_Conf!(EXPR $toml_name),
-                                                                stringify!($ty),
+                                                                stringify!($($ty)+),
                                                                 value.type_str()));
                             }
                         },
@@ -117,12 +117,13 @@ fn set(&mut self, name: String, value: toml::Value) -> Result<(), ConfError> {
     // hack to convert tts
     (PAT $pat: pat) => { $pat };
     (EXPR $e: expr) => { $e };
+    (TY $ty: ty) => { $ty };
 
     // 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) };
-    (CONV StringVec, $value: expr) => {{
+    (CONV Vec<String>, $value: expr) => {{
         let slice = $value.as_slice();
 
         if let Some(slice) = slice {
@@ -137,16 +138,21 @@ fn set(&mut self, name: String, value: toml::Value) -> Result<(), ConfError> {
             None
         }
     }};
-}
 
-/// To keep the `define_Conf!` macro simple
-pub type StringVec = Vec<String>;
+    // 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 };
+}
 
 define_Conf! {
-    ("blacklisted-names", blacklisted_names, vec!["foo".to_owned(), "bar".to_owned(), "baz".to_owned()], StringVec),
-    ("cyclomatic-complexity-threshold", cyclomatic_complexity_threshold, 25, u64),
-    ("too-many-arguments-threshold", too_many_arguments_threshold, 6, u64),
-    ("type-complexity-threshold", type_complexity_threshold, 250, u64),
+    /// 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
+    ("too-many-arguments-threshold", too_many_arguments_threshold, 6 => u64),
+    /// Lint: TYPE_COMPLEXITY. The maximum complexity a type can have
+    ("type-complexity-threshold", type_complexity_threshold, 250 => u64),
 }
 
 /// Read the `toml` configuration file. The function will ignore “File not found” errors iif
index 4c0a069a090577c4ec67f0c1f2dd5f86fba6a0a5..b69fdf70c9985e8f967fad1255527ff3719117d1 100644 (file)
@@ -1,3 +1,4 @@
+#![feature(type_macros)]
 #![feature(plugin_registrar, box_syntax)]
 #![feature(rustc_private, collections)]
 #![feature(iter_arith)]
index d3467a410124728eb6c779ea0a9386641eabe399..a10b3549a225525c7c52e6280cc43f7dcaa08ec2 100755 (executable)
@@ -9,6 +9,8 @@ import sys
 
 
 level_re = re.compile(r'''(Forbid|Deny|Warn|Allow)''')
+conf_re = re.compile(r'''define_Conf! {\n([^}]*)\n}''', re.MULTILINE)
+confvar_re = re.compile(r'''/// Lint: (\w+). (.*).*\n *\("([^"]*)", (?:[^,]*), (.*) => (.*)\),''')
 
 
 def parse_path(p="src"):
@@ -16,10 +18,23 @@ def parse_path(p="src"):
     for f in os.listdir(p):
         if f.endswith(".rs"):
             parse_file(d, os.path.join(p, f))
-    return d
+    return (d, parse_conf(p))
 
-START = 0
-LINT = 1
+
+def parse_conf(p):
+    c = {}
+    with open(p + '/conf.rs') as f:
+        f = f.read()
+
+        m = re.search(conf_re, f)
+        m = m.groups()[0]
+
+        m = re.findall(confvar_re, m)
+
+        for (lint, doc, name, default, ty) in m:
+            c[lint.lower()] = (name, ty, doc, default)
+
+    return c
 
 
 def parse_file(d, f):
@@ -85,8 +100,14 @@ template = """\n# `%s`
 
 %s"""
 
+conf_template = """
+**Configuration:** This lint has the following configuration variables:
 
-def write_wiki_page(d, f):
+* `%s: %s`: %s (defaults to `%s`).
+"""
+
+
+def write_wiki_page(d, c, f):
     keys = list(d.keys())
     keys.sort()
     with open(f, "w") as w:
@@ -102,8 +123,11 @@ def write_wiki_page(d, f):
         for k in keys:
             w.write(template % (k, d[k][0], "".join(d[k][1])))
 
+            if k in c:
+                w.write(conf_template % c[k])
+
 
-def check_wiki_page(d, f):
+def check_wiki_page(d, c, f):
     errors = []
     with open(f) as w:
         for line in w:
@@ -122,11 +146,11 @@ def check_wiki_page(d, f):
 
 
 def main():
-    d = parse_path()
+    (d, c) = parse_path()
     if "-c" in sys.argv:
-        check_wiki_page(d, "../rust-clippy.wiki/Home.md")
+        check_wiki_page(d, c, "../rust-clippy.wiki/Home.md")
     else:
-        write_wiki_page(d, "../rust-clippy.wiki/Home.md")
+        write_wiki_page(d, c, "../rust-clippy.wiki/Home.md")
 
 if __name__ == "__main__":
     main()