mirror of
https://github.com/gorules/zen.git
synced 2026-10-04 08:02:28 +00:00
189 lines
6.5 KiB
C
189 lines
6.5 KiB
C
#include <stdarg.h>
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
|
|
typedef enum ZenLoaderConfigKind {
|
|
ZenLoaderConfigKind_Static,
|
|
ZenLoaderConfigKind_Filesystem,
|
|
ZenLoaderConfigKind_Zip,
|
|
} ZenLoaderConfigKind;
|
|
|
|
typedef struct ZenDecisionStruct {
|
|
uint8_t _data[0];
|
|
} ZenDecisionStruct;
|
|
|
|
/**
|
|
* CResult can be seen as Either<Result, Error>. It cannot, and should not, be initialized
|
|
* manually. Instead, use error or ok functions for initialisation.
|
|
*/
|
|
typedef struct ZenResult_c_char {
|
|
char *result;
|
|
uint8_t error;
|
|
char *details;
|
|
} ZenResult_c_char;
|
|
|
|
typedef struct ZenEngineEvaluationOptions {
|
|
bool trace;
|
|
uint8_t max_depth;
|
|
} ZenEngineEvaluationOptions;
|
|
|
|
typedef struct ZenEngineStruct {
|
|
uint8_t _data[0];
|
|
} ZenEngineStruct;
|
|
|
|
/**
|
|
* CResult can be seen as Either<Result, Error>. It cannot, and should not, be initialized
|
|
* manually. Instead, use error or ok functions for initialisation.
|
|
*/
|
|
typedef struct ZenResult_ZenEngineStruct {
|
|
struct ZenEngineStruct *result;
|
|
uint8_t error;
|
|
char *details;
|
|
} ZenResult_ZenEngineStruct;
|
|
|
|
typedef struct ZenEngineLoaderConfig {
|
|
enum ZenLoaderConfigKind kind;
|
|
const char *content;
|
|
const uint8_t *bytes;
|
|
uintptr_t bytes_len;
|
|
} ZenEngineLoaderConfig;
|
|
|
|
typedef struct ZenCustomNodeResult {
|
|
char *content;
|
|
char *error;
|
|
} ZenCustomNodeResult;
|
|
|
|
/**
|
|
* CResult can be seen as Either<Result, Error>. It cannot, and should not, be initialized
|
|
* manually. Instead, use error or ok functions for initialisation.
|
|
*/
|
|
typedef struct ZenResult_ZenDecisionStruct {
|
|
struct ZenDecisionStruct *result;
|
|
uint8_t error;
|
|
char *details;
|
|
} ZenResult_ZenDecisionStruct;
|
|
|
|
typedef struct ZenEngineEvaluateBatchRequest {
|
|
const char *key;
|
|
const char *context;
|
|
} ZenEngineEvaluateBatchRequest;
|
|
|
|
/**
|
|
* CResult can be seen as Either<Result, Error>. It cannot, and should not, be initialized
|
|
* manually. Instead, use error or ok functions for initialisation.
|
|
*/
|
|
typedef struct ZenResult_c_int {
|
|
int *result;
|
|
uint8_t error;
|
|
char *details;
|
|
} ZenResult_c_int;
|
|
|
|
typedef struct ZenDecisionLoaderResult {
|
|
char *content;
|
|
char *error;
|
|
} ZenDecisionLoaderResult;
|
|
|
|
typedef struct ZenDecisionLoaderResult (*ZenDecisionLoaderNativeCallback)(const char *key);
|
|
|
|
typedef struct ZenCustomNodeResult (*ZenCustomNodeNativeCallback)(const char *request);
|
|
|
|
/**
|
|
* Frees ZenDecision
|
|
*/
|
|
void zen_decision_free(struct ZenDecisionStruct *decision);
|
|
|
|
/**
|
|
* Evaluates ZenDecision
|
|
* Caller is responsible for freeing context and ZenResult.
|
|
*/
|
|
struct ZenResult_c_char zen_decision_evaluate(const struct ZenDecisionStruct *decision,
|
|
const char *context_ptr,
|
|
struct ZenEngineEvaluationOptions options);
|
|
|
|
/**
|
|
* Create a new ZenEngine instance, caller is responsible for freeing the returned reference
|
|
* by calling zen_engine_free.
|
|
*/
|
|
struct ZenEngineStruct *zen_engine_new(void);
|
|
|
|
/**
|
|
* Creates a new ZenEngine instance from a loader configuration, caller is responsible for
|
|
* freeing the returned reference by calling zen_engine_free.
|
|
*/
|
|
struct ZenResult_ZenEngineStruct zen_engine_new_with_loader_config(struct ZenEngineLoaderConfig config,
|
|
struct ZenCustomNodeResult (*maybe_custom_node)(const char *request));
|
|
|
|
/**
|
|
* Frees the ZenEngine instance reference from the memory
|
|
*/
|
|
void zen_engine_free(struct ZenEngineStruct *engine);
|
|
|
|
/**
|
|
* Creates a Decision using a reference of DecisionEngine and content (JSON)
|
|
* Caller is responsible for freeing content and ZenResult.
|
|
*/
|
|
struct ZenResult_ZenDecisionStruct zen_engine_create_decision(const struct ZenEngineStruct *engine,
|
|
const char *content);
|
|
|
|
/**
|
|
* Evaluates rules engine using a DecisionEngine reference via loader
|
|
* Caller is responsible for freeing: key, context and ZenResult.
|
|
*/
|
|
struct ZenResult_c_char zen_engine_evaluate(const struct ZenEngineStruct *engine,
|
|
const char *key,
|
|
const char *context,
|
|
struct ZenEngineEvaluationOptions options);
|
|
|
|
/**
|
|
* Evaluates a batch of requests in parallel using a DecisionEngine reference via loader.
|
|
* Returns a JSON array of { success, data?, error? } envelopes in request order.
|
|
* Caller is responsible for freeing: requests and ZenResult.
|
|
*/
|
|
struct ZenResult_c_char zen_engine_evaluate_batch(const struct ZenEngineStruct *engine,
|
|
const struct ZenEngineEvaluateBatchRequest *requests,
|
|
uintptr_t requests_len,
|
|
struct ZenEngineEvaluationOptions options);
|
|
|
|
/**
|
|
* Loads a Decision through DecisionEngine
|
|
* Caller is responsible for freeing: key and ZenResult.
|
|
*/
|
|
struct ZenResult_ZenDecisionStruct zen_engine_get_decision(const struct ZenEngineStruct *engine,
|
|
const char *key);
|
|
|
|
struct ZenResult_c_char zen_evaluate_expression(const char *expression, const char *context);
|
|
|
|
/**
|
|
* Evaluate unary expression, responsible for freeing expression and context
|
|
* True = 1
|
|
* False = 0
|
|
*/
|
|
struct ZenResult_c_int zen_evaluate_unary_expression(const char *expression, const char *context);
|
|
|
|
/**
|
|
* Evaluate unary expression, responsible for freeing expression and context
|
|
* True = 1
|
|
* False = 0
|
|
*/
|
|
struct ZenResult_c_char zen_evaluate_template(const char *template_, const char *context);
|
|
|
|
/**
|
|
* Creates a new ZenEngine instance with loader, caller is responsible for freeing the returned reference
|
|
* by calling zen_engine_free.
|
|
*/
|
|
struct ZenEngineStruct *zen_engine_new_native(ZenDecisionLoaderNativeCallback loader_callback,
|
|
ZenCustomNodeNativeCallback custom_node_callback);
|
|
|
|
/**
|
|
* Creates a DecisionEngine for using GoLang handler (optional). Caller is responsible for freeing DecisionEngine.
|
|
*/
|
|
struct ZenEngineStruct *zen_engine_new_golang(const uintptr_t *maybe_loader,
|
|
const uintptr_t *maybe_custom_node);
|
|
|
|
/**
|
|
* Creates a DecisionEngine from a loader configuration using GoLang handler (optional). Caller is responsible for freeing DecisionEngine.
|
|
*/
|
|
struct ZenResult_ZenEngineStruct zen_engine_new_golang_with_loader_config(struct ZenEngineLoaderConfig config,
|
|
const uintptr_t *maybe_custom_node);
|