wasi-common 0.3.0

WASI implementation in Rust
Documentation
diff --git a/Cargo.toml b/Cargo.toml
index 04a961e..20fee21 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -44,6 +44,7 @@ wasi-common = { path = "." }
 
 [build-dependencies]
 cfg-if = "0.1.9"
+witx = "0.2.0"
 
 [lib]
 name = "wasi_common"
diff --git a/build.rs b/build.rs
index caf01c8..1450708 100644
--- a/build.rs
+++ b/build.rs
@@ -11,11 +11,18 @@ use std::fs::{read_dir, DirEntry, File};
 use std::io::{self, Write};
 use std::path::{Path, PathBuf};
 use std::process::{Command, Stdio};
+use witx;
 
 fn main() {
     let out_dir =
         PathBuf::from(env::var("OUT_DIR").expect("The OUT_DIR environment variable must be set"));
     println!("OUT_DIR is {:?}", out_dir);
+
+    build_all_tests(&out_dir);
+    build_all_interfaces(&out_dir);
+}
+
+fn build_all_tests(out_dir: &Path) {
     let mut out = File::create(out_dir.join("misc_testsuite_tests.rs"))
         .expect("error generating test source file");
 
@@ -206,3 +213,90 @@ fn no_preopens(testsuite: &str, name: &str) -> bool {
         unreachable!()
     }
 }
+
+fn render_datatype_ident(_variant: &witx::DatatypeIdent) -> String {
+    "fixme".to_owned()
+}
+
+fn render_intrepr(intrepr: &witx::IntRepr) -> &'static str {
+    match intrepr {
+        witx::IntRepr::U8 => "u8",
+        witx::IntRepr::U16 => "u16",
+        witx::IntRepr::U32 => "u32",
+        witx::IntRepr::U64 => "u64",
+    }
+}
+
+fn declare_datatype(out: &mut dyn io::Write, variant: &witx::DatatypeVariant) -> io::Result<()> {
+    match variant {
+        witx::DatatypeVariant::Struct(s) => {
+            writeln!(out, "#[repr(C)]")?;
+            writeln!(out, "#[derive(Debug, Clone)]")?;
+            writeln!(out, "struct {} {{", s.name.as_str())?;
+            for member in &s.members {
+                writeln!(
+                    out,
+                    "    {}: {},",
+                    member.name.as_str(),
+                    render_datatype_ident(&member.type_)
+                )?;
+            }
+            writeln!(out, "}}")?;
+        }
+        witx::DatatypeVariant::Alias(a) => {
+            writeln!(
+                out,
+                "type {} = {};",
+                a.name.as_str(),
+                render_datatype_ident(&a.to)
+            )?;
+        }
+        witx::DatatypeVariant::Enum(e) => {
+            writeln!(
+                out,
+                "type {} = {};",
+                e.name.as_str(),
+                render_intrepr(&e.repr)
+            )?;
+            for member in e.
+        }
+        x => writeln!(out, "fixme: declare type {:?}", x)?,
+    }
+
+    writeln!(out)?;
+
+    Ok(())
+}
+
+fn render_datatype(variant: &witx::DatatypeVariant) -> String {
+    match variant {
+        witx::DatatypeVariant::Struct(s) => s.name.as_str().to_owned(),
+        _ => "fixme type".to_owned(),
+    }
+}
+
+fn build_all_interfaces(out_dir: &Path) {
+    let doc = witx::load("/home/sunfish/WASI/phases/unstable/witx/wasi_unstable_preview0.witx")
+        .expect("can't load witx");
+
+    let mut out =
+        File::create(out_dir.join("wasi_unstable_preview0.rs")).expect("can't open output file");
+
+    for t in doc.datatypes() {
+        declare_datatype(&mut out, &t.variant).expect("can't write");
+    }
+    writeln!(out).expect("can't write");
+    for t in doc.datatypes() {
+        writeln!(
+            out,
+            "type {} = {};",
+            t.name.as_str(),
+            render_datatype(&t.variant)
+        )
+        .expect("can't write");
+    }
+
+    for m in doc.modules() {
+        writeln!(out, "module {}", m.name.as_str()).expect("can't write");
+    }
+}