allframe_core/router/
adapter.rs

1//! Protocol adapter trait for supporting multiple protocols
2
3use std::{future::Future, pin::Pin};
4
5/// Protocol adapter trait
6///
7/// Adapters implement this trait to provide protocol-specific
8/// request/response handling while using the unified handler interface.
9pub trait ProtocolAdapter: Send + Sync {
10    /// Get the name of this protocol adapter
11    fn name(&self) -> &str;
12
13    /// Handle a protocol-specific request
14    ///
15    /// The adapter is responsible for:
16    /// - Parsing the incoming request format
17    /// - Extracting parameters for the handler
18    /// - Calling the appropriate handler
19    /// - Formatting the response in protocol-specific format
20    fn handle(
21        &self,
22        request: &str,
23    ) -> Pin<Box<dyn Future<Output = Result<String, String>> + Send + '_>>;
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29
30    struct TestAdapter;
31
32    impl ProtocolAdapter for TestAdapter {
33        fn name(&self) -> &str {
34            "test"
35        }
36
37        fn handle(
38            &self,
39            request: &str,
40        ) -> Pin<Box<dyn Future<Output = Result<String, String>> + Send + '_>> {
41            let response = format!("Handled: {}", request);
42            Box::pin(async move { Ok(response) })
43        }
44    }
45
46    #[tokio::test]
47    async fn test_protocol_adapter() {
48        let adapter = TestAdapter;
49        assert_eq!(adapter.name(), "test");
50
51        let result = adapter.handle("test request").await;
52        assert_eq!(result, Ok("Handled: test request".to_string()));
53    }
54}