1use crate::{ResultCode, Value};
2use std::{
3 ffi::{c_char, c_void},
4 fmt::Display,
5};
6
7pub type ScalarFunction = unsafe extern "C" fn(argc: i32, *const Value) -> Value;
8
9pub type RegisterScalarFn =
10 unsafe extern "C" fn(ctx: *mut c_void, name: *const c_char, func: ScalarFunction) -> ResultCode;
11
12pub type RegisterAggFn = unsafe extern "C" fn(
13 ctx: *mut c_void,
14 name: *const c_char,
15 args: i32,
16 init: InitAggFunction,
17 step: StepFunction,
18 finalize: FinalizeFunction,
19) -> ResultCode;
20
21pub type InitAggFunction = unsafe extern "C" fn() -> *mut AggCtx;
22pub type StepFunction = unsafe extern "C" fn(ctx: *mut AggCtx, argc: i32, argv: *const Value);
23pub type FinalizeFunction = unsafe extern "C" fn(ctx: *mut AggCtx) -> Value;
24
25#[repr(C)]
26pub struct AggCtx {
27 pub state: *mut c_void,
28}
29
30pub trait AggFunc {
31 type State: Default;
32 type Error: Display;
33 const NAME: &'static str;
34 const ARGS: i32;
35
36 fn step(state: &mut Self::State, args: &[Value]);
37 fn finalize(state: Self::State) -> Result<Value, Self::Error>;
38}