Skip to content

Commit

Permalink
cmd/wit-bindgen-go: add new describe subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
ydnar committed Sep 24, 2023
1 parent 67fa5fc commit 92ffce5
Showing 1 changed file with 94 additions and 6 deletions.
100 changes: 94 additions & 6 deletions cmd/wit-bindgen-go/cmd/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package describe

import (
"fmt"
"io"
"os"
"strings"

"github.com/k0kubun/pp/v3"
"github.com/urfave/cli/v3"
"github.com/ydnar/wasm-tools-go/internal/witcli"
"github.com/ydnar/wasm-tools-go/wit"
)

// Command is the CLI command for describe.
Expand All @@ -21,11 +24,96 @@ func action(ctx *cli.Context) error {
return err
}

fmt.Printf("// %d worlds(s), %d packages(s), %d interfaces(s), %d types(s)\n",
len(res.Worlds), len(res.Packages), len(res.Interfaces), len(res.TypeDefs))
p := pp.New()
p.SetExportedOnly(true)
p.Print(res)
p := &printer{w: os.Stdout}

for _, w := range res.Worlds {
describeWorld(p, w)
}

return nil
}

func describeWorld(p *printer, w *wit.World) {
name := w.Package.Name.String() + "/" + w.Name
if len(w.Imports) == 0 && len(w.Exports) == 0 {
p.Printf("world %s {}", name)
return
}
// TODO: print World.Docs
p.Printf("world %s {", name)
{
p := p.indent()
for name, item := range w.Imports {
describeWorldItem(p, "import ", name, item)
}
for name, item := range w.Exports {
describeWorldItem(p, "export ", name, item)
}
}
p.Println("}")
p.Println()
}

func describeWorldItem(p *printer, pfx, name string, item wit.WorldItem) {
switch v := item.(type) {
case *wit.Interface:
describeWorldInterface(p, pfx, name, v)
case *wit.TypeDef:
describeWorldTypeDef(p, pfx, name, v)
case *wit.Function:
describeWorldFunction(p, pfx, name, v)
}
}

func describeWorldInterface(p *printer, pfx, name string, i *wit.Interface) {
if i.Name != nil {
name = i.Package.Name.String() + "/" + *i.Name
}
// TODO: print Interface.Docs
if len(i.TypeDefs) == 0 && len(i.Functions) == 0 {
p.Printf("%s%s {}", pfx, name)
return
}
p.Printf("%s%s {", pfx, name)
{

}
p.Println("}")
}

func describeWorldTypeDef(p *printer, pfx, name string, t *wit.TypeDef) {
// TODO
}

func describeWorldFunction(p *printer, pfx, name string, t *wit.Function) {
// TODO
}

type printer struct {
w io.Writer
depth int
}

func (p *printer) indent() *printer {
pi := *p
pi.depth++
return &pi
}

func (p *printer) println(s string) {
fmt.Fprintln(p.w, strings.Repeat("\t", p.depth), s)
}

func (p *printer) Println(a ...any) {
s := fmt.Sprint(a...)
for _, line := range strings.Split(s, "\n") {
p.println(line)
}
}

func (p *printer) Printf(format string, a ...any) {
s := fmt.Sprintf(format, a...)
for _, line := range strings.Split(s, "\n") {
p.println(line)
}
}

0 comments on commit 92ffce5

Please sign in to comment.