use --message-format=plain for cargo locate-project

This avoids the deserialization step.
This commit is contained in:
Remo Senekowitsch
2026-09-21 16:36:32 +02:00
parent 359ece4d27
commit cf3a66fe5e
+9 -15
View File
@@ -3,7 +3,6 @@ use crossterm::{
QueueableCommand, QueueableCommand,
style::{Attribute, Color, ResetColor, SetAttribute, SetForegroundColor}, style::{Attribute, Color, ResetColor, SetAttribute, SetForegroundColor},
}; };
use serde::Deserialize;
use std::{ use std::{
env::{current_dir, set_current_dir}, env::{current_dir, set_current_dir},
fs::{self, create_dir}, fs::{self, create_dir},
@@ -17,12 +16,6 @@ use crate::{
info_file::InfoFile, term::press_enter_prompt, info_file::InfoFile, term::press_enter_prompt,
}; };
#[derive(Deserialize)]
struct CargoLocateProject<'a> {
#[serde(borrow)]
root: &'a str,
}
pub fn init() -> Result<()> { pub fn init() -> Result<()> {
let rustlings_dir = Path::new("rustlings"); let rustlings_dir = Path::new("rustlings");
if rustlings_dir.exists() { if rustlings_dir.exists() {
@@ -33,6 +26,7 @@ pub fn init() -> Result<()> {
.arg("locate-project") .arg("locate-project")
.arg("-q") .arg("-q")
.arg("--workspace") .arg("--workspace")
.arg("--message-format=plain")
.stdin(Stdio::null()) .stdin(Stdio::null())
.stderr(Stdio::null()) .stderr(Stdio::null())
.output() .output()
@@ -66,15 +60,15 @@ pub fn init() -> Result<()> {
bail!(IN_INITIALIZED_DIR_ERR); bail!(IN_INITIALIZED_DIR_ERR);
} }
let workspace_manifest = let workspace_manifest = {
serde_json::de::from_slice::<CargoLocateProject>(&locate_project_output.stdout) let mut stdout = String::try_from(locate_project_output.stdout)
.context( .context("Failed to convert the output of `cargo locate-project …` to a string")?;
"Failed to read the field `root` from the output of `cargo locate-project …`", stdout.truncate(stdout.trim_end().len()); // trim trailing newline
)? stdout
.root; };
let workspace_manifest_content = fs::read_to_string(workspace_manifest) let workspace_manifest_content = fs::read_to_string(&workspace_manifest)
.with_context(|| format!("Failed to read the file {}", workspace_manifest))?; .with_context(|| format!("Failed to read the file {workspace_manifest}"))?;
if !workspace_manifest_content.contains("[workspace]") if !workspace_manifest_content.contains("[workspace]")
&& !workspace_manifest_content.contains("workspace.") && !workspace_manifest_content.contains("workspace.")
{ {