#!/bin/sh

# Regenerate the Wit bindings code; derived from
# https://github.com/bytecodealliance/wasi-rs/blob/main/ci/regenerate.sh

set -ex

# Invoke the `wit-bindgen` command.
generate() {
  wit-bindgen rust \
    ../platform/WASI/wasip2/io \
    ../platform/WASI/wasip2/clocks \
    ../platform/WASI/wasip2/filesystem \
    ../platform/WASI/wasip2/random \
    ../platform/WASI/wasip2/sockets \
    ../platform/WASI/wasip2/cli \
    ../platform/compute-wit/wit \
    "$@" \
    --format
}

# Generate bindings for `compute`.
#
# Use `--with` to re-export bindings from the `wasi` create instead of
# generating our own, so that if users have code that uses the wasi crate
# directly, everything uses the same types.
with="wasi:cli/environment@0.2.6=wasip2::cli::environment"
with="$with,wasi:cli/exit@0.2.6=wasip2::cli::exit"
with="$with,wasi:cli/stdin@0.2.6=wasip2::cli::stdin"
with="$with,wasi:cli/stdout@0.2.6=wasip2::cli::stdout"
with="$with,wasi:cli/stderr@0.2.6=wasip2::cli::stderr"
with="$with,wasi:clocks/monotonic-clock@0.2.6=wasip2::clocks::monotonic_clock"
with="$with,wasi:clocks/wall-clock@0.2.6=wasip2::clocks::wall_clock"
with="$with,wasi:io/error@0.2.6=wasip2::io::error"
with="$with,wasi:io/poll@0.2.6=wasip2::io::poll"
with="$with,wasi:io/streams@0.2.6=wasip2::io::streams"
with="$with,wasi:random/random@0.2.6=wasip2::random::random"
generate \
  --world fastly:compute/service@0.1.0 \
  --type-section-suffix fastly-compute-service-0-1-0 \
  --with "$with" \
  --default-bindings-module '$crate::service0_1_0' \
  --pub-export-macro \
  --generate-all
mv service.rs src/service0_1_0.rs
