1use crate::types::*;
4
5pub trait Handler {
7 fn handle_request(&self, request: Request) -> Response;
9
10 fn handle_notification(&self, notification: Notification);
12
13 fn supports_method(&self, method: &str) -> bool {
15 let _ = method;
16 true
17 }
18
19 fn get_supported_methods(&self) -> Vec<String> {
21 vec![]
22 }
23
24 fn get_method_info(&self, method: &str) -> Option<MethodInfo> {
26 let _ = method;
27 None
28 }
29}
30
31pub trait MessageProcessor {
33 fn process_message(&self, message: Message) -> Option<Response>;
35
36 fn process_batch(&self, messages: Vec<Message>) -> Vec<Response> {
38 messages
39 .into_iter()
40 .filter_map(|msg| self.process_message(msg))
41 .collect()
42 }
43
44 fn supports_batching(&self) -> bool {
46 true
47 }
48
49 fn get_capabilities(&self) -> ProcessorCapabilities {
51 ProcessorCapabilities::default()
52 }
53}
54
55#[derive(Debug, Clone)]
57pub struct MethodInfo {
58 pub name: String,
59 pub description: Option<String>,
60 pub params_schema: Option<serde_json::Value>,
61 pub result_schema: Option<serde_json::Value>,
62}
63
64impl MethodInfo {
65 pub fn new(name: impl Into<String>) -> Self {
67 Self {
68 name: name.into(),
69 description: None,
70 params_schema: None,
71 result_schema: None,
72 }
73 }
74
75 pub fn with_description(mut self, description: impl Into<String>) -> Self {
77 self.description = Some(description.into());
78 self
79 }
80
81 pub fn with_params_schema(mut self, schema: serde_json::Value) -> Self {
83 self.params_schema = Some(schema);
84 self
85 }
86
87 pub fn with_result_schema(mut self, schema: serde_json::Value) -> Self {
89 self.result_schema = Some(schema);
90 self
91 }
92}
93
94#[derive(Debug, Clone)]
95pub struct ProcessorCapabilities {
96 pub supports_batch: bool,
97 pub supports_notifications: bool,
98 pub max_batch_size: Option<usize>,
99 pub supported_versions: Vec<String>,
100}
101
102impl Default for ProcessorCapabilities {
103 fn default() -> Self {
104 Self {
105 supports_batch: true,
106 supports_notifications: true,
107 max_batch_size: None,
108 supported_versions: vec!["2.0".to_string()],
109 }
110 }
111}