use std::{fs, path::PathBuf};
#[macro_export]
macro_rules! lookup {
() => {{
if let Some(artifact) = ::gcli::embed::Artifact::from_out_dir(env!("OUT_DIR")) {
artifact
} else {
::gcli::log::warn!("No artifact is found from the out_dir");
Default::default()
}
}};
}
const OUT_SUFFIX_LENGTH: usize = 17;
#[derive(Debug, Default)]
pub struct Artifact {
pub opt: Vec<u8>,
}
impl Artifact {
pub fn from_out_dir(out: &str) -> Option<Self> {
let out_dir = PathBuf::from(out);
let mut ancestors = out_dir.ancestors();
let [name, profile, target] = [
ancestors
.nth(1)?
.file_name()?
.to_str()
.and_then(|name| name.get(..name.len().checked_sub(OUT_SUFFIX_LENGTH)?))?,
if ancestors.nth(1)?.file_name()?.to_str()? == "debug" {
"debug"
} else {
"release"
},
ancestors.next()?.to_str()?,
];
let bin = PathBuf::from(format!("{target}/wasm32-gear/{profile}"));
let stem = PathBuf::from(name.replace('-', "_"));
Some(Self {
opt: fs::read(bin.join(stem.with_extension("wasm"))).ok()?,
})
}
}