-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7750fae
commit 3740317
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::{collections::HashMap, fmt::Display, path::PathBuf}; | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub struct Entry { | ||
pub line: Option<usize>, | ||
pub text: String, | ||
} | ||
|
||
impl Display for Entry { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!( | ||
f, | ||
"{}{}", | ||
if let Some(line) = self.line.as_ref() { | ||
format!("L{}: ", line) | ||
} else { | ||
String::new() | ||
}, | ||
self.text, | ||
) | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Default, Deserialize, Serialize)] | ||
pub struct Report { | ||
pub entries: HashMap<PathBuf, Vec<Entry>>, | ||
} | ||
|
||
impl Report { | ||
pub fn add_entry<P: Into<PathBuf>, S: Into<String>>( | ||
&mut self, | ||
file: P, | ||
line: Option<usize>, | ||
text: S, | ||
) { | ||
self.entries | ||
.entry(file.into()) | ||
.or_insert_with(|| vec![]) | ||
.push(Entry { | ||
line, | ||
text: text.into(), | ||
}); | ||
} | ||
|
||
pub fn sort_entries(&mut self) { | ||
self.entries.iter_mut().for_each(|(_, entries)| { | ||
entries.sort_by(|a, b| { | ||
a.line | ||
.unwrap_or_else(|| 0) | ||
.cmp(&b.line.unwrap_or_else(|| 0)) | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
impl Display for Report { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
for (i, (path, entries)) in self.entries.iter().enumerate() { | ||
if i > 0 { | ||
writeln!(f)?; | ||
} | ||
|
||
writeln!(f, "{}:", path.to_string_lossy())?; | ||
|
||
for entry in entries.iter() { | ||
writeln!(f, "\t{entry}")?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |