#ifndef HYDRANT_H
#define HYDRANT_H

/* run hydrant inside another program. hydrant serves its usual http api, ideally on a
 * `unix:` bind, and the embedder talks to it over that. see ffi/src/lib.rs for details. */

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct HydrantHandle hydrant_t;

/* starts hydrant from a json object of the HYDRANT_* settings the binary reads from its
 * environment, plus RUST_LOG. HYDRANT_API_BIND is required, and `none` runs without an api.
 * returns NULL and sets *err on failure. ignore SIGPIPE before calling this, the way
 * rust binaries do at startup, or a write to a socket whose peer left kills the process. */
hydrant_t *hydrant_start(const char *settings_json, char **err);

/* blocks until hydrant stops. returns -1 with *err set when it failed, or 0 once hydrant_shutdown
 * stopped it. a failed hydrant still holds its database until hydrant_shutdown. */
int hydrant_wait(const hydrant_t *handle, char **err);

/* stops hydrant and waits up to timeout_ms for its database to close, so its memory is
 * given back and hydrant_start can open the same database again. returns -1 with *err set
 * if it's still open after that, or if it was already shut down. */
int hydrant_shutdown(const hydrant_t *handle, uint64_t timeout_ms, char **err);

/* frees a handle, telling it to stop first if hydrant_shutdown wasn't called. nothing may
 * still be blocked in hydrant_wait on it. */
void hydrant_free(hydrant_t *handle);

/* frees an error string from this library. */
void hydrant_free_string(char *s);

#ifdef __cplusplus
}
#endif

#endif
