Add a path to rustfmt.toml to GitIgnore as root path

This commit is contained in:
topecongiro 2019-04-24 20:22:35 +09:00
parent f8d3aa5908
commit a20ffc6d52
2 changed files with 49 additions and 19 deletions

View File

@ -1,7 +1,9 @@
use std::collections::{hash_set, HashSet};
use std::fmt;
use std::path::{Path, PathBuf};
use atty;
use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
use crate::config::config_type::ConfigType;
use crate::config::lists::*;
@ -396,33 +398,63 @@ impl Default for EmitMode {
}
/// A set of directories, files and modules that rustfmt should ignore.
#[derive(Default, Deserialize, Serialize, Clone, Debug, PartialEq)]
pub struct IgnoreList(HashSet<PathBuf>);
#[derive(Default, Serialize, Clone, Debug, PartialEq)]
pub struct IgnoreList {
/// A set of path specified in rustfmt.toml.
#[serde(flatten)]
path_set: HashSet<PathBuf>,
/// A path to rustfmt.toml.
#[serde(skip_serializing)]
rustfmt_toml_path: PathBuf,
}
impl<'de> Deserialize<'de> for IgnoreList {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct HashSetVisitor;
impl<'v> Visitor<'v> for HashSetVisitor {
type Value = HashSet<PathBuf>;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a sequence of path")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'v>,
{
let mut path_set = HashSet::new();
while let Some(elem) = seq.next_element()? {
path_set.insert(elem);
}
Ok(path_set)
}
}
Ok(IgnoreList {
path_set: deserializer.deserialize_seq(HashSetVisitor)?,
rustfmt_toml_path: PathBuf::new(),
})
}
}
impl<'a> IntoIterator for &'a IgnoreList {
type Item = &'a PathBuf;
type IntoIter = hash_set::Iter<'a, PathBuf>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
self.path_set.iter()
}
}
impl IgnoreList {
pub fn add_prefix(&mut self, dir: &Path) {
self.0 = self
.0
.iter()
.map(|s| {
if s.has_root() {
s.clone()
} else {
let mut path = PathBuf::from(dir);
path.push(s);
path
}
})
.collect();
self.rustfmt_toml_path = dir.to_path_buf();
}
pub fn rustfmt_toml_path(&self) -> &Path {
&self.rustfmt_toml_path
}
}

View File

@ -1,5 +1,3 @@
use std::path::PathBuf;
use ignore::{self, gitignore};
use crate::config::{FileName, IgnoreList};
@ -10,7 +8,7 @@ pub struct IgnorePathSet {
impl IgnorePathSet {
pub fn from_ignore_list(ignore_list: &IgnoreList) -> Result<Self, ignore::Error> {
let mut ignore_builder = gitignore::GitignoreBuilder::new(PathBuf::from(""));
let mut ignore_builder = gitignore::GitignoreBuilder::new(ignore_list.rustfmt_toml_path());
for ignore_path in ignore_list {
ignore_builder.add_line(None, ignore_path.to_str().unwrap())?;