Skip to content

Commit

Permalink
wit, testdata: generate WIT for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
ydnar committed Sep 25, 2023
1 parent 1076b14 commit baa952d
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 6 deletions.
4 changes: 2 additions & 2 deletions testdata/types.wit.json.golden.wit
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ interface types {
variant t36 { a, b(u32) }
variant t37 { a, b(option<u32>) }
type t4 = u64
/* TODO(t41) */
/* TODO(t42) */
enum t41 { a, b, c }
enum t42 { a, b, c }
type t43 = bool
type t44 = string
type t45 = list<list<list<t32>>>
Expand Down
82 changes: 80 additions & 2 deletions testdata/wasi.wit.json.golden.wit
Original file line number Diff line number Diff line change
@@ -1,7 +1,85 @@
package wasi:filesystem

interface wasi {
/* TODO(clockid) */
/* TODO(errno) */
enum clockid { realtime, monotonic }
enum errno {
success,
toobig,
access,
addrinuse,
addrnotavail,
afnosupport,
again,
already,
badf,
badmsg,
busy,
canceled,
child,
connaborted,
connrefused,
connreset,
deadlk,
destaddrreq,
dom,
dquot,
exist,
fault,
fbig,
hostunreach,
idrm,
ilseq,
inprogress,
intr,
inval,
io,
isconn,
isdir,
loop,
mfile,
mlink,
msgsize,
multihop,
nametoolong,
netdown,
netreset,
netunreach,
nfile,
nobufs,
nodev,
noent,
noexec,
nolck,
nolink,
nomem,
nomsg,
noprotoopt,
nospc,
nosys,
notconn,
notdir,
notempty,
notrecoverable,
notsock,
notsup,
notty,
nxio,
overflow,
ownerdead,
perm,
pipe,
proto,
protonosupport,
prototype,
range,
rofs,
spipe,
srch,
stale,
timedout,
txtbsy,
xdev,
notcapable
}
type timestamp = u64
}
28 changes: 26 additions & 2 deletions wit/wit.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (t *Tuple) WIT(ctx Node, _ string) string {
return b.String()
}

func (v *Variant) WIT(ctx Node, name string) string {
func (v *Variant) WIT(_ Node, name string) string {
var b strings.Builder
b.WriteString("variant ")
b.WriteString(name)
Expand All @@ -289,7 +289,7 @@ func (v *Variant) WIT(ctx Node, name string) string {
if i > 0 {
b.WriteString(",\n")
}
b.WriteString(indent(v.Cases[i].WIT(ctx, "")))
b.WriteString(indent(v.Cases[i].WIT(v, "")))
}
b.WriteRune('\n')
}
Expand All @@ -309,6 +309,30 @@ func (c *Case) WIT(_ Node, _ string) string {
return b.String()
}

func (e *Enum) WIT(_ Node, name string) string {
var b strings.Builder
b.WriteString("enum ")
b.WriteString(name)
b.WriteString(" {")
if len(e.Cases) > 0 {
b.WriteRune('\n')
for i := range e.Cases {
if i > 0 {
b.WriteString(",\n")
}
b.WriteString(indent(e.Cases[i].WIT(e, "")))
}
b.WriteRune('\n')
}
b.WriteRune('}')
return unwrap(b.String())
}

func (c *EnumCase) WIT(_ Node, _ string) string {
// TODO: docs
return c.Name
}

func (o *Option) WIT(_ Node, name string) string {
var b strings.Builder
if name != "" {
Expand Down

0 comments on commit baa952d

Please sign in to comment.