@@ -49,7 +49,7 @@ use std::sync::Arc;
49
49
50
50
use ra_cfg:: CfgOptions ;
51
51
use rustc_hash:: FxHashMap ;
52
- use test_utils:: { extract_offset, parse_fixture, parse_single_fixture, CURSOR_MARKER } ;
52
+ use test_utils:: { extract_offset, parse_fixture, parse_single_fixture, FixtureMeta , CURSOR_MARKER } ;
53
53
54
54
use crate :: {
55
55
input:: CrateName , CrateGraph , CrateId , Edition , Env , FileId , FilePosition , RelativePathBuf ,
@@ -99,7 +99,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId
99
99
let fixture = parse_single_fixture ( ra_fixture) ;
100
100
101
101
let crate_graph = if let Some ( entry) = fixture {
102
- let meta = match parse_meta ( & entry. meta ) {
102
+ let meta = match ParsedMeta :: from ( & entry. parsed_meta ) {
103
103
ParsedMeta :: File ( it) => it,
104
104
_ => panic ! ( "with_single_file only support file meta" ) ,
105
105
} ;
@@ -156,7 +156,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
156
156
let mut file_position = None ;
157
157
158
158
for entry in fixture. iter ( ) {
159
- let meta = match parse_meta ( & entry. meta ) {
159
+ let meta = match ParsedMeta :: from ( & entry. parsed_meta ) {
160
160
ParsedMeta :: Root { path } => {
161
161
let source_root = std:: mem:: replace ( & mut source_root, SourceRoot :: new_local ( ) ) ;
162
162
db. set_source_root ( source_root_id, Arc :: new ( source_root) ) ;
@@ -244,53 +244,31 @@ struct FileMeta {
244
244
env : Env ,
245
245
}
246
246
247
- //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo)
248
- fn parse_meta ( meta : & str ) -> ParsedMeta {
249
- let components = meta. split_ascii_whitespace ( ) . collect :: < Vec < _ > > ( ) ;
250
-
251
- if components[ 0 ] == "root" {
252
- let path: RelativePathBuf = components[ 1 ] . into ( ) ;
253
- assert ! ( path. starts_with( "/" ) && path. ends_with( "/" ) ) ;
254
- return ParsedMeta :: Root { path } ;
255
- }
256
-
257
- let path: RelativePathBuf = components[ 0 ] . into ( ) ;
258
- assert ! ( path. starts_with( "/" ) ) ;
259
-
260
- let mut krate = None ;
261
- let mut deps = Vec :: new ( ) ;
262
- let mut edition = Edition :: Edition2018 ;
263
- let mut cfg = CfgOptions :: default ( ) ;
264
- let mut env = Env :: default ( ) ;
265
- for component in components[ 1 ..] . iter ( ) {
266
- let ( key, value) = split1 ( component, ':' ) . unwrap ( ) ;
267
- match key {
268
- "crate" => krate = Some ( value. to_string ( ) ) ,
269
- "deps" => deps = value. split ( ',' ) . map ( |it| it. to_string ( ) ) . collect ( ) ,
270
- "edition" => edition = Edition :: from_str ( & value) . unwrap ( ) ,
271
- "cfg" => {
272
- for key in value. split ( ',' ) {
273
- match split1 ( key, '=' ) {
274
- None => cfg. insert_atom ( key. into ( ) ) ,
275
- Some ( ( k, v) ) => cfg. insert_key_value ( k. into ( ) , v. into ( ) ) ,
276
- }
277
- }
247
+ impl From < & FixtureMeta > for ParsedMeta {
248
+ fn from ( meta : & FixtureMeta ) -> Self {
249
+ match meta {
250
+ FixtureMeta :: Root { path } => {
251
+ // `Self::Root` causes a false warning: 'variant is never constructed: `Root` '
252
+ // see https://github.com/rust-lang/rust/issues/69018
253
+ ParsedMeta :: Root { path : path. to_owned ( ) }
278
254
}
279
- "env" => {
280
- for key in value. split ( ',' ) {
281
- if let Some ( ( k, v) ) = split1 ( key, '=' ) {
282
- env. set ( k, v. into ( ) ) ;
255
+ FixtureMeta :: File ( f) => Self :: File ( FileMeta {
256
+ path : f. path . to_owned ( ) . into ( ) ,
257
+ krate : f. krate . to_owned ( ) . into ( ) ,
258
+ deps : f. deps . to_owned ( ) ,
259
+ cfg : f. cfg . to_owned ( ) ,
260
+ edition : f
261
+ . edition
262
+ . as_ref ( )
263
+ . map_or ( Edition :: Edition2018 , |v| Edition :: from_str ( & v) . unwrap ( ) ) ,
264
+ env : {
265
+ let mut env = Env :: default ( ) ;
266
+ for ( k, v) in & f. env {
267
+ env. set ( & k, v. to_owned ( ) ) ;
283
268
}
284
- }
285
- }
286
- _ => panic ! ( "bad component: {:?}" , component ) ,
269
+ env
270
+ } ,
271
+ } ) ,
287
272
}
288
273
}
289
-
290
- ParsedMeta :: File ( FileMeta { path, krate, deps, edition, cfg, env } )
291
- }
292
-
293
- fn split1 ( haystack : & str , delim : char ) -> Option < ( & str , & str ) > {
294
- let idx = haystack. find ( delim) ?;
295
- Some ( ( & haystack[ ..idx] , & haystack[ idx + delim. len_utf8 ( ) ..] ) )
296
274
}
0 commit comments