C API

Policy

Load inline policies or governed manifests into an opaque policy set. Select and identify policies without exposing parsed ruleset structures.

Declarations from <maelys/datalog.h> and its optional C11 builders <maelys/datalog_builders.h>. For explanations and complete examples, see the Policy guide.

The examples use source, manifest_json, policy, diagnostic and check_status supplied by the page reference context.

The examples use policy, the loaded document-access policy, and check_status, the error-handling helper from the page reference context.

Handle: policy_t

C · handleSince ≤ v0.1.0-alpha.2
policy_t

#include <maelys/datalog.h>

typedef struct maelys_datalog_policy maelys_datalog_policy_t;
Opaque handle owning one loaded policy or a manifest set. Select policies by index through sessions; release the handle with policy_free().

Creation

policy_load_inline()

Load an inline policy.

C · functionSince ≤ v0.1.0-alpha.2
policy_load_inline()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_policy_load_inline(
const char *domain,
const char *policy_id,
const char *source,
size_t source_length,
maelys_datalog_policy_t **out_policy,
maelys_datalog_diagnostic_t *out_diagnostic
);
Loads one policy directly from Datalog source text and validates it against a registered domain. This path does not read a manifest.
Arguments
domainconst char *in
Previously registered domain name.
policy_idconst char *in
Stable application-level identifier for this policy.
sourceconst char *in
Exact MAELYS-DATALOG-v2 source bytes.
source_lengthsize_tin
Number of bytes in source.
Outputs

Written on success.

out_policymaelys_datalog_policy_t **out
Receives an opaque policy on success and NULL on failure.

out_diagnostic: optional diagnostic details on failure; pass NULL when not needed.

Return status

maelys_datalog_status_t — OK on success.

MAELYS_DATALOG_STATUS_OK on success; otherwise a fail-closed public status.

Example
maelys_datalog_status_t status = maelys_datalog_policy_load_inline(
    "documents", "documents.main", source, strlen(source), &policy, &diagnostic);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
size_t count = 0;
status = maelys_datalog_policy_count(policy, &count);
check_status(status);
printf("policies: %zu\n", count);

policy_load_manifest()

Load a manifest policy set.

C · functionSince ≤ v0.1.0-alpha.2
policy_load_manifest()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_policy_load_manifest(
const char *manifest_path,
unsigned flags,
maelys_datalog_policy_t **out_policy,
maelys_datalog_diagnostic_t *out_diagnostic
);
Loads the policies listed in a manifest file, checking their source hashes, domain declarations and public query permissions.
Arguments
manifest_pathconst char *in
Path to the policy-set manifest.
Outputs

Written on success.

out_policymaelys_datalog_policy_t **out
Receives the complete opaque policy set.

out_diagnostic: optional diagnostic details on failure; pass NULL when not needed.

Return status

maelys_datalog_status_t — OK on success.

MAELYS_DATALOG_STATUS_OK on success; otherwise a fail-closed public status.

Example
maelys_datalog_status_t status = maelys_datalog_policy_load_manifest(
    "manifest.json", MAELYS_DATALOG_PUBLIC_ALLOW_NONE, &policy, &diagnostic);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
size_t count = 0;
status = maelys_datalog_policy_count(policy, &count);
check_status(status);
printf("policies: %zu\n", count);

policy_load_manifest_buffer()

Load a manifest bundle from memory.

C · functionSince v0.15.0
policy_load_manifest_buffer()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_policy_load_manifest_buffer(
const char *manifest_json,
size_t manifest_length,
const maelys_datalog_policy_bundle_entry_t *bundle,
size_t bundle_count,
unsigned flags,
maelys_datalog_policy_t **out_policy,
maelys_datalog_diagnostic_t *out_diagnostic
);
Load a governed policy set from manifest JSON and source bytes already in memory, without reading policy files.
Arguments
manifest_jsonconst char *in
Borrowed JSON bytes for this call.
manifest_lengthsize_tin
Exact JSON byte count; no NUL terminator required.
Entries keyed by manifest policy ID, borrowed for this call.
bundle_countsize_tin
Number of source entries.
Outputs

Written on success.

out_policymaelys_datalog_policy_t **out
Receives the complete policy set on success, NULL on failure.

out_diagnostic: optional diagnostic details on failure; pass NULL when not needed.

Return status

maelys_datalog_status_t — OK on success.

Returns OK only for a completely validated set; missing source IDs return NOT_FOUND and no partial policy escapes.

Example
const maelys_datalog_policy_bundle_entry_t bundle[] = {
    { "documents.main", source, strlen(source) },
};
maelys_datalog_status_t status = maelys_datalog_policy_load_manifest_buffer(
    manifest_json, strlen(manifest_json), bundle, sizeof(bundle) / sizeof(bundle[0]),
    MAELYS_DATALOG_PUBLIC_ALLOW_NONE, &policy, &diagnostic);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
size_t count = 0;
status = maelys_datalog_policy_count(policy, &count);
check_status(status);
printf("policies: %zu\n", count);

policy_load_manifest_text()

C · functionSince v0.10.0
policy_load_manifest_text()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_policy_load_manifest_text(
const char *manifest_json,
size_t manifest_length,
const maelys_datalog_policy_bundle_entry_t *bundle,
size_t bundle_count,
unsigned flags,
maelys_datalog_policy_t **out_policy,
maelys_datalog_diagnostic_t *out_diagnostic
);
Loads a manifest bundle from memory using the historical text-loading contract. If default_profile is omitted, it defaults to MAELYS-DATALOG-v2; policy_load_manifest_buffer uses the file-loader default, enforce. Source bytes are borrowed only for the call.
Arguments
manifest_jsonconst char *in
Manifest JSON bytes borrowed only for this load.
manifest_lengthsize_tin
Exact manifest byte count.
Caller-supplied policy sources, matched by their policy IDs.
bundle_countsize_tin
Number of entries in the source bundle.
Outputs

Written on success.

out_policymaelys_datalog_policy_t **out
Receives the loaded opaque policy set on success.

out_diagnostic: optional diagnostic details on failure; pass NULL when not needed.

Return status

maelys_datalog_status_t — OK on success.

MAELYS_DATALOG_STATUS_OK on success; otherwise a fail-closed public status.

Example
const maelys_datalog_policy_bundle_entry_t entries[] = {
    {"documents.main", source, strlen(source)}
};
maelys_datalog_status_t status = maelys_datalog_policy_load_manifest_text(
    historical_manifest_json, strlen(historical_manifest_json), entries, sizeof(entries) / sizeof(entries[0]),
    MAELYS_DATALOG_PUBLIC_ALLOW_NONE, &policy, &diagnostic);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
size_t count = 0;
check_status(maelys_datalog_policy_count(policy, &count));
printf("policies: %zu\n", count);

Functions

policy_count()

Count loaded policies.

C · functionSince ≤ v0.1.0-alpha.2
policy_count()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_policy_count(
const maelys_datalog_policy_t *policy,
size_t *out_count
);
Reads how many policies belong to a loaded set, so the application can choose a valid policy index when creating a session.
Arguments
policyconst maelys_datalog_policy_t *handleread
Loaded policy set to count.
Outputs

Written on success.

out_countsize_t *out
Receives the number of selectable policies in this handle.
Return status

maelys_datalog_status_t — OK on success.

MAELYS_DATALOG_STATUS_OK on success; otherwise a fail-closed public status.

Example
size_t count = 0;
maelys_datalog_status_t status = maelys_datalog_policy_count(policy, &count);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("policies: %zu\n", count);

policy_id()

C · functionSince v0.20.0
policy_id()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_policy_id(
const maelys_datalog_policy_t *policy,
size_t policy_index,
const char **out_policy_id
);
Reads the identifier of one enabled policy without preparing a session or allocating storage. The returned string is borrowed from the loaded policy set.
Arguments
policyconst maelys_datalog_policy_t *handleread
Loaded policy set whose enabled policy is inspected.
policy_indexsize_tin
Zero-based enabled policy index, in the manifest order used by policy_count and session_create.
Outputs

Written on success.

out_policy_idconst char **out
Receives a borrowed identifier string valid only while policy remains live; failure leaves this pointer unchanged.
Return status

maelys_datalog_status_t — OK on success.

OK on success; NOT_FOUND for an invalid policy index and INVALID_STATE for a released handle whose storage remains addressable. A freed pointer is invalid. Failure leaves out_policy_id unchanged.

Example
const char *policy_id = NULL;
maelys_datalog_status_t status = maelys_datalog_policy_id(policy, 0, &policy_id);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("policy id: %s\n", policy_id);
status: ok
policy id: documents.main

policy_fingerprint()

Fingerprint the loaded set.

C · functionSince ≤ v0.1.0-alpha.2
policy_fingerprint()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_policy_fingerprint(
const maelys_datalog_policy_t *policy,
char out_fingerprint[MAELYS_DATALOG_PUBLIC_FINGERPRINT_BYTES]
);
Reads the identity of the complete loaded policy set. Use it to distinguish policy sets, rather than the execution configuration of a session.
Arguments
policyconst maelys_datalog_policy_t *handleread
Loaded policy set to fingerprint.
Outputs

Written on success.

Return status

maelys_datalog_status_t — OK on success.

MAELYS_DATALOG_STATUS_OK on success; otherwise a fail-closed public status.

Example
char fingerprint[MAELYS_DATALOG_PUBLIC_FINGERPRINT_BYTES] = {0};
maelys_datalog_status_t status = maelys_datalog_policy_fingerprint(policy, fingerprint);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("fingerprint: %s\n", fingerprint);
status: ok
fingerprint: b05b9b1d3e99c8029abc2ffa0bee3ae7295f620870f4b0dd3172b12e13a735e8

policy_stat_get()

Read a compiled policy statistic.

C · functionSince v0.16.0
policy_stat_get()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_policy_stat_get(
const maelys_datalog_policy_t *policy,
size_t policy_index,
maelys_datalog_policy_stat_t statistic,
size_t *out_value
);
Read a selected policy's compiled predicate, policy-fact or normalized-rule count without preparing a session.
Arguments
policyconst maelys_datalog_policy_t *handleread
Loaded policy set to inspect.
policy_indexsize_tin
Zero-based enabled policy index.
Predicate, fact or rule count selector.
Outputs

Written on success.

out_valuesize_t *out
Receives the selected count; failure leaves it unchanged.
Return status

maelys_datalog_status_t — OK on success.

OK on success; NOT_FOUND for an invalid index, UNSUPPORTED for an unknown selector and INVALID_STATE for a released handle. Failure leaves the output unchanged.

Example
size_t predicates = 0;
maelys_datalog_status_t status = maelys_datalog_policy_stat_get(
    policy, 0, MAELYS_DATALOG_POLICY_PREDICATE_COUNT, &predicates);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("predicates: %zu\n", predicates);
status: ok
predicates: 4

policy_reads_predicate()

C · functionSince v0.25.0
policy_reads_predicate()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_policy_reads_predicate(
const maelys_datalog_policy_t *policy,
size_t policy_index,
const char *name,
size_t arity,
int *out_reads
);
Checks whether one compiled policy reads a named predicate in a rule body, without preparing a session. Reads include positive and negated atoms and aggregate sources; heads and static facts do not count.
Arguments
policyconst maelys_datalog_policy_t *handleread
Loaded policy set to inspect.
policy_indexsize_tin
Zero-based index of an enabled policy in manifest order.
nameconst char *in
Name of the predicate whose rule-body use is checked.
aritysize_tin
Number of arguments in that predicate.
Outputs

Written on success.

out_readsint *out
Receives 1 when the selected program reads the predicate, or 0 otherwise.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with out_reads set to 0 or 1; an unknown name/arity pair yields 0. Returns INVALID_ARGUMENT for NULL arguments, NOT_FOUND for an invalid policy index, or INVALID_STATE for a released handle whose storage remains addressable. Failures leave out_reads unchanged. A construction whose reads cannot be established conservatively reads every registered pair.

Example
int reads = 0;
maelys_datalog_status_t status = maelys_datalog_policy_reads_predicate(
    policy, 0, "owns", 2, &reads);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("reads owns/2: %s\n", reads ? "yes" : "no");
status: ok
reads owns/2: yes

Release

policy_free()

Release the loaded policy set.

C · functionSince ≤ v0.1.0-alpha.2
policy_free()

#include <maelys/datalog.h>

maelys_datalog_status_t
maelys_datalog_policy_free(
maelys_datalog_policy_t *policy
);
Releases a loaded policy-set handle when the application no longer needs it for creating sessions.
Arguments
policymaelys_datalog_policy_t *handlereleased
Loaded set to release after preparing any required sessions.
Return status

maelys_datalog_status_t — OK on success.

Returns OK when the handle is released; an invalid handle or state returns an error.

Example
maelys_datalog_status_t status = maelys_datalog_policy_free(policy);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
policy = NULL;

Constants

Default loading permission
NameValueMeaning
PUBLIC_ALLOW_NONE
Since v0.7.1
0x0No optional permissions: require declared policy constants and reject enabled test-only policies.
Loading permissions
NameValueMeaning
PUBLIC_ALLOW_TEST_ONLY
Since ≤ v0.1.0-alpha.2
0x1Admit enabled entries whose manifest mode is test_only.
PUBLIC_ALLOW_UNDECLARED_POLICY_ATOMS
Since ≤ v0.1.0-alpha.2
0x2Admit policy-local string constants absent from the domain's atoms.
Policy atom bounds
NameValueMeaning
PUBLIC_MAX_POLICY_ATOMS
Since ≤ v0.1.0-alpha.2
256Published maximum number of policy atoms; also reported by the MAX_POLICY_ATOMS runtime limit.
PUBLIC_MAX_POLICY_ATOM_BYTES
Since ≤ v0.1.0-alpha.2
63Published maximum byte length of a policy atom; also reported by the MAX_POLICY_ATOM_BYTES runtime limit.
Fingerprint buffer
NameValueMeaning
PUBLIC_FINGERPRINT_BYTES
Since ≤ v0.1.0-alpha.2
65Required caller-owned fingerprint buffer size, including the terminating NUL, as declared by policy_fingerprint.

Struct: policy_bundle_entry_t

C · structSince v0.10.0
policy_bundle_entry_t

#include <maelys/datalog.h>

Pairs a manifest policy ID with caller-supplied source bytes for one buffer load.
Fields
policy_idconst char *
Policy ID listed by the manifest; lookup uses this ID, not bundle order.
srcconst char *
Source bytes borrowed only for the load call.
src_lensize_t
Exact source byte count; a NUL terminator is not required.
Typedef
typedef struct {
    const char *policy_id;
    const char *src;
    size_t src_len;
} maelys_datalog_policy_bundle_entry_t;

status_t ↗ · diagnostic_t ↗.

Enum: policy_stat_t

C · enumSince v0.16.0
policy_stat_t

#include <maelys/datalog.h>

Selects which immutable compiled-program count to read from one loaded policy.
Members
POLICY_PREDICATE_COUNT1
Compiled predicate count.
POLICY_FACT_COUNT2
Policy-fact count.
POLICY_RULE_COUNT3
Normalized-rule count.
Typedef
typedef enum {
    MAELYS_DATALOG_POLICY_PREDICATE_COUNT = 1,
    MAELYS_DATALOG_POLICY_FACT_COUNT,
    MAELYS_DATALOG_POLICY_RULE_COUNT
} maelys_datalog_policy_stat_t;

status_t ↗ · policy_t ↗.

Usage guides

Policy guide · Full program.