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 type SubscriptionHandlerFn = Arc<
22	dyn Fn(
23			serde_json::Value,
24			serde_json::Value,
25		) -> BoxFuture<Result<BoxStream<Result<serde_json::Value, SeamError>>, SeamError>>
26		+ Send
27		+ Sync,
28>;
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub enum ProcedureType {
32	Query,
33	Command,
34}
35
36pub struct ProcedureDef {
37	pub name: String,
38	pub proc_type: ProcedureType,
39	pub input_schema: serde_json::Value,
40	pub output_schema: serde_json::Value,
41	pub error_schema: Option<serde_json::Value>,
42	pub context_keys: Vec<String>,
43	pub suppress: Option<Vec<String>>,
44	pub cache: Option<serde_json::Value>,
45	pub handler: HandlerFn,
46}
47
48pub struct SubscriptionDef {
49	pub name: String,
50	pub input_schema: serde_json::Value,
51	pub output_schema: serde_json::Value,
52	pub error_schema: Option<serde_json::Value>,
53	pub context_keys: Vec<String>,
54	pub suppress: Option<Vec<String>>,
55	pub handler: SubscriptionHandlerFn,
56}
57
58/// Stream reuses the same handler signature as subscription (returns BoxStream),
59/// but the SSE protocol differs: stream data events carry an incrementing `id`.
60pub type StreamHandlerFn = SubscriptionHandlerFn;
61
62pub type UploadHandlerFn = Arc<
63	dyn Fn(
64			serde_json::Value,
65			SeamFileHandle,
66			serde_json::Value,
67		) -> BoxFuture<Result<serde_json::Value, SeamError>>
68		+ Send
69		+ Sync,
70>;
71
72/// File received from a multipart upload request.
73pub struct SeamFileHandle {
74	pub name: Option<String>,
75	pub content_type: Option<String>,
76	pub data: bytes::Bytes,
77}
78
79pub struct StreamDef {
80	pub name: String,
81	pub input_schema: serde_json::Value,
82	pub chunk_output_schema: serde_json::Value,
83	pub error_schema: Option<serde_json::Value>,
84	pub context_keys: Vec<String>,
85	pub suppress: Option<Vec<String>>,
86	pub handler: StreamHandlerFn,
87}
88
89pub struct UploadDef {
90	pub name: String,
91	pub input_schema: serde_json::Value,
92	pub output_schema: serde_json::Value,
93	pub error_schema: Option<serde_json::Value>,
94	pub context_keys: Vec<String>,
95	pub suppress: Option<Vec<String>>,
96	pub handler: UploadHandlerFn,
97}