kotoba_handler/
executor.rs1use crate::error::{HandlerError, Result};
4use crate::types::{HandlerContext, HandlerResult, ExecutionMode};
5use crate::handler::UnifiedHandler;
6use std::sync::Arc;
7
8pub struct HandlerExecutor<T: crate::KeyValueStore + 'static> {
10 handler: Arc<UnifiedHandler<T>>,
11 execution_mode: ExecutionMode,
12}
13
14impl<T: crate::KeyValueStore + 'static> HandlerExecutor<T> {
15 pub fn new(handler: Arc<UnifiedHandler<T>>) -> Self {
17 Self {
18 handler,
19 execution_mode: ExecutionMode::Sync,
20 }
21 }
22
23 pub fn with_mode(mut self, mode: ExecutionMode) -> Self {
25 self.execution_mode = mode;
26 self
27 }
28
29 pub async fn execute_sync(&self, content: &str, context: HandlerContext) -> Result<String> {
31 match self.execution_mode {
32 ExecutionMode::Sync => {
33 self.handler.execute(content, context).await
34 }
35 ExecutionMode::Async => {
36 self.execute_async_internal(content, context).await
37 }
38 ExecutionMode::Streaming => {
39 self.execute_streaming_internal(content, context).await
40 }
41 }
42 }
43
44 pub async fn execute_async(&self, content: &str, context: HandlerContext) -> Result<String> {
46 self.handler.execute(content, context).await
47 }
48
49 pub async fn execute_streaming(&self, content: &str, context: HandlerContext) -> Result<String> {
51 self.handler.execute(content, context).await
54 }
55
56 pub async fn execute_file(&self, file_path: &str, context: HandlerContext) -> Result<String> {
58 self.handler.execute_file(file_path, context).await
59 }
60
61 pub async fn execute_batch(
63 &self,
64 requests: Vec<(String, HandlerContext)>
65 ) -> Result<Vec<String>> {
66 let mut results = Vec::new();
67
68 for (content, context) in requests {
69 let result = self.handler.execute(&content, context).await?;
70 results.push(result);
71 }
72
73 Ok(results)
74 }
75
76 async fn execute_async_internal(&self, content: &str, context: HandlerContext) -> Result<String> {
79 let handler = Arc::clone(&self.handler);
81 let content = content.to_string();
82
83 tokio::spawn(async move {
84 handler.execute(&content, context).await
85 })
86 .await
87 .map_err(|e| HandlerError::Execution(format!("Async execution failed: {}", e)))?
88 }
89
90 async fn execute_streaming_internal(&self, content: &str, context: HandlerContext) -> Result<String> {
91 self.handler.execute(content, context).await
94 }
95}
96
97impl<T: crate::KeyValueStore + 'static> Default for HandlerExecutor<T> {
98 fn default() -> Self {
99 let handler = Arc::new(UnifiedHandler::new(todo!("KeyValueStore implementation needed")));
100 Self::new(handler)
101 }
102}
103
104pub struct HandlerExecutorBuilder<T: crate::KeyValueStore + 'static> {
106 handler: Option<Arc<UnifiedHandler<T>>>,
107 execution_mode: ExecutionMode,
108}
109
110impl<T: crate::KeyValueStore + 'static> HandlerExecutorBuilder<T> {
111 pub fn new() -> Self {
112 Self {
113 handler: None,
114 execution_mode: ExecutionMode::Sync,
115 }
116 }
117
118 pub fn with_handler(mut self, handler: Arc<UnifiedHandler<T>>) -> Self {
119 self.handler = Some(handler);
120 self
121 }
122
123 pub fn with_mode(mut self, mode: ExecutionMode) -> Self {
124 self.execution_mode = mode;
125 self
126 }
127
128 pub fn build(self) -> HandlerExecutor<T> {
129 let handler = self.handler.unwrap_or_else(|| Arc::new(UnifiedHandler::new(todo!("KeyValueStore implementation needed"))));
130 HandlerExecutor::new(handler).with_mode(self.execution_mode)
131 }
132}
133
134impl<T: crate::KeyValueStore + 'static> Default for HandlerExecutorBuilder<T> {
135 fn default() -> Self {
136 Self::new()
137 }
138}