02 First evaluation

The example decodes a configuration, opens a database without storage, submits one evaluation, waits for its response and prints it. The request contains the native DUST expression (+ 20 22) in params.code. A string containing that expression would remain a string; the engine does not parse code strings in requests.

#include "stardust.h"
#include <inttypes.h>
#include <stdio.h>
#include <string.h>

static int64_t decode(const char *text, stardust_document **out,
                      stardust_document **error) {
    stardust_bytes input = {(const uint8_t *)text, (int64_t)strlen(text)};
    return stardust_decode(input, NULL, out, error);
}

static void diagnostic(int64_t status, stardust_document *error) {
    fprintf(stderr, "Stardust status: %" PRId64 "\n", status);
    if (error != NULL) {
        stardust_buffer text = {0};
        if (stardust_encode(error, NULL, &text, NULL) == STARDUST_OK) {
            fwrite(text.data, 1, (size_t)text.length, stderr);
            fputc('\n', stderr);
            stardust_release(text.owner, NULL);
        }
    }
}

int main(void) {
    stardust_document *config = NULL, *requests = NULL, *response = NULL;
    stardust_document *error = NULL;
    stardust_database *database = NULL;
    stardust_buffer encoded = {0};
    int result = 1;
    int64_t status = decode("{workers 1 plugins <stdlib>}",
                            &config, &error);
    if (status != STARDUST_OK) goto failed;

    status = stardust_open(config, &database, &error);
    if (status != STARDUST_OK) goto failed;
    stardust_release(config, NULL);
    config = NULL;

    status = decode("{message-id #uuid \"7b3dd174-0be3-47ad-b4fa-933c297b1760\" method \"eval\" params {code (+ 20 22)}}",
                    &requests, &error);
    if (status != STARDUST_OK) goto failed;
    int64_t accepted = 0;
    status = stardust_submit(database, &requests, 1, &accepted, &error);
    if (status != STARDUST_OK || accepted != 1) goto failed;
    stardust_release(requests, NULL);
    requests = NULL;

    /* This one scalar evaluation has one terminal response. */
    int64_t count = 0;
    status = stardust_poll(database, -1, &response, 1, &count, &error);
    if (status != STARDUST_OK || count != 1) goto failed;
    status = stardust_encode(response, NULL, &encoded, &error);
    if (status != STARDUST_OK) goto failed;
    if (fwrite(encoded.data, 1, (size_t)encoded.length, stdout) !=
        (size_t)encoded.length || fputc('\n', stdout) == EOF) goto cleanup;
    result = 0;
    goto cleanup;

failed:
    diagnostic(status, error);
cleanup:
    stardust_release(encoded.owner, NULL);
    stardust_release(response, NULL);
    stardust_release(requests, NULL);
    stardust_release(config, NULL);
    stardust_release(error, NULL);
    stardust_release(database, NULL);
    return result;
}

It prints a response containing result 42 and three UUIDs. This example has one scalar request and one terminal response. A general client must continue polling and match response IDs, as described in Requests and responses.