Skip to main content

seam_server/
procedure.rs

1/* src/server/core/rust/src/procedure.rs */
2
3use std::future::Future;
4use std::pin::Pin;
5use std::sync::Arc;
6
7use futures_core::Stream;
8
9use crate::errors::SeamError;
10
11pub type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
12
13pub type BoxStream<T> = Pin<Box<dyn Stream<Item = T> + Send>>;
14
15pub type HandlerFn = Arc<
16	dyn Fn(serde_json::Value, serde_json::Value) -> BoxFuture<Result<serde_json::Value, SeamError>>
17		+ Send
18		+ Sync,
19>;
20
21pub struct SubscriptionParams {
22	pub input: serde_json::Value,
23	pub ctx: serde_json::Value,
24	pub last_event_id: Option<String>,
25}
26
27pub type SubscriptionHandlerFn = Arc<
28	dyn Fn(
29			SubscriptionParams,
30		) -> BoxFuture<Result<BoxStream<Result<serde_json::Value, SeamError>>, SeamError>>
31		+ Send
32		+ Sync,
33>;
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum ProcedureType {
37	Query,
38	Command,
39}
40
41pub struct ProcedureDef {
42	pub name: String,
43	pub proc_type: ProcedureType,
44	pub input_schema: serde_json::Value,
45	pub output_schema: serde_json::Value,
46	pub error_schema: Option<serde_json::Value>,
47	pub context_keys: Vec<String>,
48	pub suppress: Option<Vec<String>>,
49	pub cache: Option<serde_json::Value>,
50	pub handler: HandlerFn,
51}
52
53pub struct SubscriptionDef {
54	pub name: String,
55	pub input_schema: serde_json::Value,
56	pub output_schema: serde_json::Value,
57	pub error_schema: Option<serde_json::Value>,
58	pub context_keys: Vec<String>,
59	pub suppress: Option<Vec<String>>,
60	pub handler: SubscriptionHandlerFn,
61}
62
63pub struct StreamParams {
64	pub input: serde_json::Value,
65	pub ctx: serde_json::Value,
66}
67
68pub type StreamHandlerFn = Arc<
69	dyn Fn(
70			StreamParams,
71		) -> BoxFuture<Result<BoxStream<Result<serde_json::Value, SeamError>>, SeamError>>
72		+ Send
73		+ Sync,
74>;
75
76pub type UploadHandlerFn = Arc<
77	dyn Fn(
78			serde_json::Value,
79			SeamFileHandle,
80			serde_json::Value,
81		) -> BoxFuture<Result<serde_json::Value, SeamError>>
82		+ Send
83		+ Sync,
84>;
85
86/// File received from a multipart upload request.
87pub struct SeamFileHandle {
88	pub name: Option<String>,
89	pub content_type: Option<String>,
90	pub data: bytes::Bytes,
91}
92
93pub struct StreamDef {
94	pub name: String,
95	pub input_schema: serde_json::Value,
96	pub chunk_output_schema: serde_json::Value,
97	pub error_schema: Option<serde_json::Value>,
98	pub context_keys: Vec<String>,
99	pub suppress: Option<Vec<String>>,
100	pub handler: StreamHandlerFn,
101}
102
103pub struct UploadDef {
104	pub name: String,
105	pub input_schema: serde_json::Value,
106	pub output_schema: serde_json::Value,
107	pub error_schema: Option<serde_json::Value>,
108	pub context_keys: Vec<String>,
109	pub suppress: Option<Vec<String>>,
110	pub handler: UploadHandlerFn,
111}