Skip to content

Commit

Permalink
fix: use cargo-metadata to support init command in workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
0xalpharush committed Dec 8, 2023
1 parent a860fd9 commit 076c771
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 27 deletions.
120 changes: 112 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ clap = { version = "4.0.29", features = ["derive", "deprecated"] }
tempfile = "3.3.0"
toml = "0.5.9"
rustc_version = "0.4.0"
cargo_metadata = "0.18.1"

[dev-dependencies]
assert_cmd = "2.0.7"
Expand Down
32 changes: 13 additions & 19 deletions src/project.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::options::{self, BuildMode, BuildOptions, Sanitizer};
use crate::utils::default_target;
use anyhow::{anyhow, bail, Context, Result};
use cargo_metadata::MetadataCommand;
use std::collections::HashSet;
use std::io::Read;
use std::io::Write;
Expand Down Expand Up @@ -931,25 +932,18 @@ pub struct Manifest {

impl Manifest {
pub fn parse(path: &Path) -> Result<Self> {
let contents = fs::read(path)?;
let value: toml::Value = toml::from_slice(&contents)?;
let package = value
.as_table()
.and_then(|v| v.get("package"))
.and_then(toml::Value::as_table);
let crate_name = package
.and_then(|v| v.get("name"))
.and_then(toml::Value::as_str)
.with_context(|| anyhow!("{} (package.name) is malformed", path.display()))?
.to_owned();
let edition = package
.expect("can't be None at this point")
.get("edition")
.map(|v| match v.as_str() {
Some(s) => Ok(s.to_owned()),
None => bail!("{} (package.edition) is malformed", path.display()),
})
.transpose()?;
let metatdata = MetadataCommand::new().exec()?;
let package = metatdata
.packages
.iter()
.find(|p| p.manifest_path == path)
.expect({
let path = path.display();
&format!("could not find package for {}", path)
});
let crate_name = package.name.clone();
let edition = Some(String::from(package.edition.as_str()));

Ok(Manifest {
crate_name,
edition,
Expand Down

0 comments on commit 076c771

Please sign in to comment.