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 world_name: Option<String>,
25 mut cb: F,
26) -> R {
27 let path = std::path::Path::new(&filename);
28 let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
29 let manifest_dir = std::path::Path::new(&manifest_dir);
30 let path = manifest_dir.join(path);
31
32 let bytes = std::fs::read(path).unwrap();
33 let i = wasmparser::Parser::new(0).parse_all(&bytes);
34 let ct = crate::component::read_component_single_exported_type(i, world_name);
35
36 if !ct.uvars.is_empty()
39 || !ct.imports.is_empty()
40 || !ct.instance.evars.is_empty()
41 || ct.instance.unqualified.exports.len() != 1
42 {
43 panic!("malformed component type container for wit type");
44 };
45 let export = &ct.instance.unqualified.exports[0];
46 use etypes::ExternDesc;
47 let ExternDesc::Component(ct) = &export.desc else {
48 panic!("malformed component type container: does not contain component type");
49 };
50 tracing::debug!("hcm: considering component type {:?}", ct);
51 cb(export.kebab_name.to_string(), ct)
52}
53
54pub 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}