|
1 | 1 | use std::collections::{hash_set, HashSet}; |
| 2 | +use std::fmt; |
2 | 3 | use std::path::{Path, PathBuf}; |
3 | 4 |
|
4 | 5 | use atty; |
| 6 | +use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor}; |
5 | 7 |
|
6 | 8 | use crate::config::config_type::ConfigType; |
7 | 9 | use crate::config::lists::*; |
@@ -396,33 +398,63 @@ impl Default for EmitMode { |
396 | 398 | } |
397 | 399 |
|
398 | 400 | /// 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 | +} |
401 | 441 |
|
402 | 442 | impl<'a> IntoIterator for &'a IgnoreList { |
403 | 443 | type Item = &'a PathBuf; |
404 | 444 | type IntoIter = hash_set::Iter<'a, PathBuf>; |
405 | 445 |
|
406 | 446 | fn into_iter(self) -> Self::IntoIter { |
407 | | - self.0.iter() |
| 447 | + self.path_set.iter() |
408 | 448 | } |
409 | 449 | } |
410 | 450 |
|
411 | 451 | impl IgnoreList { |
412 | 452 | 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 |
426 | 458 | } |
427 | 459 | } |
428 | 460 |
|
|
0 commit comments