1
1
use std:: collections:: BTreeMap ;
2
- use std:: collections:: HashMap ;
3
2
4
3
use util:: { CargoResult , ProcessBuilder } ;
5
4
@@ -11,60 +10,56 @@ enum LintKind {
11
10
Forbid ,
12
11
}
13
12
13
+ impl LintKind {
14
+ pub fn try_from_string ( lint_state : & str ) -> Option < LintKind > {
15
+ match lint_state. as_ref ( ) {
16
+ "allow" => Some ( LintKind :: Allow ) ,
17
+ "warn" => Some ( LintKind :: Warn ) ,
18
+ "deny" => Some ( LintKind :: Deny ) ,
19
+ "forbid" => Some ( LintKind :: Forbid ) ,
20
+ _ => None ,
21
+ }
22
+ }
23
+
24
+ pub fn flag ( & self ) -> char {
25
+ match self {
26
+ LintKind :: Allow => 'A' ,
27
+ LintKind :: Warn => 'W' ,
28
+ LintKind :: Deny => 'D' ,
29
+ LintKind :: Forbid => 'F' ,
30
+ }
31
+ }
32
+ }
33
+
14
34
#[ derive( Clone , Debug ) ]
15
35
pub struct Lints {
16
- lints : HashMap < String , LintKind > ,
36
+ lints : Vec < ( String , LintKind ) > ,
17
37
}
18
38
19
39
impl Lints {
20
40
pub fn new (
21
41
manifest_lints : Option < & BTreeMap < String , String > > ,
22
42
warnings : & mut Vec < String > ,
23
43
) -> CargoResult < Lints > {
24
- let mut lints = HashMap :: new ( ) ;
44
+ let mut lints = vec ! [ ] ;
25
45
if let Some ( lint_section) = manifest_lints {
26
46
for ( lint_name, lint_state) in lint_section. iter ( ) {
27
- match lint_state. as_ref ( ) {
28
- "allow" => { lints. insert ( lint_name. to_string ( ) , LintKind :: Allow ) ; } ,
29
- "warn" => { lints. insert ( lint_name. to_string ( ) , LintKind :: Warn ) ; } ,
30
- "deny" => { lints. insert ( lint_name. to_string ( ) , LintKind :: Deny ) ; } ,
31
- "forbid" => { lints. insert ( lint_name. to_string ( ) , LintKind :: Forbid ) ; } ,
32
- _ => warnings. push ( format ! (
47
+ if let Some ( state) = LintKind :: try_from_string ( lint_state) {
48
+ lints. push ( ( lint_name. to_string ( ) , state) ) ;
49
+ } else {
50
+ warnings. push ( format ! (
33
51
"invalid lint state for `{}` (expected `warn`, `allow`, `deny` or `forbid`)" ,
34
52
lint_name
35
- ) ) ,
53
+ ) ) ;
36
54
}
37
55
}
38
56
}
39
57
Ok ( Lints { lints } )
40
58
}
41
59
42
- pub fn set_flags ( & self , cmd : & mut ProcessBuilder , package_lints : & Lints ) {
43
- let get_kind = |kind : LintKind | {
44
- self . lints . iter ( )
45
- . filter ( |l| * l. 1 == kind)
46
- . chain ( package_lints. lints . iter ( )
47
- . filter ( |l| * l. 1 == kind && !self . lints . contains_key ( l. 0 ) ) )
48
- . map ( |l| l. 0 . to_string ( ) )
49
- . collect :: < Vec < String > > ( )
50
- . join ( "," )
51
- } ;
52
-
53
- let allow = get_kind ( LintKind :: Allow ) ;
54
- if !allow. is_empty ( ) {
55
- cmd. arg ( "-A" ) . arg ( allow) ;
56
- }
57
- let warn = get_kind ( LintKind :: Warn ) ;
58
- if !warn. is_empty ( ) {
59
- cmd. arg ( "-W" ) . arg ( warn) ;
60
- }
61
- let deny = get_kind ( LintKind :: Deny ) ;
62
- if !deny. is_empty ( ) {
63
- cmd. arg ( "-D" ) . arg ( deny) ;
64
- }
65
- let forbid = get_kind ( LintKind :: Forbid ) ;
66
- if !forbid. is_empty ( ) {
67
- cmd. arg ( "-F" ) . arg ( forbid) ;
60
+ pub fn set_flags ( & self , cmd : & mut ProcessBuilder ) {
61
+ for ( lint_name, state) in self . lints . iter ( ) {
62
+ cmd. arg ( format ! ( "-{}" , state. flag( ) ) ) . arg ( lint_name) ;
68
63
}
69
64
}
70
65
}
0 commit comments