Skip to main content

interstice_sdk_core/
registry.rs

1use interstice_abi::{
2    IntersticeTypeDef, QuerySchema, ReducerSchema, SubscriptionSchema, TableSchema,
3};
4use std::{
5    collections::HashMap,
6    sync::{Arc, Mutex},
7};
8
9#[repr(C)]
10pub struct ReducerRegistration {
11    pub reducer: unsafe extern "C" fn(i32, i32) -> i64,
12    pub schema: fn() -> ReducerSchema,
13}
14
15#[repr(C)]
16pub struct TableRegistration {}
17
18#[repr(C)]
19pub struct SubscriptionRegistration {}
20
21pub type TableSchemaFn = fn() -> TableSchema;
22pub type ReducerSchemaFn = fn() -> ReducerSchema;
23pub type QuerySchemaFn = fn() -> QuerySchema;
24pub type SubscriptionSchemaFn = fn() -> SubscriptionSchema;
25pub type IntersticeTypeDefFn = fn() -> IntersticeTypeDef;
26
27lazy_static::lazy_static! {
28    pub static ref TABLE_REGISTRY: Arc<Mutex<Vec<TableSchemaFn>>> = Arc::new(Mutex::new(Vec::new()));
29    pub static ref REDUCER_REGISTRY: Arc<Mutex<Vec<ReducerSchemaFn>>> = Arc::new(Mutex::new(Vec::new()));
30    pub static ref QUERY_REGISTRY: Arc<Mutex<Vec<QuerySchemaFn>>> = Arc::new(Mutex::new(Vec::new()));
31    pub static ref SUBSCRIPTION_REGISTRY: Arc<Mutex<Vec<SubscriptionSchemaFn>>> = Arc::new(Mutex::new(Vec::new()));
32    pub static ref INTERSTICE_TYPE_DEFINITION_REGISTRY: Arc<Mutex<Vec<IntersticeTypeDefFn>>> = Arc::new(Mutex::new(Vec::new()));
33}
34
35/// Called by each `#[table]` macro to register its schema function
36pub fn register_table(f: TableSchemaFn) {
37    TABLE_REGISTRY.lock().unwrap().push(f);
38}
39
40/// Called by each `#[reducer]` macro to register its schema function
41pub fn register_reducer(f: ReducerSchemaFn) {
42    REDUCER_REGISTRY.lock().unwrap().push(f);
43}
44
45/// Called by each `#[query]` macro to register its schema function
46pub fn register_query(f: QuerySchemaFn) {
47    QUERY_REGISTRY.lock().unwrap().push(f);
48}
49
50/// Called by each `#[reducer]` macro to register its potential subscription schema function
51pub fn register_subscription(s: SubscriptionSchemaFn) {
52    SUBSCRIPTION_REGISTRY.lock().unwrap().push(s);
53}
54
55/// Called by each `#[derive(IntersticeType)]` macro to register its TypeDef function
56pub fn register_type_def(s: IntersticeTypeDefFn) {
57    INTERSTICE_TYPE_DEFINITION_REGISTRY.lock().unwrap().push(s);
58}
59
60pub fn collect_tables() -> Vec<TableSchema> {
61    TABLE_REGISTRY.lock().unwrap().iter().map(|f| f()).collect()
62}
63
64pub fn collect_reducers() -> Vec<ReducerSchema> {
65    REDUCER_REGISTRY
66        .lock()
67        .unwrap()
68        .iter()
69        .map(|f| f())
70        .collect()
71}
72
73pub fn collect_queries() -> Vec<QuerySchema> {
74    QUERY_REGISTRY.lock().unwrap().iter().map(|f| f()).collect()
75}
76
77pub fn collect_subscriptions() -> Vec<SubscriptionSchema> {
78    SUBSCRIPTION_REGISTRY
79        .lock()
80        .unwrap()
81        .iter()
82        .map(|f| f())
83        .collect()
84}
85
86pub fn collect_type_definitions() -> HashMap<String, IntersticeTypeDef> {
87    let mut result = HashMap::new();
88    for interstice_type_def_fn in INTERSTICE_TYPE_DEFINITION_REGISTRY.lock().unwrap().iter() {
89        let interstice_type_def = interstice_type_def_fn();
90        result.insert(interstice_type_def.get_name().clone(), interstice_type_def);
91    }
92    return result;
93}