Skip to main content

spicedb_embedded_sys/
lib.rs

1//! FFI bindings to the SpiceDB C shared library (built from Go or downloaded per target).
2//!
3//! Provides an in-memory server and raw C functions that take in marshalled gRPC requests and
4//! return marshalled responses or error strings.
5
6mod instance;
7mod memory;
8
9pub use instance::{dispose, start};
10pub use memory::{RpcError, RpcResult};
11
12pub mod memory_transport {
13    //! Safe, typed wrappers for memory-transport RPCs (marshall/unmarshall in this crate).
14    pub use crate::memory::{
15        RpcError, RpcResult, check_bulk_permissions, check_permission, delete_relationships,
16        expand_permission_tree, read_schema, write_relationships, write_schema,
17    };
18}
19
20use std::os::raw::{c_char, c_int, c_uchar, c_ulonglong};
21
22#[link(name = "spicedb")]
23unsafe extern "C" {
24    pub(crate) fn spicedb_start(options_json: *const c_char) -> *mut c_char;
25    pub(crate) fn spicedb_dispose(handle: c_ulonglong) -> *mut c_char;
26    pub(crate) fn spicedb_free(ptr: *mut c_char);
27
28    /// Frees a byte buffer returned by RPC FFI functions.
29    pub(crate) fn spicedb_free_bytes(ptr: *mut std::ffi::c_void);
30
31    /// Invokes PermissionsService.CheckPermission. Request/response are marshalled protobuf (authzed.api.v1).
32    /// On success: `*out_response_bytes` and `*out_response_len` are set; caller frees `*out_response_bytes` with `spicedb_free_bytes`.
33    /// On error: `*out_response_bytes` is NULL, `*out_error` is set; caller frees `*out_error` with `spicedb_free`.
34    pub(crate) fn spicedb_permissions_check_permission(
35        handle: c_ulonglong,
36        request_bytes: *const c_uchar,
37        request_len: c_int,
38        out_response_bytes: *mut *mut c_uchar,
39        out_response_len: *mut c_int,
40        out_error: *mut *mut c_char,
41    );
42
43    /// Invokes PermissionsService.WriteRelationships. Same ABI as other RPC FFI functions.
44    pub(crate) fn spicedb_permissions_write_relationships(
45        handle: c_ulonglong,
46        request_bytes: *const c_uchar,
47        request_len: c_int,
48        out_response_bytes: *mut *mut c_uchar,
49        out_response_len: *mut c_int,
50        out_error: *mut *mut c_char,
51    );
52
53    /// Invokes SchemaService.WriteSchema. Same ABI as other RPC FFI functions.
54    pub(crate) fn spicedb_schema_write_schema(
55        handle: c_ulonglong,
56        request_bytes: *const c_uchar,
57        request_len: c_int,
58        out_response_bytes: *mut *mut c_uchar,
59        out_response_len: *mut c_int,
60        out_error: *mut *mut c_char,
61    );
62
63    pub(crate) fn spicedb_permissions_delete_relationships(
64        handle: c_ulonglong,
65        request_bytes: *const c_uchar,
66        request_len: c_int,
67        out_response_bytes: *mut *mut c_uchar,
68        out_response_len: *mut c_int,
69        out_error: *mut *mut c_char,
70    );
71
72    pub(crate) fn spicedb_permissions_check_bulk_permissions(
73        handle: c_ulonglong,
74        request_bytes: *const c_uchar,
75        request_len: c_int,
76        out_response_bytes: *mut *mut c_uchar,
77        out_response_len: *mut c_int,
78        out_error: *mut *mut c_char,
79    );
80
81    pub(crate) fn spicedb_permissions_expand_permission_tree(
82        handle: c_ulonglong,
83        request_bytes: *const c_uchar,
84        request_len: c_int,
85        out_response_bytes: *mut *mut c_uchar,
86        out_response_len: *mut c_int,
87        out_error: *mut *mut c_char,
88    );
89
90    pub(crate) fn spicedb_schema_read_schema(
91        handle: c_ulonglong,
92        request_bytes: *const c_uchar,
93        request_len: c_int,
94        out_response_bytes: *mut *mut c_uchar,
95        out_response_len: *mut c_int,
96        out_error: *mut *mut c_char,
97    );
98}