1use std::future::Future;
4use std::pin::Pin;
5use std::sync::Arc;
6
7use futures_core::Stream;
8use futures_util::StreamExt;
9use serde::Serialize;
10
11use crate::errors::SeamError;
12
13pub type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
14
15pub type BoxStream<T> = Pin<Box<dyn Stream<Item = T> + Send>>;
16
17pub fn map_stream_output<T>(
18 stream: BoxStream<Result<T, SeamError>>,
19) -> BoxStream<Result<serde_json::Value, SeamError>>
20where
21 T: Serialize + Send + 'static,
22{
23 Box::pin(stream.map(|item| {
24 item
25 .and_then(|value| serde_json::to_value(value).map_err(|e| SeamError::internal(e.to_string())))
26 }))
27}
28
29pub type HandlerFn = Arc<
30 dyn Fn(serde_json::Value, serde_json::Value) -> BoxFuture<Result<serde_json::Value, SeamError>>
31 + Send
32 + Sync,
33>;
34
35pub struct SubscriptionParams {
36 pub input: serde_json::Value,
37 pub ctx: serde_json::Value,
38 pub last_event_id: Option<String>,
39}
40
41pub type SubscriptionHandlerFn = Arc<
42 dyn Fn(
43 SubscriptionParams,
44 ) -> BoxFuture<Result<BoxStream<Result<serde_json::Value, SeamError>>, SeamError>>
45 + Send
46 + Sync,
47>;
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub enum ProcedureType {
51 Query,
52 Command,
53}
54
55pub struct ProcedureDef {
56 pub name: String,
57 pub proc_type: ProcedureType,
58 pub input_schema: serde_json::Value,
59 pub output_schema: serde_json::Value,
60 pub error_schema: Option<serde_json::Value>,
61 pub context_keys: Vec<String>,
62 pub suppress: Option<Vec<String>>,
63 pub cache: Option<serde_json::Value>,
64 pub handler: HandlerFn,
65}
66
67pub struct SubscriptionDef {
68 pub name: String,
69 pub input_schema: serde_json::Value,
70 pub output_schema: serde_json::Value,
71 pub error_schema: Option<serde_json::Value>,
72 pub context_keys: Vec<String>,
73 pub suppress: Option<Vec<String>>,
74 pub handler: SubscriptionHandlerFn,
75}
76
77pub struct StreamParams {
78 pub input: serde_json::Value,
79 pub ctx: serde_json::Value,
80}
81
82pub type StreamHandlerFn = Arc<
83 dyn Fn(
84 StreamParams,
85 ) -> BoxFuture<Result<BoxStream<Result<serde_json::Value, SeamError>>, SeamError>>
86 + Send
87 + Sync,
88>;
89
90pub type UploadHandlerFn = Arc<
91 dyn Fn(
92 serde_json::Value,
93 SeamFileHandle,
94 serde_json::Value,
95 ) -> BoxFuture<Result<serde_json::Value, SeamError>>
96 + Send
97 + Sync,
98>;
99
100pub struct SeamFileHandle {
102 pub name: Option<String>,
103 pub content_type: Option<String>,
104 pub data: bytes::Bytes,
105}
106
107pub struct StreamDef {
108 pub name: String,
109 pub input_schema: serde_json::Value,
110 pub chunk_output_schema: serde_json::Value,
111 pub error_schema: Option<serde_json::Value>,
112 pub context_keys: Vec<String>,
113 pub suppress: Option<Vec<String>>,
114 pub handler: StreamHandlerFn,
115}
116
117pub struct UploadDef {
118 pub name: String,
119 pub input_schema: serde_json::Value,
120 pub output_schema: serde_json::Value,
121 pub error_schema: Option<serde_json::Value>,
122 pub context_keys: Vec<String>,
123 pub suppress: Option<Vec<String>>,
124 pub handler: UploadHandlerFn,
125}