Skip to content

Commit 0a558bc

Browse files
committed
Don't generate bindings for stdlib
Signed-off-by: itowlson <[email protected]>
1 parent 4e1f500 commit 0a558bc

File tree

2 files changed

+23
-85
lines changed

2 files changed

+23
-85
lines changed

src/commands/add.rs

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,9 @@ async fn generate_rust_bindings(root_dir: &Path, cargo_toml: &Path, _component_i
440440
let dep_module_name = crate::language::rust::identifier_safe(package_name);
441441

442442
// step 1: create a module with the generate! macro
443-
let imps = interfaces.iter().map(|i| format!(r#" import {i};"#)).collect::<Vec<_>>();
443+
let imps = interfaces.iter().filter(|itf| !crate::language::rust::is_stdlib_known(itf)).map(|i| format!(r#" import {i};"#)).collect::<Vec<_>>();
444444
let imps = imps.join("\n");
445-
let gens = interfaces.iter().map(|i| format!(r#" "{i}": generate,"#)).collect::<Vec<_>>();
445+
let gens = interfaces.iter().filter(|itf| !crate::language::rust::is_stdlib_known(itf)).map(|i| format!(r#" "{i}": generate,"#)).collect::<Vec<_>>();
446446
let gens = gens.join("\n");
447447
let gen_name = format!("{}-{}", package_name.namespace, package_name.name);
448448

@@ -503,87 +503,4 @@ async fn generate_rust_bindings(root_dir: &Path, cargo_toml: &Path, _component_i
503503
}
504504

505505
Ok(())
506-
507-
// let lib_file = root_dir.join("src/lib.rs");
508-
// if !lib_file.is_file() {
509-
// bail!("src/lib.rs is not a file");
510-
// }
511-
// let lib_text = std::fs::read_to_string(&lib_file)?;
512-
513-
// // ALL RIGHT HERE WE GO
514-
515-
// // If we already have a `mod deps`...
516-
// if let Some(mod_deps_index) = lib_text.lines().position(|l| l.trim().starts_with("mod deps {")) {
517-
// // oh no we gotta do some flippin parsing
518-
// // TODO: can syn help us? It seemed a bit agonising and not terribly supportive
519-
// let mut lines: Vec<_> = lib_text.lines().map(|s| s.to_owned()).collect();
520-
// let mut index = mod_deps_index;
521-
// let mut in_imports = false;
522-
// let mut in_with = false;
523-
// let mut unseen_imports: Vec<_> = interfaces.iter().map(|i| format!("import {i};")).collect();
524-
// let mut unseen_withs: Vec<_> = interfaces.iter().map(|i| format!("\"{i}\": generate,")).collect();
525-
// loop {
526-
// index += 1;
527-
// let current = &lines[index];
528-
// if current.trim().starts_with("world imports {") {
529-
// in_imports = true;
530-
// continue;
531-
// }
532-
// if in_imports {
533-
// if current.trim().starts_with("}") {
534-
// // insert those not yet seen and BUMP INDEX PAST THEM
535-
// in_imports = false;
536-
// for import in &unseen_imports {
537-
// lines.insert(index - 1, format!(" {import}"));
538-
// index += 1;
539-
// }
540-
// continue;;
541-
// }
542-
// if current.trim().starts_with("import ") {
543-
// // if this was one we were planning to insert, remove it from the plan!
544-
// unseen_imports.retain(|imp| imp != current.trim());
545-
// continue;
546-
// }
547-
// }
548-
// if current.trim().starts_with("with: {") {
549-
// in_with = true;
550-
// continue;
551-
// }
552-
// if in_with {
553-
// if current.trim().ends_with(": generate,") {
554-
// // if this was one we were planning to insert, remove it from the plan!
555-
// unseen_withs.retain(|w| w != current.trim());
556-
// continue;
557-
// }
558-
559-
// }
560-
// }
561-
562-
// } else {
563-
// // We will create a `mod deps` with SCIENCE in it
564-
// let imps = interfaces.iter().map(|i| format!(r#" import {i};"#)).collect::<Vec<_>>();
565-
// let imps = imps.join("\n");
566-
// let gens = interfaces.iter().map(|i| format!(r#" "{i}": generate,"#)).collect::<Vec<_>>();
567-
// let gens = gens.join("\n");
568-
// let deps_text = format!(r###"
569-
// mod deps {{
570-
// wit_bindgen::generate!({{
571-
// inline: r#"
572-
// package root:component;
573-
// world imports {{
574-
// {imps}
575-
// }}
576-
// "#,
577-
// with: {{
578-
// {gens}
579-
// }},
580-
// path: ".wit/components/{component_id}",
581-
// }});
582-
// }}
583-
// "###);
584-
585-
// // TODO: insert this into the file in a SCIENTIFICALLY DETERMINED place
586-
// }
587-
588-
// todo!()
589506
}

src/language/rust.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
11
pub fn identifier_safe(package_name: &wit_parser::PackageName) -> String {
22
format!("{ns}_{name}", ns = package_name.namespace, name = package_name.name)
33
}
4+
5+
// TODO: moar
6+
const STDLIB_INTERFACES: &[&str] = &[
7+
"wasi:io/[email protected]",
8+
"wasi:io/[email protected]",
9+
"wasi:cli/[email protected]",
10+
"wasi:cli/[email protected]",
11+
"wasi:cli/[email protected]",
12+
"wasi:cli/[email protected]",
13+
"wasi:cli/[email protected]",
14+
"wasi:clocks/[email protected]",
15+
"wasi:filesystem/[email protected]",
16+
"wasi:filesystem/[email protected]",
17+
];
18+
19+
// Interfaces that are implemented by stdlib and shouldn't be bound explicitly
20+
// TODO: We have lost a lot of structure at this point and might want to try
21+
// to operate on packages but at this point let's just bodge it
22+
pub fn is_stdlib_known(interface_name: &str) -> bool {
23+
STDLIB_INTERFACES.contains(&interface_name)
24+
}

0 commit comments

Comments
 (0)