@@ -83,6 +83,17 @@ pub struct CrateGraph {
83
83
#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
84
84
pub struct CrateId ( pub u32 ) ;
85
85
86
+ pub struct CrateName ( SmolStr ) ;
87
+
88
+ impl < T : AsRef < str > > From < T > for CrateName {
89
+ fn from ( name : T ) -> Self {
90
+ // For root projects with dashes in their name,
91
+ // cargo metadata does not do any normalization
92
+ // so we do it ourselves
93
+ Self ( SmolStr :: new ( name. as_ref ( ) . replace ( '-' , "_" ) ) )
94
+ }
95
+ }
96
+
86
97
#[ derive( Debug , Clone , PartialEq , Eq ) ]
87
98
struct CrateData {
88
99
file_id : FileId ,
@@ -131,13 +142,13 @@ impl CrateGraph {
131
142
pub fn add_dep (
132
143
& mut self ,
133
144
from : CrateId ,
134
- name : SmolStr ,
145
+ name : CrateName ,
135
146
to : CrateId ,
136
147
) -> Result < ( ) , CyclicDependenciesError > {
137
148
if self . dfs_find ( from, to, & mut FxHashSet :: default ( ) ) {
138
149
return Err ( CyclicDependenciesError ) ;
139
150
}
140
- self . arena . get_mut ( & from) . unwrap ( ) . add_dep ( name, to) ;
151
+ self . arena . get_mut ( & from) . unwrap ( ) . add_dep ( name. 0 , to) ;
141
152
Ok ( ( ) )
142
153
}
143
154
@@ -268,7 +279,7 @@ pub struct CyclicDependenciesError;
268
279
269
280
#[ cfg( test) ]
270
281
mod tests {
271
- use super :: { CfgOptions , CrateGraph , Edition :: Edition2018 , Env , FileId , SmolStr } ;
282
+ use super :: { CfgOptions , CrateGraph , Dependency , Edition :: Edition2018 , Env , FileId } ;
272
283
273
284
#[ test]
274
285
fn it_should_panic_because_of_cycle_dependencies ( ) {
@@ -279,9 +290,9 @@ mod tests {
279
290
graph. add_crate_root ( FileId ( 2u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
280
291
let crate3 =
281
292
graph. add_crate_root ( FileId ( 3u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
282
- assert ! ( graph. add_dep( crate1, SmolStr :: new ( "crate2" ) , crate2) . is_ok( ) ) ;
283
- assert ! ( graph. add_dep( crate2, SmolStr :: new ( "crate3" ) , crate3) . is_ok( ) ) ;
284
- assert ! ( graph. add_dep( crate3, SmolStr :: new ( "crate1" ) , crate1) . is_err( ) ) ;
293
+ assert ! ( graph. add_dep( crate1, "crate2" . into ( ) , crate2) . is_ok( ) ) ;
294
+ assert ! ( graph. add_dep( crate2, "crate3" . into ( ) , crate3) . is_ok( ) ) ;
295
+ assert ! ( graph. add_dep( crate3, "crate1" . into ( ) , crate1) . is_err( ) ) ;
285
296
}
286
297
287
298
#[ test]
@@ -293,7 +304,21 @@ mod tests {
293
304
graph. add_crate_root ( FileId ( 2u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
294
305
let crate3 =
295
306
graph. add_crate_root ( FileId ( 3u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
296
- assert ! ( graph. add_dep( crate1, SmolStr :: new( "crate2" ) , crate2) . is_ok( ) ) ;
297
- assert ! ( graph. add_dep( crate2, SmolStr :: new( "crate3" ) , crate3) . is_ok( ) ) ;
307
+ assert ! ( graph. add_dep( crate1, "crate2" . into( ) , crate2) . is_ok( ) ) ;
308
+ assert ! ( graph. add_dep( crate2, "crate3" . into( ) , crate3) . is_ok( ) ) ;
309
+ }
310
+
311
+ #[ test]
312
+ fn dashes_are_normalized ( ) {
313
+ let mut graph = CrateGraph :: default ( ) ;
314
+ let crate1 =
315
+ graph. add_crate_root ( FileId ( 1u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
316
+ let crate2 =
317
+ graph. add_crate_root ( FileId ( 2u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
318
+ assert ! ( graph. add_dep( crate1, "crate-name-with-dashes" . into( ) , crate2) . is_ok( ) ) ;
319
+ assert_eq ! (
320
+ graph. dependencies( crate1) . collect:: <Vec <_>>( ) ,
321
+ vec![ & Dependency { crate_id: crate2, name: "crate_name_with_dashes" . into( ) } ]
322
+ ) ;
298
323
}
299
324
}
0 commit comments