]> git.lizzy.rs Git - rust.git/commitdiff
Simplify IgnoreList
authortopecongiro <seuchida@gmail.com>
Sun, 11 Mar 2018 23:41:19 +0000 (08:41 +0900)
committertopecongiro <seuchida@gmail.com>
Sun, 11 Mar 2018 23:41:19 +0000 (08:41 +0900)
Configurations.md
src/config/options.rs

index 0b2048958e1bc77b702446ba906a4ae61e2dd990..2b9cd13ad3368c012ea174d8c3cdd356fcecf955 100644 (file)
@@ -2149,8 +2149,7 @@ Skip formatting the specified files and directories.
 If you want to ignore specific files, put the following to your config file:
 
 ```toml
-[ignore]
-files = [
+ignore = [
     "src/types.rs",
     "src/foo/bar.rs",
 ]
@@ -2159,8 +2158,7 @@ files = [
 If you want to ignore every file under `examples/`, put the following to your config file:
 
 ```toml
-[ignore]
-directories = [
+ignore [
     "examples",
 ]
 ```
index 7a5f9785b9ba1545e5ea3ddc965b3aebef5dec52..31f7d106d1a3a049a9052bbdddf67057eea7562f 100644 (file)
@@ -252,14 +252,12 @@ fn from_str(_: &str) -> Result<Self, Self::Err> {
 
 /// A set of directories, files and modules that rustfmt should ignore.
 #[derive(Default, Deserialize, Serialize, Clone, Debug)]
-pub struct IgnoreList {
-    directories: Option<HashSet<PathBuf>>,
-    files: Option<HashSet<PathBuf>>,
-}
+pub struct IgnoreList(HashSet<PathBuf>);
 
 impl IgnoreList {
-    fn add_prefix_inner(set: &HashSet<PathBuf>, dir: &Path) -> HashSet<PathBuf> {
-        set.iter()
+    pub fn add_prefix(&mut self, dir: &Path) {
+        self.0 = self.0
+            .iter()
             .map(|s| {
                 if s.has_root() {
                     s.clone()
@@ -269,29 +267,13 @@ fn add_prefix_inner(set: &HashSet<PathBuf>, dir: &Path) -> HashSet<PathBuf> {
                     path
                 }
             })
-            .collect()
-    }
-
-    pub fn add_prefix(&mut self, dir: &Path) {
-        macro add_prefix_inner_with ($($field: ident),* $(,)*) {
-            $(if let Some(set) = self.$field.as_mut() {
-                *set = IgnoreList::add_prefix_inner(set, dir);
-            })*
-        }
-
-        add_prefix_inner_with!(directories, files);
+            .collect();
     }
 
-    fn is_ignore_file(&self, path: &Path) -> bool {
-        self.files.as_ref().map_or(false, |set| set.contains(path))
-    }
-
-    fn is_under_ignore_dir(&self, path: &Path) -> bool {
-        if let Some(ref dirs) = self.directories {
-            for dir in dirs {
-                if path.starts_with(dir) {
-                    return true;
-                }
+    fn skip_file_inner(&self, file: &Path) -> bool {
+        for path in &self.0 {
+            if file.starts_with(path) {
+                return true;
             }
         }
 
@@ -300,7 +282,7 @@ fn is_under_ignore_dir(&self, path: &Path) -> bool {
 
     pub fn skip_file(&self, file: &FileName) -> bool {
         if let FileName::Real(ref path) = file {
-            self.is_ignore_file(path) || self.is_under_ignore_dir(path)
+            self.skip_file_inner(path)
         } else {
             false
         }