@@ -49,7 +49,7 @@ use std::sync::Arc;
4949
5050use ra_cfg:: CfgOptions ;
5151use 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 } ;
5353
5454use crate :: {
5555 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
9999 let fixture = parse_single_fixture ( ra_fixture) ;
100100
101101 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 ) {
103103 ParsedMeta :: File ( it) => it,
104104 _ => panic ! ( "with_single_file only support file meta" ) ,
105105 } ;
@@ -156,7 +156,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
156156 let mut file_position = None ;
157157
158158 for entry in fixture. iter ( ) {
159- let meta = match parse_meta ( & entry. meta ) {
159+ let meta = match ParsedMeta :: from ( & entry. parsed_meta ) {
160160 ParsedMeta :: Root { path } => {
161161 let source_root = std:: mem:: replace ( & mut source_root, SourceRoot :: new_local ( ) ) ;
162162 db. set_source_root ( source_root_id, Arc :: new ( source_root) ) ;
@@ -244,53 +244,31 @@ struct FileMeta {
244244 env : Env ,
245245}
246246
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 ( ) }
278254 }
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 ( ) ) ;
283268 }
284- }
285- }
286- _ => panic ! ( "bad component: {:?}" , component ) ,
269+ env
270+ } ,
271+ } ) ,
287272 }
288273 }
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 ( ) ..] ) )
296274}
0 commit comments