From 30748161f0b4699ba9bc699a38ac9fc2fae49461 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 23 Jun 2020 18:20:32 +0200 Subject: Simplify --- crates/ra_db/src/fixture.rs | 30 +++++------ crates/ra_ide/src/mock_analysis.rs | 16 +++--- crates/rust-analyzer/tests/heavy_tests/support.rs | 2 +- crates/test_utils/src/fixture.rs | 62 ++--------------------- crates/test_utils/src/lib.rs | 2 +- 5 files changed, 27 insertions(+), 85 deletions(-) diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index bf897baff..7f006487a 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs @@ -61,7 +61,7 @@ use std::{str::FromStr, sync::Arc}; use ra_cfg::CfgOptions; use rustc_hash::FxHashMap; -use test_utils::{extract_offset, parse_fixture, parse_single_fixture, FixtureMeta, CURSOR_MARKER}; +use test_utils::{extract_offset, parse_fixture, parse_single_fixture, CURSOR_MARKER}; use vfs::{file_set::FileSet, VfsPath}; use crate::{ @@ -243,20 +243,18 @@ struct FileMeta { env: Env, } -impl From<&FixtureMeta> for ParsedMeta { - fn from(meta: &FixtureMeta) -> Self { - match meta { - FixtureMeta::File(f) => Self::File(FileMeta { - path: f.path.to_owned(), - krate: f.crate_name.to_owned(), - deps: f.deps.to_owned(), - cfg: f.cfg.to_owned(), - edition: f - .edition - .as_ref() - .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()), - env: Env::from(f.env.iter()), - }), - } +impl From<&test_utils::FileMeta> for ParsedMeta { + fn from(f: &test_utils::FileMeta) -> Self { + Self::File(FileMeta { + path: f.path.to_owned(), + krate: f.crate_name.to_owned(), + deps: f.deps.to_owned(), + cfg: f.cfg.to_owned(), + edition: f + .edition + .as_ref() + .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()), + env: Env::from(f.env.iter()), + }) } } diff --git a/crates/ra_ide/src/mock_analysis.rs b/crates/ra_ide/src/mock_analysis.rs index 58fafecab..c0840c6ea 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(), + MockFileData::Fixture(f) => f.meta.path.as_str(), } } @@ -38,25 +38,25 @@ impl MockFileData { fn cfg_options(&self) -> CfgOptions { match self { - MockFileData::Fixture(f) => { - f.meta.cfg_options().map_or_else(Default::default, |o| o.clone()) - } + MockFileData::Fixture(f) => f.meta.cfg.clone(), _ => CfgOptions::default(), } } fn edition(&self) -> Edition { match self { - MockFileData::Fixture(f) => { - f.meta.edition().map_or(Edition::Edition2018, |v| Edition::from_str(v).unwrap()) - } + MockFileData::Fixture(f) => f + .meta + .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()), + MockFileData::Fixture(f) => Env::from(f.meta.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 bb8585355..3bbfb43aa 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.meta.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 0dbeb01b1..a07d057e1 100644 --- a/crates/test_utils/src/fixture.rs +++ b/crates/test_utils/src/fixture.rs @@ -4,15 +4,10 @@ use stdx::split1; #[derive(Debug, Eq, PartialEq)] pub struct FixtureEntry { - pub meta: FixtureMeta, + pub meta: FileMeta, pub text: String, } -#[derive(Debug, Eq, PartialEq)] -pub enum FixtureMeta { - File(FileMeta), -} - #[derive(Debug, Eq, PartialEq)] pub struct FileMeta { pub path: String, @@ -23,57 +18,6 @@ pub struct FileMeta { pub env: FxHashMap, } -impl FixtureMeta { - pub fn path(&self) -> &str { - match self { - FixtureMeta::File(f) => &f.path, - } - } - - pub fn crate_name(&self) -> Option<&String> { - match self { - FixtureMeta::File(f) => f.crate_name.as_ref(), - } - } - - pub fn cfg_options(&self) -> Option<&CfgOptions> { - match self { - FixtureMeta::File(f) => Some(&f.cfg), - } - } - - pub fn edition(&self) -> Option<&String> { - match self { - FixtureMeta::File(f) => f.edition.as_ref(), - } - } - - pub fn env(&self) -> impl Iterator { - struct EnvIter<'a> { - iter: Option>, - } - - impl<'a> EnvIter<'a> { - fn new(meta: &'a FixtureMeta) -> Self { - Self { - iter: match meta { - FixtureMeta::File(f) => Some(f.env.iter()), - }, - } - } - } - - impl<'a> Iterator for EnvIter<'a> { - type Item = (&'a String, &'a String); - fn next(&mut self) -> Option { - self.iter.as_mut().and_then(|i| i.next()) - } - } - - EnvIter::new(self) - } -} - /// Same as `parse_fixture`, except it allow empty fixture pub fn parse_single_fixture(ra_fixture: &str) -> Option { if !ra_fixture.lines().any(|it| it.trim_start().starts_with("//-")) { @@ -137,7 +81,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) -> FixtureMeta { +fn parse_meta(meta: &str) -> FileMeta { let components = meta.split_ascii_whitespace().collect::>(); let path = components[0].to_string(); @@ -173,7 +117,7 @@ fn parse_meta(meta: &str) -> FixtureMeta { } } - FixtureMeta::File(FileMeta { path, crate_name: krate, deps, edition, cfg, env }) + FileMeta { path, crate_name: krate, deps, edition, cfg, env } } /// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines. diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index f22fcc8b2..f99786606 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, FixtureEntry, FixtureMeta}; +pub use crate::fixture::{parse_fixture, parse_single_fixture, FileMeta, FixtureEntry}; pub const CURSOR_MARKER: &str = "<|>"; -- cgit v1.2.3