@@ -19,6 +19,7 @@ use cargo_util::{paths, ProcessBuilder};
19
19
use serde:: { Deserialize , Serialize } ;
20
20
use std:: cell:: RefCell ;
21
21
use std:: collections:: hash_map:: { Entry , HashMap } ;
22
+ use std:: collections:: HashSet ;
22
23
use std:: path:: { Path , PathBuf } ;
23
24
use std:: rc:: Rc ;
24
25
use std:: str:: { self , FromStr } ;
@@ -56,6 +57,80 @@ pub struct TargetInfo {
56
57
pub rustdocflags : Rc < [ String ] > ,
57
58
}
58
59
60
+ /// Check Config (aka `--print=check-cfg` representation)
61
+ #[ derive( Debug , Default , Clone ) ]
62
+ pub struct CheckCfg {
63
+ /// Is --check-cfg activated
64
+ pub exhaustive : bool ,
65
+ /// List of expected cfgs
66
+ pub expecteds : HashMap < String , ExpectedValues < String > > ,
67
+ }
68
+
69
+ /// List of expected check-cfg values
70
+ #[ derive( Debug , Clone ) ]
71
+ pub enum ExpectedValues < T > {
72
+ /// List of expected values
73
+ Some ( HashSet < Option < T > > ) ,
74
+ /// All values expected
75
+ Any ,
76
+ }
77
+
78
+ impl CheckCfg {
79
+ fn process_line ( & mut self , line : & str ) {
80
+ if line == "any()=any()" || line == "any()" {
81
+ self . exhaustive = false ;
82
+ } else {
83
+ if let Some ( ( name, val) ) = line. split_once ( '=' ) {
84
+ let name = name. to_string ( ) ;
85
+
86
+ // strip "" around the value, e.g. "linux" -> linux
87
+ let val = val
88
+ . strip_prefix ( '"' )
89
+ . map ( |v| v. strip_suffix ( '"' ) )
90
+ . flatten ( )
91
+ . unwrap_or ( val) ;
92
+
93
+ self . expecteds
94
+ . entry ( name)
95
+ . and_modify ( |v| match v {
96
+ ExpectedValues :: Some ( _) if val. is_empty ( ) => { }
97
+ ExpectedValues :: Some ( _) if val == "any()" => {
98
+ * v = ExpectedValues :: Any ;
99
+ }
100
+ ExpectedValues :: Some ( v) => {
101
+ v. insert ( Some ( val. to_string ( ) ) ) ;
102
+ }
103
+ ExpectedValues :: Any => { }
104
+ } )
105
+ . or_insert_with ( || {
106
+ if val. is_empty ( ) {
107
+ // no value to add
108
+ ExpectedValues :: Some ( Default :: default ( ) )
109
+ } else if val == "any()" {
110
+ ExpectedValues :: Any
111
+ } else {
112
+ let values = HashSet :: from ( [ Some ( val. to_string ( ) ) ] ) ;
113
+ ExpectedValues :: Some ( values)
114
+ }
115
+ } ) ;
116
+ } else {
117
+ let cfg = line. to_string ( ) ;
118
+
119
+ self . expecteds
120
+ . entry ( cfg)
121
+ . and_modify ( |v| match v {
122
+ ExpectedValues :: Some ( v) => v. extend ( None ) ,
123
+ ExpectedValues :: Any => { }
124
+ } )
125
+ . or_insert_with ( || {
126
+ let values = HashSet :: from ( [ None ] ) ;
127
+ ExpectedValues :: Some ( values)
128
+ } ) ;
129
+ }
130
+ }
131
+ }
132
+ }
133
+
59
134
/// Kind of each file generated by a Unit, part of `FileType`.
60
135
#[ derive( Clone , PartialEq , Eq , Debug ) ]
61
136
pub enum FileFlavor {
0 commit comments