allframe_core/router/
adapter.rs1use std::{future::Future, pin::Pin};
4
5pub trait ProtocolAdapter: Send + Sync {
10 fn name(&self) -> &str;
12
13 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}