From: topecongiro Date: Sun, 11 Mar 2018 23:41:19 +0000 (+0900) Subject: Simplify IgnoreList X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=3999d64f12d8b211371a8adb9f1ce39f162a60f0;p=rust.git Simplify IgnoreList --- diff --git a/Configurations.md b/Configurations.md index 0b2048958e1..2b9cd13ad33 100644 --- a/Configurations.md +++ b/Configurations.md @@ -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", ] ``` diff --git a/src/config/options.rs b/src/config/options.rs index 7a5f9785b9b..31f7d106d1a 100644 --- a/src/config/options.rs +++ b/src/config/options.rs @@ -252,14 +252,12 @@ fn from_str(_: &str) -> Result { /// A set of directories, files and modules that rustfmt should ignore. #[derive(Default, Deserialize, Serialize, Clone, Debug)] -pub struct IgnoreList { - directories: Option>, - files: Option>, -} +pub struct IgnoreList(HashSet); impl IgnoreList { - fn add_prefix_inner(set: &HashSet, dir: &Path) -> HashSet { - 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, dir: &Path) -> HashSet { 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 }