# Courier package

A fictional parcel shop exposes its delivery tariff to DETLEF scripts. The package
uses only `detlef_sdk.h`: the same callback and registration table compile for
native and Wasm hosts.

`courier_quote(weight_grams, express)` is an eager Function with an integer weight
and boolean service selector. It returns a `CourierQuote` record:

| Field | Type | Meaning |
| --- | --- | --- |
| `price_cents` | `integer` | Total price in cents |
| `delivery_days` | `integer` | One day for express, three for standard |
| `service` | `string` | `"express"` or `"standard"` |

Parcels must weigh 1–20,000 grams. The tariff is 350 cents plus 75 cents per started
500 grams; express adds 500 cents. Integer cents keep the calculation exact.
Invalid weights produce a callback error; the registered signature checks argument
types and count before calling C.

After loading the package and installing stdlib, a host can evaluate:

```text
declare(quote, CourierQuote, courier_quote(1200, true))
print(get_field(quote, service)) / express
print(get_field(quote, price_cents)) / 1075
print(get_field(quote, delivery_days)) / 1
```

## Build and run

With Clang and its Wasm linker installed, run from the repository root:

```console
python sdk/c/tools/build-sdk-package.py examples/packages/courier/package.c examples/packages/courier/metadata.toml -o target/courier.dtfpkg
```

The manifest contains a Windows native target and a Wasm target. Adapt the native
target and library extension to your platform; compiling it also requires that
platform's C toolchain. Both artifacts are bundled into one archive.

Build the Wasm bindings and run the [Node package host](../../js/package_host.mjs):

```console
python crates/detlef-wasm/build.py --target nodejs
node examples/js/package_host.mjs target/courier.dtfpkg
```

The [C package host](../../c/package_host.c) loads the same archive and evaluates
the same call. Compile it with `sdk/c/include/` on the include path and link it
against `detlef-ffi`; pass `target/courier.dtfpkg` as its only argument. Both hosts
print the returned record as JSON.

Run `python sdk/c/tests/run.py` to build this source and check its behavior in native,
Node, and web-target runtimes, including weight boundaries and invalid arguments.
