ash_rpc_core/
traits.rs

1//! Core traits for JSON-RPC handlers and processors.
2
3use crate::types::*;
4
5/// Trait for handling JSON-RPC requests and notifications
6pub trait Handler {
7    /// Handle a JSON-RPC request and return a response
8    fn handle_request(&self, request: Request) -> Response;
9
10    /// Handle a JSON-RPC notification (no response expected)
11    fn handle_notification(&self, notification: Notification);
12
13    /// Check if a method is supported
14    fn supports_method(&self, method: &str) -> bool {
15        let _ = method;
16        true
17    }
18
19    /// Get list of supported methods
20    fn get_supported_methods(&self) -> Vec<String> {
21        vec![]
22    }
23
24    /// Get method information for documentation
25    fn get_method_info(&self, method: &str) -> Option<MethodInfo> {
26        let _ = method;
27        None
28    }
29}
30
31/// Trait for processing JSON-RPC messages
32pub trait MessageProcessor {
33    /// Process a single JSON-RPC message
34    fn process_message(&self, message: Message) -> Option<Response>;
35
36    /// Process a batch of JSON-RPC messages
37    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    /// Check if batch processing is supported
45    fn supports_batching(&self) -> bool {
46        true
47    }
48
49    /// Get processor capabilities
50    fn get_capabilities(&self) -> ProcessorCapabilities {
51        ProcessorCapabilities::default()
52    }
53}
54
55/// Method information for documentation and introspection
56#[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    /// Create new method info
66    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    /// Add method description
76    pub fn with_description(mut self, description: impl Into<String>) -> Self {
77        self.description = Some(description.into());
78        self
79    }
80
81    /// Add parameter schema
82    pub fn with_params_schema(mut self, schema: serde_json::Value) -> Self {
83        self.params_schema = Some(schema);
84        self
85    }
86
87    /// Add result schema
88    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}