From 21f751a0e5da5dd488612e25abfc545c259050e7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 23 Jun 2020 18:34:50 +0200 Subject: [PATCH] Simplify --- crates/ra_db/src/fixture.rs | 12 +++++---- crates/ra_ide/src/mock_analysis.rs | 14 +++++------ .../tests/heavy_tests/support.rs | 2 +- crates/test_utils/src/fixture.rs | 25 +++++++------------ crates/test_utils/src/lib.rs | 2 +- 5 files changed, 24 insertions(+), 31 deletions(-) diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index 7f006487ae..d65536bbc1 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs @@ -61,7 +61,9 @@ use std::{str::FromStr, sync::Arc}; use ra_cfg::CfgOptions; use rustc_hash::FxHashMap; -use test_utils::{extract_offset, parse_fixture, parse_single_fixture, CURSOR_MARKER}; +use test_utils::{ + extract_offset, parse_fixture, parse_single_fixture, FixtureEntry, CURSOR_MARKER, +}; use vfs::{file_set::FileSet, VfsPath}; use crate::{ @@ -112,7 +114,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId let fixture = parse_single_fixture(ra_fixture); let crate_graph = if let Some(entry) = fixture { - let meta = match ParsedMeta::from(&entry.meta) { + let meta = match ParsedMeta::from(&entry) { ParsedMeta::File(it) => it, }; @@ -165,7 +167,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option it, }; assert!(meta.path.starts_with(&source_root_prefix)); @@ -243,8 +245,8 @@ struct FileMeta { env: Env, } -impl From<&test_utils::FileMeta> for ParsedMeta { - fn from(f: &test_utils::FileMeta) -> Self { +impl From<&FixtureEntry> for ParsedMeta { + fn from(f: &FixtureEntry) -> Self { Self::File(FileMeta { path: f.path.to_owned(), krate: f.crate_name.to_owned(), diff --git a/crates/ra_ide/src/mock_analysis.rs b/crates/ra_ide/src/mock_analysis.rs index c0840c6ea7..d480fcf625 100644 --- a/crates/ra_ide/src/mock_analysis.rs +++ b/crates/ra_ide/src/mock_analysis.rs @@ -25,7 +25,7 @@ impl MockFileData { fn path(&self) -> &str { match self { MockFileData::Plain { path, .. } => path.as_str(), - MockFileData::Fixture(f) => f.meta.path.as_str(), + MockFileData::Fixture(f) => f.path.as_str(), } } @@ -38,25 +38,23 @@ impl MockFileData { fn cfg_options(&self) -> CfgOptions { match self { - MockFileData::Fixture(f) => f.meta.cfg.clone(), + MockFileData::Fixture(f) => f.cfg.clone(), _ => CfgOptions::default(), } } fn edition(&self) -> Edition { match self { - MockFileData::Fixture(f) => f - .meta - .edition - .as_ref() - .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()), + MockFileData::Fixture(f) => { + f.edition.as_ref().map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()) + } _ => Edition::Edition2018, } } fn env(&self) -> Env { match self { - MockFileData::Fixture(f) => Env::from(f.meta.env.iter()), + MockFileData::Fixture(f) => Env::from(f.env.iter()), _ => Env::default(), } } diff --git a/crates/rust-analyzer/tests/heavy_tests/support.rs b/crates/rust-analyzer/tests/heavy_tests/support.rs index 3bbfb43aa7..59565bf3d8 100644 --- a/crates/rust-analyzer/tests/heavy_tests/support.rs +++ b/crates/rust-analyzer/tests/heavy_tests/support.rs @@ -69,7 +69,7 @@ impl<'a> Project<'a> { let mut paths = vec![]; for entry in parse_fixture(self.fixture) { - let path = tmp_dir.path().join(&entry.meta.path['/'.len_utf8()..]); + let path = tmp_dir.path().join(&entry.path['/'.len_utf8()..]); fs::create_dir_all(path.parent().unwrap()).unwrap(); fs::write(path.as_path(), entry.text.as_bytes()).unwrap(); paths.push((path, entry.text)); diff --git a/crates/test_utils/src/fixture.rs b/crates/test_utils/src/fixture.rs index a07d057e14..bda826d501 100644 --- a/crates/test_utils/src/fixture.rs +++ b/crates/test_utils/src/fixture.rs @@ -4,13 +4,8 @@ use stdx::split1; #[derive(Debug, Eq, PartialEq)] pub struct FixtureEntry { - pub meta: FileMeta, - pub text: String, -} - -#[derive(Debug, Eq, PartialEq)] -pub struct FileMeta { pub path: String, + pub text: String, pub crate_name: Option, pub deps: Vec, pub cfg: CfgOptions, @@ -71,7 +66,7 @@ The offending line: {:?}"#, if line.starts_with("//-") { let meta = line["//-".len()..].trim().to_string(); let meta = parse_meta(&meta); - res.push(FixtureEntry { meta, text: String::new() }) + res.push(meta) } else if let Some(entry) = res.last_mut() { entry.text.push_str(line); entry.text.push('\n'); @@ -81,7 +76,7 @@ The offending line: {:?}"#, } //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo -fn parse_meta(meta: &str) -> FileMeta { +fn parse_meta(meta: &str) -> FixtureEntry { let components = meta.split_ascii_whitespace().collect::>(); let path = components[0].to_string(); @@ -117,7 +112,7 @@ fn parse_meta(meta: &str) -> FileMeta { } } - FileMeta { path, crate_name: krate, deps, edition, cfg, env } + FixtureEntry { path, text: String::new(), crate_name: krate, deps, edition, cfg, env } } /// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines. @@ -209,12 +204,10 @@ fn parse_fixture_gets_full_meta() { ); assert_eq!(1, parsed.len()); - let parsed = &parsed[0]; - assert_eq!("mod m;\n\n", parsed.text); + let meta = &parsed[0]; + assert_eq!("mod m;\n\n", meta.text); - let meta = &parsed.meta; - assert_eq!("foo", meta.crate_name().unwrap()); - assert_eq!("/lib.rs", meta.path()); - assert!(meta.cfg_options().is_some()); - assert_eq!(2, meta.env().count()); + assert_eq!("foo", meta.crate_name.as_ref().unwrap()); + assert_eq!("/lib.rs", meta.path); + assert_eq!(2, meta.env.len()); } diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index f99786606f..0fdd1a36b7 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -22,7 +22,7 @@ pub use difference::Changeset as __Changeset; pub use ra_cfg::CfgOptions; pub use rustc_hash::FxHashMap; -pub use crate::fixture::{parse_fixture, parse_single_fixture, FileMeta, FixtureEntry}; +pub use crate::fixture::{parse_fixture, parse_single_fixture, FixtureEntry}; pub const CURSOR_MARKER: &str = "<|>";