Skip to content

Commit 4936abd

Browse files
committed
Reduce scope of deserialization
1 parent e870cbc commit 4936abd

File tree

5 files changed

+16
-21
lines changed

5 files changed

+16
-21
lines changed

crates/ra_project_model/src/cargo_workspace.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId}
1313
use ra_arena::{Arena, Idx};
1414
use ra_db::Edition;
1515
use rustc_hash::FxHashMap;
16-
use serde::Deserialize;
1716

1817
/// `CargoWorkspace` represents the logical structure of, well, a Cargo
1918
/// workspace. It pretty closely mirrors `cargo metadata` output.
@@ -43,10 +42,8 @@ impl ops::Index<Target> for CargoWorkspace {
4342
}
4443
}
4544

46-
// TODO: rename to CargoConfig, kill `rename_all`, kill serde dep?
47-
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
48-
#[serde(rename_all = "camelCase", default)]
49-
pub struct CargoFeatures {
45+
#[derive(Clone, Debug, PartialEq, Eq)]
46+
pub struct CargoConfig {
5047
/// Do not activate the `default` feature.
5148
pub no_default_features: bool,
5249

@@ -61,9 +58,9 @@ pub struct CargoFeatures {
6158
pub load_out_dirs_from_check: bool,
6259
}
6360

64-
impl Default for CargoFeatures {
61+
impl Default for CargoConfig {
6562
fn default() -> Self {
66-
CargoFeatures {
63+
CargoConfig {
6764
no_default_features: false,
6865
all_features: true,
6966
features: Vec::new(),
@@ -142,7 +139,7 @@ impl PackageData {
142139
impl CargoWorkspace {
143140
pub fn from_cargo_metadata(
144141
cargo_toml: &Path,
145-
cargo_features: &CargoFeatures,
142+
cargo_features: &CargoConfig,
146143
) -> Result<CargoWorkspace> {
147144
let mut meta = MetadataCommand::new();
148145
meta.manifest_path(cargo_toml);
@@ -276,7 +273,7 @@ pub struct ExternResources {
276273

277274
pub fn load_extern_resources(
278275
cargo_toml: &Path,
279-
cargo_features: &CargoFeatures,
276+
cargo_features: &CargoConfig,
280277
) -> Result<ExternResources> {
281278
let mut cmd = Command::new(cargo_binary());
282279
cmd.args(&["check", "--message-format=json", "--manifest-path"]).arg(cargo_toml);
@@ -294,9 +291,8 @@ pub fn load_extern_resources(
294291

295292
let mut res = ExternResources::default();
296293

297-
let stdout = String::from_utf8(output.stdout)?;
298-
for line in stdout.lines() {
299-
if let Ok(message) = serde_json::from_str::<cargo_metadata::Message>(&line) {
294+
for message in cargo_metadata::parse_messages(output.stdout.as_slice()) {
295+
if let Ok(message) = message {
300296
match message {
301297
Message::BuildScriptExecuted(BuildScript { package_id, out_dir, .. }) => {
302298
res.out_dirs.insert(package_id, out_dir);

crates/ra_project_model/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_hash::FxHashMap;
1919
use serde_json::from_reader;
2020

2121
pub use crate::{
22-
cargo_workspace::{CargoFeatures, CargoWorkspace, Package, Target, TargetKind},
22+
cargo_workspace::{CargoConfig, CargoWorkspace, Package, Target, TargetKind},
2323
json_project::JsonProject,
2424
sysroot::Sysroot,
2525
};
@@ -78,14 +78,14 @@ impl PackageRoot {
7878
}
7979

8080
impl ProjectWorkspace {
81-
pub fn discover(path: &Path, cargo_features: &CargoFeatures) -> Result<ProjectWorkspace> {
81+
pub fn discover(path: &Path, cargo_features: &CargoConfig) -> Result<ProjectWorkspace> {
8282
ProjectWorkspace::discover_with_sysroot(path, true, cargo_features)
8383
}
8484

8585
pub fn discover_with_sysroot(
8686
path: &Path,
8787
with_sysroot: bool,
88-
cargo_features: &CargoFeatures,
88+
cargo_features: &CargoConfig,
8989
) -> Result<ProjectWorkspace> {
9090
match find_rust_project_json(path) {
9191
Some(json_path) => {

crates/rust-analyzer/src/bin/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
mod args;
55

66
use lsp_server::Connection;
7-
87
use rust_analyzer::{cli, config::Config, from_json, Result};
98

109
use crate::args::HelpPrinted;

crates/rust-analyzer/src/cli/load_cargo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crossbeam_channel::{unbounded, Receiver};
88
use ra_db::{ExternSourceId, FileId, SourceRootId};
99
use ra_ide::{AnalysisChange, AnalysisHost};
1010
use ra_project_model::{
11-
get_rustc_cfg_options, CargoFeatures, PackageRoot, ProcMacroClient, ProjectWorkspace,
11+
get_rustc_cfg_options, CargoConfig, PackageRoot, ProcMacroClient, ProjectWorkspace,
1212
};
1313
use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch};
1414
use rustc_hash::{FxHashMap, FxHashSet};
@@ -29,7 +29,7 @@ pub(crate) fn load_cargo(
2929
let root = std::env::current_dir()?.join(root);
3030
let ws = ProjectWorkspace::discover(
3131
root.as_ref(),
32-
&CargoFeatures { load_out_dirs_from_check, ..Default::default() },
32+
&CargoConfig { load_out_dirs_from_check, ..Default::default() },
3333
)?;
3434

3535
let mut extern_dirs = FxHashSet::default();

crates/rust-analyzer/src/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use lsp_types::TextDocumentClientCapabilities;
1111
use ra_flycheck::FlycheckConfig;
1212
use ra_ide::{CompletionConfig, InlayHintsConfig};
13-
use ra_project_model::CargoFeatures;
13+
use ra_project_model::CargoConfig;
1414
use serde::Deserialize;
1515

1616
#[derive(Debug, Clone)]
@@ -29,7 +29,7 @@ pub struct Config {
2929
pub lru_capacity: Option<usize>,
3030
pub use_client_watching: bool,
3131
pub exclude_globs: Vec<String>,
32-
pub cargo: CargoFeatures,
32+
pub cargo: CargoConfig,
3333
pub with_sysroot: bool,
3434
}
3535

@@ -92,7 +92,7 @@ impl Default for Config {
9292
lru_capacity: None,
9393
use_client_watching: false,
9494
exclude_globs: Vec::new(),
95-
cargo: CargoFeatures::default(),
95+
cargo: CargoConfig::default(),
9696
with_sysroot: true,
9797
}
9898
}

0 commit comments

Comments
 (0)