From e1aa96f2c5b6cdbf0fb7f49b47209055b7a937f2 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 9 Mar 2020 11:26:46 +0200 Subject: Less abstract CrateData api --- crates/ra_db/src/input.rs | 60 +++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 33 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 7b9f4efe4..4069c0fed 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -104,13 +104,16 @@ impl CrateName { } #[derive(Debug, Clone, PartialEq, Eq)] -struct CrateData { - file_id: FileId, - edition: Edition, - declaration_name: Option, +pub struct CrateData { + pub root_file_id: FileId, + pub edition: Edition, + /// The name to display to the end user. + /// This actual crate name can be different in a particular dependent crate + /// or may even be missing for some cases, such as a dummy crate for the code snippet. + pub display_name: Option, cfg_options: CfgOptions, env: Env, - dependencies: Vec, + pub dependencies: Vec, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -135,11 +138,11 @@ impl CrateGraph { &mut self, file_id: FileId, edition: Edition, - declaration_name: Option, + display_name: Option, cfg_options: CfgOptions, env: Env, ) -> CrateId { - let data = CrateData::new(file_id, edition, declaration_name, cfg_options, env); + let data = CrateData::new(file_id, edition, display_name, cfg_options, env); let crate_id = CrateId(self.arena.len() as u32); let prev = self.arena.insert(crate_id, data); assert!(prev.is_none()); @@ -171,33 +174,17 @@ impl CrateGraph { self.arena.keys().copied() } - pub fn crate_root(&self, crate_id: CrateId) -> FileId { - self.arena[&crate_id].file_id - } - - pub fn edition(&self, crate_id: CrateId) -> Edition { - self.arena[&crate_id].edition - } - - /// Returns a name of a crate, declared in the root project. - /// May be missing for some cases, such as when the crate definition was created for a code snippet. - /// - /// This should not be considered as a normal crate name, since the actual name can be different in - /// a particular dependent crate, where it is specified. - pub fn declaration_name(&self, crate_id: &CrateId) -> Option<&String> { - self.arena[crate_id].declaration_name.as_ref() + pub fn crate_data(&self, crate_id: &CrateId) -> &CrateData { + &self.arena[crate_id] } // FIXME: this only finds one crate with the given root; we could have multiple pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option { - let (&crate_id, _) = self.arena.iter().find(|(_crate_id, data)| data.file_id == file_id)?; + let (&crate_id, _) = + self.arena.iter().find(|(_crate_id, data)| data.root_file_id == file_id)?; Some(crate_id) } - pub fn dependencies(&self, crate_id: CrateId) -> impl Iterator { - self.arena[&crate_id].dependencies.iter() - } - /// Extends this crate graph by adding a complete disjoint second crate /// graph. /// @@ -220,7 +207,7 @@ impl CrateGraph { return false; } - for dep in self.dependencies(from) { + for dep in &self.crate_data(&from).dependencies { let crate_id = dep.crate_id(); if crate_id == target { return true; @@ -242,13 +229,20 @@ impl CrateId { impl CrateData { fn new( - file_id: FileId, + root_file_id: FileId, edition: Edition, - declaration_name: Option, + display_name: Option, cfg_options: CfgOptions, env: Env, ) -> CrateData { - CrateData { file_id, edition, declaration_name, dependencies: Vec::new(), cfg_options, env } + CrateData { + root_file_id, + edition, + display_name, + dependencies: Vec::new(), + cfg_options, + env, + } } fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { @@ -382,8 +376,8 @@ mod tests { .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) .is_ok()); assert_eq!( - graph.dependencies(crate1).collect::>(), - vec![&Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] + graph.crate_data(&crate1).dependencies, + vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] ); } } -- cgit v1.2.3