From 4936abdd49b1d0ba6b2a858bee3a5a665de9d6f3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 1 Apr 2020 18:51:16 +0200 Subject: Reduce scope of deserialization --- crates/ra_project_model/src/cargo_workspace.rs | 20 ++++++++------------ crates/ra_project_model/src/lib.rs | 6 +++--- 2 files changed, 11 insertions(+), 15 deletions(-) (limited to 'crates/ra_project_model') diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index 91735a726..c1b6e1ddc 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs @@ -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 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 { 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 { 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::(&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); diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 444d3bb3f..dd9c80691 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -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 { + pub fn discover(path: &Path, cargo_features: &CargoConfig) -> Result { 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 { match find_rust_project_json(path) { Some(json_path) => { -- cgit v1.2.3