Skip to content

Commit a20ffc6

Browse files
committed
Add a path to rustfmt.toml to GitIgnore as root path
1 parent f8d3aa5 commit a20ffc6

File tree

2 files changed

+49
-19
lines changed

2 files changed

+49
-19
lines changed

src/config/options.rs

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::collections::{hash_set, HashSet};
2+
use std::fmt;
23
use std::path::{Path, PathBuf};
34

45
use atty;
6+
use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
57

68
use crate::config::config_type::ConfigType;
79
use crate::config::lists::*;
@@ -396,33 +398,63 @@ impl Default for EmitMode {
396398
}
397399

398400
/// A set of directories, files and modules that rustfmt should ignore.
399-
#[derive(Default, Deserialize, Serialize, Clone, Debug, PartialEq)]
400-
pub struct IgnoreList(HashSet<PathBuf>);
401+
#[derive(Default, Serialize, Clone, Debug, PartialEq)]
402+
pub struct IgnoreList {
403+
/// A set of path specified in rustfmt.toml.
404+
#[serde(flatten)]
405+
path_set: HashSet<PathBuf>,
406+
/// A path to rustfmt.toml.
407+
#[serde(skip_serializing)]
408+
rustfmt_toml_path: PathBuf,
409+
}
410+
411+
impl<'de> Deserialize<'de> for IgnoreList {
412+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
413+
where
414+
D: Deserializer<'de>,
415+
{
416+
struct HashSetVisitor;
417+
impl<'v> Visitor<'v> for HashSetVisitor {
418+
type Value = HashSet<PathBuf>;
419+
420+
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
421+
formatter.write_str("a sequence of path")
422+
}
423+
424+
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
425+
where
426+
A: SeqAccess<'v>,
427+
{
428+
let mut path_set = HashSet::new();
429+
while let Some(elem) = seq.next_element()? {
430+
path_set.insert(elem);
431+
}
432+
Ok(path_set)
433+
}
434+
}
435+
Ok(IgnoreList {
436+
path_set: deserializer.deserialize_seq(HashSetVisitor)?,
437+
rustfmt_toml_path: PathBuf::new(),
438+
})
439+
}
440+
}
401441

402442
impl<'a> IntoIterator for &'a IgnoreList {
403443
type Item = &'a PathBuf;
404444
type IntoIter = hash_set::Iter<'a, PathBuf>;
405445

406446
fn into_iter(self) -> Self::IntoIter {
407-
self.0.iter()
447+
self.path_set.iter()
408448
}
409449
}
410450

411451
impl IgnoreList {
412452
pub fn add_prefix(&mut self, dir: &Path) {
413-
self.0 = self
414-
.0
415-
.iter()
416-
.map(|s| {
417-
if s.has_root() {
418-
s.clone()
419-
} else {
420-
let mut path = PathBuf::from(dir);
421-
path.push(s);
422-
path
423-
}
424-
})
425-
.collect();
453+
self.rustfmt_toml_path = dir.to_path_buf();
454+
}
455+
456+
pub fn rustfmt_toml_path(&self) -> &Path {
457+
&self.rustfmt_toml_path
426458
}
427459
}
428460

src/ignore_path.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::path::PathBuf;
2-
31
use ignore::{self, gitignore};
42

53
use crate::config::{FileName, IgnoreList};
@@ -10,7 +8,7 @@ pub struct IgnorePathSet {
108

119
impl IgnorePathSet {
1210
pub fn from_ignore_list(ignore_list: &IgnoreList) -> Result<Self, ignore::Error> {
13-
let mut ignore_builder = gitignore::GitignoreBuilder::new(PathBuf::from(""));
11+
let mut ignore_builder = gitignore::GitignoreBuilder::new(ignore_list.rustfmt_toml_path());
1412

1513
for ignore_path in ignore_list {
1614
ignore_builder.add_line(None, ignore_path.to_str().unwrap())?;

0 commit comments

Comments
 (0)