Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let the build_current_project function return only the path to cdylib. #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ crate-type = ["cdylib"]
[dependencies]
serde = { version = "1.0.103", features = ["derive"] }
serde_json = "1.0"
toml = "0.5.2"
cargo_metadata = "0.9.0"
toml = "0.8.14"
cargo_metadata = "0.18.1"

[dev-dependencies]
dlopen = "0.1.8"
Expand Down
20 changes: 16 additions & 4 deletions src/cargo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use cargo_metadata::Message;
use serde::Deserialize;
use serde_json::Map;
use std::io::{BufReader, Cursor};
use std::path::PathBuf;
use std::process::{Command, Output, Stdio};

Expand Down Expand Up @@ -73,7 +75,7 @@ pub fn build_example(name: &str) -> Result<PathBuf> {

pub fn parse_output(result: Output) -> Result<PathBuf> {
let mut artifact = None;
for message in cargo_metadata::parse_messages(result.stdout.as_slice()) {
for message in Message::parse_stream(Cursor::new(result.stdout)) {
match message? {
Message::CompilerMessage(m) => eprintln!("{}", m),
Message::CompilerArtifact(a) => artifact = Some(a),
Expand All @@ -84,9 +86,19 @@ pub fn parse_output(result: Output) -> Result<PathBuf> {
if !result.status.success() {
return Err(Error::CargoFail);
}
artifact
.ok_or(Error::CargoFail)
.map(|a| a.filenames[0].clone())
match artifact {
Some(artifact) => artifact
.filenames
.into_iter()
.filter(|filename| match filename.extension() {
Some("dll") | Some("dylib") | Some("so") => true,
_ => false,
})
.next()
.ok_or(Error::CdylibNotFound)
.map(|a| a.into_std_path_buf()),
None => Err(Error::CargoFail),
}
}

pub fn metadata() -> Result<Metadata> {
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum Error {
TomlDe(toml::de::Error),
TomlSer(toml::ser::Error),
Json(serde_json::Error),
CdylibNotFound,
}

pub type Result<T> = std::result::Result<T, Error>;
Expand All @@ -34,6 +35,9 @@ impl Display for Error {
TomlDe(e) => e.fmt(f),
TomlSer(e) => e.fmt(f),
Json(e) => e.fmt(f),
CdylibNotFound => {
write!(f, "can't find cdylib(.dll,.so,.cdylib) in output dir, please check that you have set [crate-type] correctly in Cargo.toml")
}
}
}
}
Expand Down