1- use pgt_analyse:: { RuleCategory , RuleFilter } ;
2- use pgt_diagnostics:: { Diagnostic , MessageAndDescription } ;
1+ use pgt_analyse:: RuleFilter ;
2+ use pgt_diagnostics:: { Category , Diagnostic , MessageAndDescription } ;
33use pgt_text_size:: { TextRange , TextSize } ;
44
55/// A specialized diagnostic for the typechecker.
@@ -28,14 +28,16 @@ pub(crate) enum SuppressionKind {
2828/// e.g. `lint/safety/banDropColumn`, or `lint/safety`, or just `lint`.
2929/// The format of a rule specifier string is `<category>(/<group>(/<rule>))`.
3030///
31+ /// `RuleSpecifier` can only be constructed from a `&str` that matches a valid
32+ /// [pgt_diagnostics::Category].
3133pub ( crate ) enum RuleSpecifier {
32- Category ( RuleCategory ) ,
33- Group ( RuleCategory , String ) ,
34- Rule ( RuleCategory , String , String ) ,
34+ Category ( String ) ,
35+ Group ( String , String ) ,
36+ Rule ( String , String , String ) ,
3537}
3638
3739impl RuleSpecifier {
38- pub ( crate ) fn category ( & self ) -> & RuleCategory {
40+ pub ( crate ) fn category ( & self ) -> & str {
3941 match self {
4042 RuleSpecifier :: Category ( rule_category) => rule_category,
4143 RuleSpecifier :: Group ( rule_category, _) => rule_category,
@@ -72,26 +74,35 @@ impl RuleSpecifier {
7274 }
7375}
7476
75- impl TryFrom < & str > for RuleSpecifier {
76- type Error = String ;
77-
78- fn try_from ( specifier_str : & str ) -> Result < Self , Self :: Error > {
79- let mut specifiers = specifier_str. split ( '/' ) . map ( |s| s. to_string ( ) ) ;
80-
81- let rule_category: RuleCategory = specifiers. next ( ) . unwrap ( ) . try_into ( ) ?;
77+ impl From < & Category > for RuleSpecifier {
78+ fn from ( category : & Category ) -> Self {
79+ let mut specifiers = category. name ( ) . split ( '/' ) . map ( |s| s. to_string ( ) ) ;
8280
81+ let category_str = specifiers. next ( ) ;
8382 let group = specifiers. next ( ) ;
8483 let rule = specifiers. next ( ) ;
8584
86- match ( group, rule) {
87- ( Some ( g) , Some ( r) ) => Ok ( RuleSpecifier :: Rule ( rule_category , g, r) ) ,
88- ( Some ( g) , None ) => Ok ( RuleSpecifier :: Group ( rule_category , g) ) ,
89- ( None , None ) => Ok ( RuleSpecifier :: Category ( rule_category ) ) ,
85+ match ( category_str , group, rule) {
86+ ( Some ( c ) , Some ( g) , Some ( r) ) => RuleSpecifier :: Rule ( c , g, r) ,
87+ ( Some ( c ) , Some ( g) , None ) => RuleSpecifier :: Group ( c , g) ,
88+ ( Some ( c ) , None , None ) => RuleSpecifier :: Category ( c ) ,
9089 _ => unreachable ! ( ) ,
9190 }
9291 }
9392}
9493
94+ impl TryFrom < & str > for RuleSpecifier {
95+ type Error = String ;
96+
97+ fn try_from ( specifier_str : & str ) -> Result < Self , Self :: Error > {
98+ let cat = specifier_str
99+ . parse :: < & Category > ( )
100+ . map_err ( |_| "Invalid rule." . to_string ( ) ) ?;
101+
102+ Ok ( RuleSpecifier :: from ( cat) )
103+ }
104+ }
105+
95106#[ derive( Debug , Clone , PartialEq , Eq ) ]
96107pub ( crate ) struct Suppression {
97108 pub ( crate ) suppression_range : TextRange ,
@@ -235,7 +246,7 @@ mod tests {
235246 assert_eq ! (
236247 suppression. rule_specifier,
237248 RuleSpecifier :: Rule (
238- RuleCategory :: Lint ,
249+ "lint" . to_string ( ) ,
239250 "safety" . to_string( ) ,
240251 "banDropColumn" . to_string( )
241252 )
@@ -252,7 +263,7 @@ mod tests {
252263 assert_eq ! ( suppression. kind, SuppressionKind :: Line ) ;
253264 assert_eq ! (
254265 suppression. rule_specifier,
255- RuleSpecifier :: Group ( RuleCategory :: Lint , "safety" . to_string( ) )
266+ RuleSpecifier :: Group ( "lint" . to_string ( ) , "safety" . to_string( ) )
256267 ) ;
257268 assert_eq ! ( suppression. explanation. as_deref( ) , Some ( "explanation" ) ) ;
258269 }
@@ -266,7 +277,7 @@ mod tests {
266277 assert_eq ! ( suppression. kind, SuppressionKind :: Line ) ;
267278 assert_eq ! (
268279 suppression. rule_specifier,
269- RuleSpecifier :: Category ( RuleCategory :: Lint )
280+ RuleSpecifier :: Category ( "lint" . to_string ( ) )
270281 ) ;
271282 }
272283
@@ -279,7 +290,7 @@ mod tests {
279290 assert_eq ! ( suppression. kind, SuppressionKind :: Line ) ;
280291 assert_eq ! (
281292 suppression. rule_specifier,
282- RuleSpecifier :: Category ( RuleCategory :: Lint )
293+ RuleSpecifier :: Category ( "lint" . to_string ( ) )
283294 ) ;
284295 assert_eq ! ( suppression. explanation. as_deref( ) , Some ( "explanation" ) ) ;
285296 }
@@ -294,7 +305,7 @@ mod tests {
294305 assert_eq ! (
295306 suppression. rule_specifier,
296307 RuleSpecifier :: Rule (
297- RuleCategory :: Lint ,
308+ "lint" . to_string ( ) ,
298309 "safety" . to_string( ) ,
299310 "banDropColumn" . to_string( )
300311 )
@@ -312,7 +323,7 @@ mod tests {
312323 assert_eq ! (
313324 suppression. rule_specifier,
314325 RuleSpecifier :: Rule (
315- RuleCategory :: Lint ,
326+ "lint" . to_string ( ) ,
316327 "safety" . to_string( ) ,
317328 "banDropColumn" . to_string( )
318329 )
@@ -330,7 +341,7 @@ mod tests {
330341 assert_eq ! (
331342 suppression. rule_specifier,
332343 RuleSpecifier :: Rule (
333- RuleCategory :: Lint ,
344+ "lint" . to_string ( ) ,
334345 "safety" . to_string( ) ,
335346 "banDropColumn" . to_string( )
336347 )
@@ -420,15 +431,15 @@ mod tests {
420431
421432 // Group filter disables all rules in that group
422433 let spec = RuleSpecifier :: Rule (
423- RuleCategory :: Lint ,
434+ "lint" . to_string ( ) ,
424435 "safety" . to_string ( ) ,
425436 "banDropColumn" . to_string ( ) ,
426437 ) ;
427438 let disabled = vec ! [ RuleFilter :: Group ( "safety" ) ] ;
428439 assert ! ( spec. is_disabled( & disabled) ) ;
429440
430441 let spec2 = RuleSpecifier :: Rule (
431- RuleCategory :: Lint ,
442+ "lint" . to_string ( ) ,
432443 "safety" . to_string ( ) ,
433444 "banDropColumn" . to_string ( ) ,
434445 ) ;
0 commit comments