From 549d750615cd99c4bef78b84f0ad9e532290c920 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 2 Jun 2024 14:17:38 -0400 Subject: [PATCH] Use rule name rather than message in --statistics --- crates/ruff/src/printer.rs | 47 ++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/crates/ruff/src/printer.rs b/crates/ruff/src/printer.rs index c44150602bdcee..1c3c00a7f46bb0 100644 --- a/crates/ruff/src/printer.rs +++ b/crates/ruff/src/printer.rs @@ -35,9 +35,9 @@ bitflags! { } #[derive(Serialize)] -struct ExpandedStatistics<'a> { +struct ExpandedStatistics { code: SerializeRuleAsCode, - message: &'a str, + name: SerializeRuleAsTitle, count: usize, fixable: bool, } @@ -65,6 +65,29 @@ impl From for SerializeRuleAsCode { } } +struct SerializeRuleAsTitle(Rule); + +impl Serialize for SerializeRuleAsTitle { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: serde::Serializer, + { + serializer.serialize_str(self.0.as_ref()) + } +} + +impl Display for SerializeRuleAsTitle { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0.as_ref()) + } +} + +impl From for SerializeRuleAsTitle { + fn from(rule: Rule) -> Self { + Self(rule) + } +} + pub(crate) struct Printer { format: SerializationFormat, log_level: LogLevel, @@ -313,29 +336,23 @@ impl Printer { let statistics: Vec = diagnostics .messages .iter() - .map(|message| { - ( - message.kind.rule(), - &message.kind.body, - message.fix.is_some(), - ) - }) + .map(|message| (message.kind.rule(), message.fix.is_some())) .sorted() - .fold(vec![], |mut acc, (rule, body, fixable)| { - if let Some((prev_rule, _, _, count)) = acc.last_mut() { + .fold(vec![], |mut acc, (rule, fixable)| { + if let Some((prev_rule, _, count)) = acc.last_mut() { if *prev_rule == rule { *count += 1; return acc; } } - acc.push((rule, body, fixable, 1)); + acc.push((rule, fixable, 1)); acc }) .iter() - .map(|(rule, message, fixable, count)| ExpandedStatistics { + .map(|(rule, fixable, count)| ExpandedStatistics { code: (*rule).into(), + name: (*rule).into(), count: *count, - message, fixable: *fixable, }) .sorted_by_key(|statistic| Reverse(statistic.count)) @@ -384,7 +401,7 @@ impl Printer { } else { "" }, - statistic.message, + statistic.name, )?; } return Ok(());