Skip to content

Commit d901e0e

Browse files
committed
Reimplement ra_db::fixture::ParsedMeta
in terms of test_utils::FixtureMeta
1 parent eeb9823 commit d901e0e

File tree

2 files changed

+28
-51
lines changed

2 files changed

+28
-51
lines changed

crates/ra_db/src/fixture.rs

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use std::sync::Arc;
4949

5050
use ra_cfg::CfgOptions;
5151
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};
5353

5454
use 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
}

crates/test_utils/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ use std::{
1616

1717
pub use ra_cfg::CfgOptions;
1818

19-
use serde_json::Value;
20-
use text_size::{TextRange, TextSize};
2119
pub use relative_path::{RelativePath, RelativePathBuf};
2220
pub use rustc_hash::FxHashMap;
21+
use serde_json::Value;
22+
use text_size::{TextRange, TextSize};
2323

2424
pub use difference::Changeset as __Changeset;
2525

@@ -292,7 +292,6 @@ fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> {
292292
Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..]))
293293
}
294294

295-
296295
/// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines.
297296
/// This allows fixtures to start off in a different indentation, e.g. to align the first line with
298297
/// the other lines visually:

0 commit comments

Comments
 (0)