#pragma once

#include <stddef.h>
#include <stdint.h>

/**
 * Initialize a folder with the given name and name length.
 *
 * On success, return non-NULL. The return value represents the accumulator and
 * will be passed to leek_extension_folder_{step,finalize,free}.
 *
 * On error, return NULL and assign to the *error... values.
 */
void* leek_extension_folder_initialize(uint8_t const* name,
                                       size_t  name_length,
                                       uint8_t const** error_message,
                                       size_t* error_message_length);

/**
 * Step the folder given an entry, and update the accumulator.
 *
 * On success, return zero.
 *
 * On error, return non-zero and assign to the *error... values.
 */
int leek_extension_folder_step(void*   accumulator,
                               uint8_t const* entry,
                               size_t  entry_length,
                               uint8_t const** error_message,
                               size_t* error_message_length);

/**
 * Finalize the accumulator, reporting its value.
 *
 * This should NOT free the accumulator; leek_extension_folder_step and
 * leek_extension_folder_finalize may still be called afterwards (in particular,
 * this will happen many times during a SCANL).
 *
 * On success, return zero and assign to *value and *value_length the
 * value and the length of the value that the accumulator represents.
 *
 * *value will not be dereferenced after any of
 * leek_extension_folder_{step,finalize,free} have been called with the same
 * accumulator.
 *
 * On error, return non-zero and assign to the *error... values.
 */
int leek_extension_folder_finalize(void*   accumulator,
                                   uint8_t const** value,
                                   size_t* value_length,
                                   uint8_t const** error_message,
                                   size_t* error_message_length);

/**
 * Free the accumulator. It will not be used anymore.
 */
void leek_extension_folder_free(void* accumulator);
