hyperlight_component_util/
util.rs1use crate::etypes;
19
20pub fn read_wit_type_from_file<R, F: FnMut(String, &etypes::Component) -> R>(
23 filename: impl AsRef<std::ffi::OsStr>,
24 mut cb: F,
25) -> R {
26 let path = std::path::Path::new(&filename);
27 let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
28 let manifest_dir = std::path::Path::new(&manifest_dir);
29 let path = manifest_dir.join(path);
30
31 let bytes = std::fs::read(path).unwrap();
32 let i = wasmparser::Parser::new(0).parse_all(&bytes);
33 let ct = crate::component::read_component_single_exported_type(i);
34
35 if !ct.uvars.is_empty()
38 || !ct.imports.is_empty()
39 || !ct.instance.evars.is_empty()
40 || ct.instance.unqualified.exports.len() != 1
41 {
42 panic!("malformed component type container for wit type");
43 };
44 let export = &ct.instance.unqualified.exports[0];
45 use etypes::ExternDesc;
46 let ExternDesc::Component(ct) = &export.desc else {
47 panic!("malformed component type container: does not contain component type");
48 };
49 log::debug!("hcm: considering component type {:?}", ct);
50 cb(export.kebab_name.to_string(), ct)
51}
52
53pub fn emit_decls(decls: proc_macro2::TokenStream) -> proc_macro2::TokenStream {
58 if let Ok(dbg_out) = std::env::var("HYPERLIGHT_COMPONENT_MACRO_DEBUG") {
59 if let Ok(file) = syn::parse2(decls.clone()) {
60 std::fs::write(&dbg_out, prettyplease::unparse(&file)).unwrap();
61 } else {
62 let decls = format!("{}", &decls);
63 std::fs::write(&dbg_out, &decls).unwrap();
64 }
65 quote::quote! { include!(#dbg_out); }
66 } else {
67 decls
68 }
69}