Reduce scope of deserialization

This commit is contained in:
Aleksey Kladov 2020-04-01 18:51:16 +02:00
parent e870cbc23d
commit 4936abdd49
5 changed files with 16 additions and 21 deletions

View File

@ -13,7 +13,6 @@ use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId}
use ra_arena::{Arena, Idx};
use ra_db::Edition;
use rustc_hash::FxHashMap;
use serde::Deserialize;
/// `CargoWorkspace` represents the logical structure of, well, a Cargo
/// workspace. It pretty closely mirrors `cargo metadata` output.
@ -43,10 +42,8 @@ impl ops::Index<Target> for CargoWorkspace {
}
}
// TODO: rename to CargoConfig, kill `rename_all`, kill serde dep?
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase", default)]
pub struct CargoFeatures {
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CargoConfig {
/// Do not activate the `default` feature.
pub no_default_features: bool,
@ -61,9 +58,9 @@ pub struct CargoFeatures {
pub load_out_dirs_from_check: bool,
}
impl Default for CargoFeatures {
impl Default for CargoConfig {
fn default() -> Self {
CargoFeatures {
CargoConfig {
no_default_features: false,
all_features: true,
features: Vec::new(),
@ -142,7 +139,7 @@ impl PackageData {
impl CargoWorkspace {
pub fn from_cargo_metadata(
cargo_toml: &Path,
cargo_features: &CargoFeatures,
cargo_features: &CargoConfig,
) -> Result<CargoWorkspace> {
let mut meta = MetadataCommand::new();
meta.manifest_path(cargo_toml);
@ -276,7 +273,7 @@ pub struct ExternResources {
pub fn load_extern_resources(
cargo_toml: &Path,
cargo_features: &CargoFeatures,
cargo_features: &CargoConfig,
) -> Result<ExternResources> {
let mut cmd = Command::new(cargo_binary());
cmd.args(&["check", "--message-format=json", "--manifest-path"]).arg(cargo_toml);
@ -294,9 +291,8 @@ pub fn load_extern_resources(
let mut res = ExternResources::default();
let stdout = String::from_utf8(output.stdout)?;
for line in stdout.lines() {
if let Ok(message) = serde_json::from_str::<cargo_metadata::Message>(&line) {
for message in cargo_metadata::parse_messages(output.stdout.as_slice()) {
if let Ok(message) = message {
match message {
Message::BuildScriptExecuted(BuildScript { package_id, out_dir, .. }) => {
res.out_dirs.insert(package_id, out_dir);

View File

@ -19,7 +19,7 @@ use rustc_hash::FxHashMap;
use serde_json::from_reader;
pub use crate::{
cargo_workspace::{CargoFeatures, CargoWorkspace, Package, Target, TargetKind},
cargo_workspace::{CargoConfig, CargoWorkspace, Package, Target, TargetKind},
json_project::JsonProject,
sysroot::Sysroot,
};
@ -78,14 +78,14 @@ impl PackageRoot {
}
impl ProjectWorkspace {
pub fn discover(path: &Path, cargo_features: &CargoFeatures) -> Result<ProjectWorkspace> {
pub fn discover(path: &Path, cargo_features: &CargoConfig) -> Result<ProjectWorkspace> {
ProjectWorkspace::discover_with_sysroot(path, true, cargo_features)
}
pub fn discover_with_sysroot(
path: &Path,
with_sysroot: bool,
cargo_features: &CargoFeatures,
cargo_features: &CargoConfig,
) -> Result<ProjectWorkspace> {
match find_rust_project_json(path) {
Some(json_path) => {

View File

@ -4,7 +4,6 @@
mod args;
use lsp_server::Connection;
use rust_analyzer::{cli, config::Config, from_json, Result};
use crate::args::HelpPrinted;

View File

@ -8,7 +8,7 @@ use crossbeam_channel::{unbounded, Receiver};
use ra_db::{ExternSourceId, FileId, SourceRootId};
use ra_ide::{AnalysisChange, AnalysisHost};
use ra_project_model::{
get_rustc_cfg_options, CargoFeatures, PackageRoot, ProcMacroClient, ProjectWorkspace,
get_rustc_cfg_options, CargoConfig, PackageRoot, ProcMacroClient, ProjectWorkspace,
};
use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch};
use rustc_hash::{FxHashMap, FxHashSet};
@ -29,7 +29,7 @@ pub(crate) fn load_cargo(
let root = std::env::current_dir()?.join(root);
let ws = ProjectWorkspace::discover(
root.as_ref(),
&CargoFeatures { load_out_dirs_from_check, ..Default::default() },
&CargoConfig { load_out_dirs_from_check, ..Default::default() },
)?;
let mut extern_dirs = FxHashSet::default();

View File

@ -10,7 +10,7 @@
use lsp_types::TextDocumentClientCapabilities;
use ra_flycheck::FlycheckConfig;
use ra_ide::{CompletionConfig, InlayHintsConfig};
use ra_project_model::CargoFeatures;
use ra_project_model::CargoConfig;
use serde::Deserialize;
#[derive(Debug, Clone)]
@ -29,7 +29,7 @@ pub struct Config {
pub lru_capacity: Option<usize>,
pub use_client_watching: bool,
pub exclude_globs: Vec<String>,
pub cargo: CargoFeatures,
pub cargo: CargoConfig,
pub with_sysroot: bool,
}
@ -92,7 +92,7 @@ impl Default for Config {
lru_capacity: None,
use_client_watching: false,
exclude_globs: Vec::new(),
cargo: CargoFeatures::default(),
cargo: CargoConfig::default(),
with_sysroot: true,
}
}