From a60b28fe4cb68ba9b81d9b66d38306a16f56d86b Mon Sep 17 00:00:00 2001 From: Julian Date: Sat, 5 Jul 2025 14:32:01 +0200 Subject: [PATCH 1/2] nice --- crates/pgt_suppressions/src/suppression.rs | 62 ++++++++++++---------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/crates/pgt_suppressions/src/suppression.rs b/crates/pgt_suppressions/src/suppression.rs index 3efc10eb..a34a0860 100644 --- a/crates/pgt_suppressions/src/suppression.rs +++ b/crates/pgt_suppressions/src/suppression.rs @@ -1,5 +1,5 @@ -use pgt_analyse::{RuleCategory, RuleFilter}; -use pgt_diagnostics::{Diagnostic, MessageAndDescription}; +use pgt_analyse::RuleFilter; +use pgt_diagnostics::{Category, Diagnostic, MessageAndDescription}; use pgt_text_size::{TextRange, TextSize}; /// A specialized diagnostic for the typechecker. @@ -27,15 +27,14 @@ pub(crate) enum SuppressionKind { /// Represents the suppressed rule, as written in the suppression comment. /// e.g. `lint/safety/banDropColumn`, or `lint/safety`, or just `lint`. /// The format of a rule specifier string is `(/(/))`. -/// pub(crate) enum RuleSpecifier { - Category(RuleCategory), - Group(RuleCategory, String), - Rule(RuleCategory, String, String), + Category(String), + Group(String, String), + Rule(String, String, String), } impl RuleSpecifier { - pub(crate) fn category(&self) -> &RuleCategory { + pub(crate) fn category(&self) -> &str { match self { RuleSpecifier::Category(rule_category) => rule_category, RuleSpecifier::Group(rule_category, _) => rule_category, @@ -72,26 +71,35 @@ impl RuleSpecifier { } } -impl TryFrom<&str> for RuleSpecifier { - type Error = String; - - fn try_from(specifier_str: &str) -> Result { - let mut specifiers = specifier_str.split('/').map(|s| s.to_string()); - - let rule_category: RuleCategory = specifiers.next().unwrap().try_into()?; +impl From<&Category> for RuleSpecifier { + fn from(category: &Category) -> Self { + let mut specifiers = category.name().split('/').map(|s| s.to_string()); + let category_str = specifiers.next(); let group = specifiers.next(); let rule = specifiers.next(); - match (group, rule) { - (Some(g), Some(r)) => Ok(RuleSpecifier::Rule(rule_category, g, r)), - (Some(g), None) => Ok(RuleSpecifier::Group(rule_category, g)), - (None, None) => Ok(RuleSpecifier::Category(rule_category)), + match (category_str, group, rule) { + (Some(c), Some(g), Some(r)) => RuleSpecifier::Rule(c, g, r), + (Some(c), Some(g), None) => RuleSpecifier::Group(c, g), + (Some(c), None, None) => RuleSpecifier::Category(c), _ => unreachable!(), } } } +impl TryFrom<&str> for RuleSpecifier { + type Error = String; + + fn try_from(specifier_str: &str) -> Result { + let cat = specifier_str + .parse::<&Category>() + .map_err(|_| "Invalid rule.".to_string())?; + + Ok(RuleSpecifier::from(cat)) + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) struct Suppression { pub(crate) suppression_range: TextRange, @@ -235,7 +243,7 @@ mod tests { assert_eq!( suppression.rule_specifier, RuleSpecifier::Rule( - RuleCategory::Lint, + "lint".to_string(), "safety".to_string(), "banDropColumn".to_string() ) @@ -252,7 +260,7 @@ mod tests { assert_eq!(suppression.kind, SuppressionKind::Line); assert_eq!( suppression.rule_specifier, - RuleSpecifier::Group(RuleCategory::Lint, "safety".to_string()) + RuleSpecifier::Group("lint".to_string(), "safety".to_string()) ); assert_eq!(suppression.explanation.as_deref(), Some("explanation")); } @@ -266,7 +274,7 @@ mod tests { assert_eq!(suppression.kind, SuppressionKind::Line); assert_eq!( suppression.rule_specifier, - RuleSpecifier::Category(RuleCategory::Lint) + RuleSpecifier::Category("lint".to_string()) ); } @@ -279,7 +287,7 @@ mod tests { assert_eq!(suppression.kind, SuppressionKind::Line); assert_eq!( suppression.rule_specifier, - RuleSpecifier::Category(RuleCategory::Lint) + RuleSpecifier::Category("lint".to_string()) ); assert_eq!(suppression.explanation.as_deref(), Some("explanation")); } @@ -294,7 +302,7 @@ mod tests { assert_eq!( suppression.rule_specifier, RuleSpecifier::Rule( - RuleCategory::Lint, + "lint".to_string(), "safety".to_string(), "banDropColumn".to_string() ) @@ -312,7 +320,7 @@ mod tests { assert_eq!( suppression.rule_specifier, RuleSpecifier::Rule( - RuleCategory::Lint, + "lint".to_string(), "safety".to_string(), "banDropColumn".to_string() ) @@ -330,7 +338,7 @@ mod tests { assert_eq!( suppression.rule_specifier, RuleSpecifier::Rule( - RuleCategory::Lint, + "lint".to_string(), "safety".to_string(), "banDropColumn".to_string() ) @@ -420,7 +428,7 @@ mod tests { // Group filter disables all rules in that group let spec = RuleSpecifier::Rule( - RuleCategory::Lint, + "lint".to_string(), "safety".to_string(), "banDropColumn".to_string(), ); @@ -428,7 +436,7 @@ mod tests { assert!(spec.is_disabled(&disabled)); let spec2 = RuleSpecifier::Rule( - RuleCategory::Lint, + "lint".to_string(), "safety".to_string(), "banDropColumn".to_string(), ); From 85571f1d9c28e8c36470bcaffa2b4672e4f8d9a7 Mon Sep 17 00:00:00 2001 From: Julian Date: Sat, 5 Jul 2025 14:43:42 +0200 Subject: [PATCH 2/2] ok --- crates/pgt_suppressions/src/suppression.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/pgt_suppressions/src/suppression.rs b/crates/pgt_suppressions/src/suppression.rs index a34a0860..c1d4e5c3 100644 --- a/crates/pgt_suppressions/src/suppression.rs +++ b/crates/pgt_suppressions/src/suppression.rs @@ -27,6 +27,9 @@ pub(crate) enum SuppressionKind { /// Represents the suppressed rule, as written in the suppression comment. /// e.g. `lint/safety/banDropColumn`, or `lint/safety`, or just `lint`. /// The format of a rule specifier string is `(/(/))`. +/// +/// `RuleSpecifier` can only be constructed from a `&str` that matches a valid +/// [pgt_diagnostics::Category]. pub(crate) enum RuleSpecifier { Category(String), Group(String, String),