2020-06-23 16:58:45 +00:00
|
|
|
//! Defines `Fixture` -- a convenient way to describe the initial state of
|
|
|
|
//! rust-analyzer database from a single string.
|
|
|
|
|
2020-06-23 15:59:56 +00:00
|
|
|
use rustc_hash::FxHashMap;
|
2020-07-30 20:19:58 +00:00
|
|
|
use stdx::{lines_with_ends, split_once, trim_indent};
|
2020-06-23 15:59:56 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-06-23 16:46:56 +00:00
|
|
|
pub struct Fixture {
|
2020-06-23 15:59:56 +00:00
|
|
|
pub path: String,
|
2020-06-23 16:34:50 +00:00
|
|
|
pub text: String,
|
2020-06-24 08:22:02 +00:00
|
|
|
pub krate: Option<String>,
|
2020-06-23 15:59:56 +00:00
|
|
|
pub deps: Vec<String>,
|
2020-06-23 16:56:26 +00:00
|
|
|
pub cfg_atoms: Vec<String>,
|
|
|
|
pub cfg_key_values: Vec<(String, String)>,
|
2020-06-23 15:59:56 +00:00
|
|
|
pub edition: Option<String>,
|
|
|
|
pub env: FxHashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:46:56 +00:00
|
|
|
impl Fixture {
|
|
|
|
/// Parses text which looks like this:
|
|
|
|
///
|
|
|
|
/// ```not_rust
|
|
|
|
/// //- some meta
|
|
|
|
/// line 1
|
|
|
|
/// line 2
|
|
|
|
/// // - other meta
|
|
|
|
/// ```
|
|
|
|
pub fn parse(ra_fixture: &str) -> Vec<Fixture> {
|
2020-06-23 20:27:24 +00:00
|
|
|
let fixture = trim_indent(ra_fixture);
|
2020-06-23 15:59:56 +00:00
|
|
|
|
2020-06-23 16:46:56 +00:00
|
|
|
let mut res: Vec<Fixture> = Vec::new();
|
2020-06-23 20:27:24 +00:00
|
|
|
|
2020-06-23 22:30:34 +00:00
|
|
|
let default = if ra_fixture.contains("//-") { None } else { Some("//- /main.rs") };
|
|
|
|
|
|
|
|
for (ix, line) in default.into_iter().chain(lines_with_ends(&fixture)).enumerate() {
|
2020-06-23 20:27:24 +00:00
|
|
|
if line.contains("//-") {
|
|
|
|
assert!(
|
|
|
|
line.starts_with("//-"),
|
|
|
|
"Metadata line {} has invalid indentation. \
|
|
|
|
All metadata lines need to have the same indentation.\n\
|
|
|
|
The offending line: {:?}",
|
|
|
|
ix,
|
|
|
|
line
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:46:56 +00:00
|
|
|
if line.starts_with("//-") {
|
2020-06-23 19:45:40 +00:00
|
|
|
let meta = Fixture::parse_meta_line(line);
|
2020-06-23 16:46:56 +00:00
|
|
|
res.push(meta)
|
|
|
|
} else if let Some(entry) = res.last_mut() {
|
|
|
|
entry.text.push_str(line);
|
|
|
|
}
|
2020-06-23 15:59:56 +00:00
|
|
|
}
|
2020-06-23 20:27:24 +00:00
|
|
|
|
2020-06-23 16:46:56 +00:00
|
|
|
res
|
2020-06-23 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 16:46:56 +00:00
|
|
|
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
|
2020-06-24 08:22:02 +00:00
|
|
|
fn parse_meta_line(meta: &str) -> Fixture {
|
2020-06-23 18:38:36 +00:00
|
|
|
assert!(meta.starts_with("//-"));
|
|
|
|
let meta = meta["//-".len()..].trim();
|
2020-06-23 16:46:56 +00:00
|
|
|
let components = meta.split_ascii_whitespace().collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let path = components[0].to_string();
|
2020-07-06 21:13:55 +00:00
|
|
|
assert!(path.starts_with('/'));
|
2020-06-23 16:46:56 +00:00
|
|
|
|
|
|
|
let mut krate = None;
|
|
|
|
let mut deps = Vec::new();
|
|
|
|
let mut edition = None;
|
2020-06-23 16:56:26 +00:00
|
|
|
let mut cfg_atoms = Vec::new();
|
|
|
|
let mut cfg_key_values = Vec::new();
|
2020-06-23 16:46:56 +00:00
|
|
|
let mut env = FxHashMap::default();
|
|
|
|
for component in components[1..].iter() {
|
2020-07-30 20:19:58 +00:00
|
|
|
let (key, value) = split_once(component, ':').unwrap();
|
2020-06-23 16:46:56 +00:00
|
|
|
match key {
|
|
|
|
"crate" => krate = Some(value.to_string()),
|
|
|
|
"deps" => deps = value.split(',').map(|it| it.to_string()).collect(),
|
|
|
|
"edition" => edition = Some(value.to_string()),
|
|
|
|
"cfg" => {
|
2020-06-23 16:56:26 +00:00
|
|
|
for entry in value.split(',') {
|
2020-07-30 20:19:58 +00:00
|
|
|
match split_once(entry, '=') {
|
2020-06-23 16:56:26 +00:00
|
|
|
Some((k, v)) => cfg_key_values.push((k.to_string(), v.to_string())),
|
|
|
|
None => cfg_atoms.push(entry.to_string()),
|
2020-06-23 16:46:56 +00:00
|
|
|
}
|
2020-06-23 15:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-23 16:46:56 +00:00
|
|
|
"env" => {
|
|
|
|
for key in value.split(',') {
|
2020-07-30 20:19:58 +00:00
|
|
|
if let Some((k, v)) = split_once(key, '=') {
|
2020-06-23 16:46:56 +00:00
|
|
|
env.insert(k.into(), v.into());
|
|
|
|
}
|
2020-06-23 15:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-23 16:46:56 +00:00
|
|
|
_ => panic!("bad component: {:?}", component),
|
2020-06-23 15:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 15:57:26 +00:00
|
|
|
Fixture { path, text: String::new(), krate, deps, cfg_atoms, cfg_key_values, edition, env }
|
2020-06-23 16:46:56 +00:00
|
|
|
}
|
2020-06-23 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn parse_fixture_checks_further_indented_metadata() {
|
2020-06-23 16:56:26 +00:00
|
|
|
Fixture::parse(
|
2020-06-23 15:59:56 +00:00
|
|
|
r"
|
|
|
|
//- /lib.rs
|
|
|
|
mod bar;
|
|
|
|
|
|
|
|
fn foo() {}
|
|
|
|
//- /bar.rs
|
|
|
|
pub fn baz() {}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fixture_gets_full_meta() {
|
2020-06-23 16:46:56 +00:00
|
|
|
let parsed = Fixture::parse(
|
2020-06-23 15:59:56 +00:00
|
|
|
r"
|
|
|
|
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo
|
|
|
|
mod m;
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_eq!(1, parsed.len());
|
|
|
|
|
2020-06-23 16:34:50 +00:00
|
|
|
let meta = &parsed[0];
|
2020-06-23 20:27:24 +00:00
|
|
|
assert_eq!("mod m;\n", meta.text);
|
2020-06-23 15:59:56 +00:00
|
|
|
|
2020-06-24 08:22:02 +00:00
|
|
|
assert_eq!("foo", meta.krate.as_ref().unwrap());
|
2020-06-23 16:34:50 +00:00
|
|
|
assert_eq!("/lib.rs", meta.path);
|
|
|
|
assert_eq!(2, meta.env.len());
|
2020-06-23 15:59:56 +00:00
|
|
|
}
|