Skip to main content

sacp_test/
lib.rs

1use sacp::*;
2use serde::{Deserialize, Serialize};
3
4pub mod arrow_proxy;
5pub mod test_binaries;
6
7/// A mock transport for doctests that panics if actually used.
8/// This is only for documentation examples that don't actually run.
9pub struct MockTransport;
10
11impl<R: Role> ConnectTo<R> for MockTransport {
12    async fn connect_to(self, _client: impl ConnectTo<R::Counterpart>) -> Result<(), Error> {
13        panic!("MockTransport should never be used in running code - it's only for doctests")
14    }
15}
16
17// Mock request/response types
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct MyRequest {}
20
21#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct MyResponse {
23    pub status: String,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct ProcessRequest {
28    pub data: String,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct ProcessResponse {
33    pub result: String,
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct ProcessStarted {}
38
39#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct AnalyzeRequest {
41    pub data: String,
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct AnalysisStarted {
46    pub job_id: u32,
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct QueryRequest {
51    pub id: u64,
52}
53
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct QueryResponse {
56    pub data: String,
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct ValidateRequest {
61    pub data: String,
62}
63
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct ValidateResponse {
66    pub is_valid: bool,
67    pub error: Option<String>,
68}
69
70#[derive(Debug, Clone, Serialize, Deserialize)]
71pub struct ExecuteRequest {}
72
73#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct ExecuteResponse {
75    pub result: String,
76}
77
78#[derive(Debug, Clone, Serialize, Deserialize)]
79pub struct OtherRequest {}
80
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct OtherResponse {
83    pub value: String,
84}
85
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct ProxyRequest {
88    pub inner_request: UntypedMessage,
89}
90
91// Mock notification types
92#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct SessionUpdate {}
94
95#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct StatusUpdate {
97    pub message: String,
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct ProcessComplete {
102    pub result: String,
103}
104
105#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct AnalysisComplete {
107    pub result: String,
108}
109
110#[derive(Debug, Clone, Serialize, Deserialize)]
111pub struct QueryComplete {}
112
113// Implement JsonRpcMessage for all types
114macro_rules! impl_jr_message {
115    ($type:ty, $method:expr) => {
116        impl JsonRpcMessage for $type {
117            fn matches_method(method: &str) -> bool {
118                method == $method
119            }
120            fn method(&self) -> &str {
121                $method
122            }
123            fn to_untyped_message(&self) -> Result<UntypedMessage, crate::Error> {
124                UntypedMessage::new($method, self)
125            }
126            fn parse_message(method: &str, params: &impl Serialize) -> Result<Self, crate::Error> {
127                if !Self::matches_method(method) {
128                    return Err(crate::Error::method_not_found());
129                }
130                sacp::util::json_cast(params)
131            }
132        }
133    };
134}
135
136// Implement JsonRpcRequest for request types
137macro_rules! impl_jr_request {
138    ($req:ty, $resp:ty, $method:expr) => {
139        impl_jr_message!($req, $method);
140        impl JsonRpcRequest for $req {
141            type Response = $resp;
142        }
143    };
144}
145
146// Implement JsonRpcNotification for notification types
147macro_rules! impl_jr_notification {
148    ($type:ty, $method:expr) => {
149        impl_jr_message!($type, $method);
150        impl JsonRpcNotification for $type {}
151    };
152}
153
154impl_jr_request!(MyRequest, MyResponse, "myRequest");
155impl_jr_request!(ProcessRequest, ProcessResponse, "processRequest");
156impl_jr_request!(AnalyzeRequest, AnalysisStarted, "analyzeRequest");
157impl_jr_request!(QueryRequest, QueryResponse, "queryRequest");
158impl_jr_request!(ValidateRequest, ValidateResponse, "validateRequest");
159impl_jr_request!(ExecuteRequest, ExecuteResponse, "executeRequest");
160impl_jr_request!(OtherRequest, OtherResponse, "otherRequest");
161impl_jr_request!(ProxyRequest, serde_json::Value, "proxyRequest");
162
163impl_jr_notification!(SessionUpdate, "sessionUpdate");
164impl_jr_notification!(StatusUpdate, "statusUpdate");
165impl_jr_notification!(ProcessComplete, "processComplete");
166impl_jr_notification!(AnalysisComplete, "analysisComplete");
167impl_jr_notification!(QueryComplete, "queryComplete");
168impl_jr_notification!(ProcessStarted, "processStarted");
169
170// Implement JsonRpcResponse for response types
171macro_rules! impl_jr_response_payload {
172    ($type:ty, $method:expr) => {
173        impl JsonRpcResponse for $type {
174            fn into_json(self, _method: &str) -> Result<serde_json::Value, crate::Error> {
175                Ok(serde_json::to_value(self)?)
176            }
177            fn from_value(_method: &str, value: serde_json::Value) -> Result<Self, crate::Error> {
178                Ok(serde_json::from_value(value)?)
179            }
180        }
181    };
182}
183
184impl_jr_response_payload!(MyResponse, "myRequest");
185impl_jr_response_payload!(ProcessResponse, "processRequest");
186impl_jr_response_payload!(AnalysisStarted, "analyzeRequest");
187impl_jr_response_payload!(QueryResponse, "queryRequest");
188impl_jr_response_payload!(ValidateResponse, "validateRequest");
189impl_jr_response_payload!(ExecuteResponse, "executeRequest");
190impl_jr_response_payload!(OtherResponse, "otherRequest");
191
192// Mock async functions
193pub async fn expensive_analysis(_data: &str) -> Result<String, crate::Error> {
194    Ok("analysis result".into())
195}
196
197pub async fn expensive_operation(_data: &str) -> Result<String, crate::Error> {
198    Ok("operation result".into())
199}
200
201pub fn update_session_state(_update: &SessionUpdate) -> Result<(), crate::Error> {
202    Ok(())
203}
204
205pub fn process(data: &str) -> Result<String, crate::Error> {
206    Ok(data.to_string())
207}
208
209// Helper to create a mock connection for examples
210pub fn mock_connection() -> Builder<Client> {
211    Client.builder()
212}
213
214pub trait Make {
215    fn make() -> Self;
216}
217
218impl<T> Make for T {
219    fn make() -> Self {
220        panic!()
221    }
222}