1use anyhow::Result;
4use serde::Deserialize;
5use std::{fs, path::PathBuf};
6
7#[derive(Deserialize)]
9struct Manifest {
10 pub package: Package,
12}
13
14#[derive(Deserialize)]
16struct Package {
17 pub name: String,
19}
20
21pub fn pkg_name() -> Result<String> {
23 let manifest = fs::read_to_string(etc::find_up("Cargo.toml")?)?;
24 Ok(toml::from_str::<Manifest>(&manifest)?.package.name)
25}
26
27pub fn wasm(name: &str) -> Result<PathBuf> {
29 let target = target_dir()?;
30 let search = |profile: &str| -> Result<PathBuf> {
31 let target = target.join(profile);
32 let mut wasm = target.join(name).with_extension("wasm");
33 if !wasm.exists() {
34 wasm = target.join("examples").join(name).with_extension("wasm");
35 }
36
37 if wasm.exists() {
38 Ok(wasm)
39 } else {
40 Err(anyhow::anyhow!("{} not found", wasm.to_string_lossy()))
41 }
42 };
43
44 search("release").or_else(|_| search("debug"))
45}
46
47fn target_dir() -> Result<PathBuf> {
49 cargo_metadata::MetadataCommand::new()
50 .no_deps()
51 .exec()
52 .map_err(Into::into)
53 .map(|metadata| {
54 metadata
55 .target_directory
56 .join("wasm32-unknown-unknown")
57 .into()
58 })
59}