Skip to main content

zynk_schema/
endpoint.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::TypeRef;
5
6/// Supported endpoint surfaces. These map to Zynk's stable wire routes.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
8#[serde(rename_all = "snake_case")]
9pub enum EndpointKind {
10    Rpc,
11    Channel,
12    Upload,
13    Static,
14    Ws,
15}
16
17/// A callable parameter exposed to generated clients.
18#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
19#[serde(rename_all = "camelCase")]
20pub struct Param {
21    pub source_name: String,
22    pub wire_name: String,
23    pub ty: TypeRef,
24    pub required: bool,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub default: Option<Value>,
27}
28
29impl Param {
30    pub fn new(
31        source_name: impl Into<String>,
32        wire_name: impl Into<String>,
33        ty: TypeRef,
34        required: bool,
35    ) -> Self {
36        Self {
37            source_name: source_name.into(),
38            wire_name: wire_name.into(),
39            ty,
40            required,
41            default: None,
42        }
43    }
44}
45
46/// One server endpoint in the Zynk API graph.
47#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
48#[serde(rename_all = "camelCase")]
49pub struct Endpoint {
50    pub name: String,
51    pub kind: EndpointKind,
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub module: Option<String>,
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub doc: Option<String>,
56    #[serde(default, skip_serializing_if = "Vec::is_empty")]
57    pub params: Vec<Param>,
58    pub returns: TypeRef,
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub channel_item: Option<TypeRef>,
61    #[serde(skip_serializing_if = "Option::is_none")]
62    pub file_param: Option<String>,
63    #[serde(default)]
64    pub multi_file: bool,
65    #[serde(skip_serializing_if = "Option::is_none")]
66    pub max_size: Option<u64>,
67    #[serde(default, skip_serializing_if = "Vec::is_empty")]
68    pub allowed_types: Vec<String>,
69    #[serde(default, skip_serializing_if = "Vec::is_empty")]
70    pub server_events: Vec<Param>,
71    #[serde(default, skip_serializing_if = "Vec::is_empty")]
72    pub client_events: Vec<Param>,
73}
74
75impl Endpoint {
76    pub fn new(name: impl Into<String>, kind: EndpointKind, returns: TypeRef) -> Self {
77        Self {
78            name: name.into(),
79            kind,
80            module: None,
81            doc: None,
82            params: Vec::new(),
83            returns,
84            channel_item: None,
85            file_param: None,
86            multi_file: false,
87            max_size: None,
88            allowed_types: Vec::new(),
89            server_events: Vec::new(),
90            client_events: Vec::new(),
91        }
92    }
93}