]> git.lizzy.rs Git - rust.git/blobdiff - src/config/file_lines.rs
add new flag to list names of misformatted files (#3747)
[rust.git] / src / config / file_lines.rs
index f52f6dab3395d9622964bcbddb0debd1e5d68648..f0dc6c66597033b7a0a6fb8b03729668beee9e5f 100644 (file)
@@ -1,5 +1,6 @@
 //! This module contains types and functions to support formatting specific line ranges.
 
+use itertools::Itertools;
 use std::collections::HashMap;
 use std::path::PathBuf;
 use std::rc::Rc;
@@ -92,6 +93,12 @@ fn from(range: &'a LineRange) -> Range {
     }
 }
 
+impl fmt::Display for Range {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "{}..{}", self.lo, self.hi)
+    }
+}
+
 impl Range {
     pub fn new(lo: usize, hi: usize) -> Range {
         Range { lo, hi }
@@ -149,6 +156,21 @@ fn merge(self, other: Range) -> Option<Range> {
 #[derive(Clone, Debug, Default, PartialEq)]
 pub struct FileLines(Option<HashMap<FileName, Vec<Range>>>);
 
+impl fmt::Display for FileLines {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        match &self.0 {
+            None => write!(f, "None")?,
+            Some(map) => {
+                for (file_name, ranges) in map.iter() {
+                    write!(f, "{}: ", file_name)?;
+                    write!(f, "{}\n", ranges.iter().format(", "))?;
+                }
+            }
+        };
+        Ok(())
+    }
+}
+
 /// Normalizes the ranges so that the invariants for `FileLines` hold: ranges are non-overlapping,
 /// and ordered by their start point.
 fn normalize_ranges(ranges: &mut HashMap<FileName, Vec<Range>>) {