Skip to main content

libdoltlite_sys/
lib.rs

1#![expect(non_snake_case, non_camel_case_types)]
2#![cfg_attr(not(test), no_std)]
3pub use self::error::*;
4
5use core::mem;
6#[cfg(not(feature = "loadable_extension"))]
7use core::sync::atomic::{AtomicI32, Ordering};
8
9mod error;
10
11#[must_use]
12pub fn SQLITE_STATIC() -> sqlite3_destructor_type {
13    None
14}
15
16#[must_use]
17pub fn SQLITE_TRANSIENT() -> sqlite3_destructor_type {
18    Some(unsafe { mem::transmute::<isize, unsafe extern "C" fn(*mut core::ffi::c_void)>(-1_isize) })
19}
20
21#[allow(dead_code, clippy::all)]
22mod bindings {
23    include!(concat!(env!("OUT_DIR"), "/bindgen.rs"));
24}
25pub use bindings::*;
26
27#[cfg(all(feature = "remote", not(target_arch = "wasm32")))]
28mod remote {
29    use core::ffi::{c_char, c_int, c_long};
30
31    #[repr(C)]
32    pub struct DoltliteServer {
33        _private: [u8; 0],
34    }
35
36    #[repr(C)]
37    pub struct DoltliteServeOpts {
38        pub zDir: *const c_char,
39        pub port: c_int,
40        pub zBindAddr: *const c_char,
41        pub certFile: *const c_char,
42        pub keyFile: *const c_char,
43        pub authKeysDir: *const c_char,
44        pub audience: *const c_char,
45        pub timeoutMs: c_int,
46    }
47
48    unsafe extern "C" {
49        pub fn doltliteServe(
50            directory: *const c_char,
51            port: c_int,
52            bind_address: *const c_char,
53        ) -> c_int;
54
55        pub fn doltliteServeAsync(
56            directory: *const c_char,
57            port: c_int,
58            bind_address: *const c_char,
59        ) -> *mut DoltliteServer;
60
61        pub fn doltliteServeOpts(options: *const DoltliteServeOpts) -> c_int;
62
63        pub fn doltliteServeAsyncOpts(options: *const DoltliteServeOpts) -> *mut DoltliteServer;
64
65        pub fn doltliteServerStop(server: *mut DoltliteServer);
66
67        pub fn doltliteServerPort(server: *mut DoltliteServer) -> c_int;
68
69        pub fn doltliteCredsVerifyBearer(
70            authorization: *const c_char,
71            expected_audience: *const c_char,
72            authorized_keys_directory: *const c_char,
73            now: c_long,
74            key_id: *mut *mut c_char,
75        ) -> c_int;
76    }
77}
78
79#[cfg(all(feature = "remote", not(target_arch = "wasm32")))]
80pub use remote::*;
81
82#[cfg(not(feature = "loadable_extension"))]
83unsafe extern "C" {
84    fn doltliteInstallAutoExt() -> core::ffi::c_int;
85}
86
87#[cfg(not(feature = "loadable_extension"))]
88static DOLTLITE_INIT_RESULT: AtomicI32 = AtomicI32::new(i32::MIN);
89
90#[cfg(feature = "loadable_extension")]
91pub fn initialize_doltlite() -> core::ffi::c_int {
92    // A loadable extension runs inside an already-initialized host process;
93    // do not load DoltLite's process-wide auto-extension a second time.
94    SQLITE_OK
95}
96
97#[cfg(not(feature = "loadable_extension"))]
98pub fn initialize_doltlite() -> core::ffi::c_int {
99    let existing = DOLTLITE_INIT_RESULT.load(Ordering::Acquire);
100    if existing != i32::MIN {
101        return existing;
102    }
103
104    let result = unsafe { doltliteInstallAutoExt() };
105    match DOLTLITE_INIT_RESULT.compare_exchange(
106        i32::MIN,
107        result,
108        Ordering::AcqRel,
109        Ordering::Acquire,
110    ) {
111        Ok(_) => result,
112        Err(previous) => previous,
113    }
114}
115
116impl Default for sqlite3_vtab {
117    fn default() -> Self {
118        unsafe { mem::zeroed() }
119    }
120}
121
122impl Default for sqlite3_vtab_cursor {
123    fn default() -> Self {
124        unsafe { mem::zeroed() }
125    }
126}