1
- use pgt_analyse:: { RuleCategory , RuleFilter } ;
2
- use pgt_diagnostics:: { Diagnostic , MessageAndDescription } ;
1
+ use pgt_analyse:: RuleFilter ;
2
+ use pgt_diagnostics:: { Category , Diagnostic , MessageAndDescription } ;
3
3
use pgt_text_size:: { TextRange , TextSize } ;
4
4
5
5
/// A specialized diagnostic for the typechecker.
@@ -28,14 +28,16 @@ pub(crate) enum SuppressionKind {
28
28
/// e.g. `lint/safety/banDropColumn`, or `lint/safety`, or just `lint`.
29
29
/// The format of a rule specifier string is `<category>(/<group>(/<rule>))`.
30
30
///
31
+ /// `RuleSpecifier` can only be constructed from a `&str` that matches a valid
32
+ /// [pgt_diagnostics::Category].
31
33
pub ( 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 ) ,
35
37
}
36
38
37
39
impl RuleSpecifier {
38
- pub ( crate ) fn category ( & self ) -> & RuleCategory {
40
+ pub ( crate ) fn category ( & self ) -> & str {
39
41
match self {
40
42
RuleSpecifier :: Category ( rule_category) => rule_category,
41
43
RuleSpecifier :: Group ( rule_category, _) => rule_category,
@@ -72,26 +74,35 @@ impl RuleSpecifier {
72
74
}
73
75
}
74
76
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 ( ) ) ;
82
80
81
+ let category_str = specifiers. next ( ) ;
83
82
let group = specifiers. next ( ) ;
84
83
let rule = specifiers. next ( ) ;
85
84
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 ) ,
90
89
_ => unreachable ! ( ) ,
91
90
}
92
91
}
93
92
}
94
93
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
+
95
106
#[ derive( Debug , Clone , PartialEq , Eq ) ]
96
107
pub ( crate ) struct Suppression {
97
108
pub ( crate ) suppression_range : TextRange ,
@@ -235,7 +246,7 @@ mod tests {
235
246
assert_eq ! (
236
247
suppression. rule_specifier,
237
248
RuleSpecifier :: Rule (
238
- RuleCategory :: Lint ,
249
+ "lint" . to_string ( ) ,
239
250
"safety" . to_string( ) ,
240
251
"banDropColumn" . to_string( )
241
252
)
@@ -252,7 +263,7 @@ mod tests {
252
263
assert_eq ! ( suppression. kind, SuppressionKind :: Line ) ;
253
264
assert_eq ! (
254
265
suppression. rule_specifier,
255
- RuleSpecifier :: Group ( RuleCategory :: Lint , "safety" . to_string( ) )
266
+ RuleSpecifier :: Group ( "lint" . to_string ( ) , "safety" . to_string( ) )
256
267
) ;
257
268
assert_eq ! ( suppression. explanation. as_deref( ) , Some ( "explanation" ) ) ;
258
269
}
@@ -266,7 +277,7 @@ mod tests {
266
277
assert_eq ! ( suppression. kind, SuppressionKind :: Line ) ;
267
278
assert_eq ! (
268
279
suppression. rule_specifier,
269
- RuleSpecifier :: Category ( RuleCategory :: Lint )
280
+ RuleSpecifier :: Category ( "lint" . to_string ( ) )
270
281
) ;
271
282
}
272
283
@@ -279,7 +290,7 @@ mod tests {
279
290
assert_eq ! ( suppression. kind, SuppressionKind :: Line ) ;
280
291
assert_eq ! (
281
292
suppression. rule_specifier,
282
- RuleSpecifier :: Category ( RuleCategory :: Lint )
293
+ RuleSpecifier :: Category ( "lint" . to_string ( ) )
283
294
) ;
284
295
assert_eq ! ( suppression. explanation. as_deref( ) , Some ( "explanation" ) ) ;
285
296
}
@@ -294,7 +305,7 @@ mod tests {
294
305
assert_eq ! (
295
306
suppression. rule_specifier,
296
307
RuleSpecifier :: Rule (
297
- RuleCategory :: Lint ,
308
+ "lint" . to_string ( ) ,
298
309
"safety" . to_string( ) ,
299
310
"banDropColumn" . to_string( )
300
311
)
@@ -312,7 +323,7 @@ mod tests {
312
323
assert_eq ! (
313
324
suppression. rule_specifier,
314
325
RuleSpecifier :: Rule (
315
- RuleCategory :: Lint ,
326
+ "lint" . to_string ( ) ,
316
327
"safety" . to_string( ) ,
317
328
"banDropColumn" . to_string( )
318
329
)
@@ -330,7 +341,7 @@ mod tests {
330
341
assert_eq ! (
331
342
suppression. rule_specifier,
332
343
RuleSpecifier :: Rule (
333
- RuleCategory :: Lint ,
344
+ "lint" . to_string ( ) ,
334
345
"safety" . to_string( ) ,
335
346
"banDropColumn" . to_string( )
336
347
)
@@ -420,15 +431,15 @@ mod tests {
420
431
421
432
// Group filter disables all rules in that group
422
433
let spec = RuleSpecifier :: Rule (
423
- RuleCategory :: Lint ,
434
+ "lint" . to_string ( ) ,
424
435
"safety" . to_string ( ) ,
425
436
"banDropColumn" . to_string ( ) ,
426
437
) ;
427
438
let disabled = vec ! [ RuleFilter :: Group ( "safety" ) ] ;
428
439
assert ! ( spec. is_disabled( & disabled) ) ;
429
440
430
441
let spec2 = RuleSpecifier :: Rule (
431
- RuleCategory :: Lint ,
442
+ "lint" . to_string ( ) ,
432
443
"safety" . to_string ( ) ,
433
444
"banDropColumn" . to_string ( ) ,
434
445
) ;
0 commit comments