mirror of
https://github.com/dtolnay/cargo-expand.git
synced 2025-04-04 13:27:37 +03:00
Deserialize Cargo.toml without spawning cargo metadata command
This commit is contained in:
parent
737293417b
commit
58201ca96b
1 changed files with 18 additions and 8 deletions
26
src/main.rs
26
src/main.rs
|
@ -36,6 +36,7 @@ use bat::{PagingMode, PrettyPrinter};
|
||||||
use clap::{Parser, ValueEnum};
|
use clap::{Parser, ValueEnum};
|
||||||
use is_terminal::IsTerminal;
|
use is_terminal::IsTerminal;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
|
use serde::Deserialize;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
@ -313,8 +314,8 @@ fn apply_args(cmd: &mut Command, args: &Expand, color: &Coloring, outfile: &Path
|
||||||
}
|
}
|
||||||
|
|
||||||
if !has_explicit_build_target {
|
if !has_explicit_build_target {
|
||||||
if let Ok(cargo_metadata) = cargo_metadata(args.manifest_path.as_deref()) {
|
if let Ok(cargo_manifest) = cargo_manifest(args.manifest_path.as_deref()) {
|
||||||
if let Some(root_package) = cargo_metadata.root_package() {
|
if let Some(root_package) = cargo_manifest.package {
|
||||||
if let Some(default_run) = &root_package.default_run {
|
if let Some(default_run) = &root_package.default_run {
|
||||||
line.arg("--bin");
|
line.arg("--bin");
|
||||||
line.arg(default_run);
|
line.arg(default_run);
|
||||||
|
@ -500,13 +501,22 @@ fn get_color(args: &Expand, config: &Config) -> Coloring {
|
||||||
Coloring::Auto // default
|
Coloring::Auto // default
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cargo_metadata(
|
#[derive(Deserialize, Debug)]
|
||||||
manifest_path: Option<&Path>,
|
struct CargoManifest {
|
||||||
) -> cargo_metadata::Result<cargo_metadata::Metadata> {
|
package: Option<CargoPackage>,
|
||||||
let mut cmd = cargo_metadata::MetadataCommand::new();
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
struct CargoPackage {
|
||||||
|
#[serde(rename = "default-run")]
|
||||||
|
default_run: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cargo_manifest(manifest_path: Option<&Path>) -> Result<CargoManifest> {
|
||||||
let manifest_path = find_cargo_manifest(manifest_path)?;
|
let manifest_path = find_cargo_manifest(manifest_path)?;
|
||||||
cmd.manifest_path(manifest_path);
|
let content = fs::read_to_string(&manifest_path)?;
|
||||||
cmd.exec()
|
let cargo_manifest: CargoManifest = toml::from_str(&content)?;
|
||||||
|
Ok(cargo_manifest)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_cargo_manifest(manifest_path: Option<&Path>) -> io::Result<PathBuf> {
|
fn find_cargo_manifest(manifest_path: Option<&Path>) -> io::Result<PathBuf> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue