use anyhow::Result;
use serde::Deserialize;
use std::{fs, path::PathBuf};
#[derive(Deserialize)]
struct Manifest {
pub package: Package,
}
#[derive(Deserialize)]
struct Package {
pub name: String,
}
pub fn pkg_name() -> Result<String> {
let manifest = fs::read_to_string(etc::find_up("Cargo.toml")?)?;
Ok(toml::from_str::<Manifest>(&manifest)?.package.name)
}
pub fn wasm(name: &str) -> Result<PathBuf> {
let target = target_dir()?;
let search = |profile: &str| -> Result<PathBuf> {
let target = target.join(profile);
let mut wasm = target.join(name).with_extension("wasm");
if !wasm.exists() {
wasm = target.join("examples").join(name).with_extension("wasm");
}
if wasm.exists() {
Ok(wasm)
} else {
Err(anyhow::anyhow!("{} not found", wasm.to_string_lossy()))
}
};
search("release").or_else(|_| search("debug"))
}
fn target_dir() -> Result<PathBuf> {
cargo_metadata::MetadataCommand::new()
.no_deps()
.exec()
.map_err(Into::into)
.map(|metadata| {
metadata
.target_directory
.join("wasm32-unknown-unknown")
.into()
})
}