mirror of
https://github.com/dtolnay/cargo-expand.git
synced 2025-04-04 13:27:37 +03:00
Reimplement Cargo.toml finding
This commit is contained in:
parent
d319e7afb4
commit
f88f534fda
1 changed files with 22 additions and 4 deletions
26
src/main.rs
26
src/main.rs
|
@ -39,7 +39,7 @@ use quote::quote;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::{self, BufRead, Write};
|
use std::io::{self, BufRead, ErrorKind, Write};
|
||||||
use std::panic::{self, PanicInfo, UnwindSafe};
|
use std::panic::{self, PanicInfo, UnwindSafe};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::{self, Command, Stdio};
|
use std::process::{self, Command, Stdio};
|
||||||
|
@ -504,8 +504,26 @@ fn cargo_metadata(
|
||||||
manifest_path: Option<&Path>,
|
manifest_path: Option<&Path>,
|
||||||
) -> cargo_metadata::Result<cargo_metadata::Metadata> {
|
) -> cargo_metadata::Result<cargo_metadata::Metadata> {
|
||||||
let mut cmd = cargo_metadata::MetadataCommand::new();
|
let mut cmd = cargo_metadata::MetadataCommand::new();
|
||||||
if let Some(manifest_path) = manifest_path {
|
let manifest_path = find_cargo_manifest(manifest_path)?;
|
||||||
cmd.manifest_path(manifest_path);
|
cmd.manifest_path(manifest_path);
|
||||||
}
|
|
||||||
cmd.exec()
|
cmd.exec()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn find_cargo_manifest(manifest_path: Option<&Path>) -> io::Result<PathBuf> {
|
||||||
|
if let Some(manifest_path) = manifest_path {
|
||||||
|
return Ok(manifest_path.to_owned());
|
||||||
|
}
|
||||||
|
|
||||||
|
let dir = env::current_dir()?;
|
||||||
|
let mut dir = dir.as_path();
|
||||||
|
loop {
|
||||||
|
let path = dir.join("Cargo.toml");
|
||||||
|
if path.try_exists()? {
|
||||||
|
return Ok(path);
|
||||||
|
}
|
||||||
|
dir = match dir.parent() {
|
||||||
|
Some(parent) => parent,
|
||||||
|
None => return Err(io::Error::new(ErrorKind::NotFound, "Cargo.toml not found")),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue