kotoba_handler/
executor.rs

1//! Handler executor for different execution environments
2
3use crate::error::{HandlerError, Result};
4use crate::types::{HandlerContext, HandlerResult, ExecutionMode};
5use crate::handler::UnifiedHandler;
6use std::sync::Arc;
7
8/// Handler executor that manages execution across different environments
9pub 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    /// Create new executor
16    pub fn new(handler: Arc<UnifiedHandler<T>>) -> Self {
17        Self {
18            handler,
19            execution_mode: ExecutionMode::Sync,
20        }
21    }
22
23    /// Set execution mode
24    pub fn with_mode(mut self, mode: ExecutionMode) -> Self {
25        self.execution_mode = mode;
26        self
27    }
28
29    /// Execute handler synchronously
30    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    /// Execute handler asynchronously
45    pub async fn execute_async(&self, content: &str, context: HandlerContext) -> Result<String> {
46        self.handler.execute(content, context).await
47    }
48
49    /// Execute handler with streaming
50    pub async fn execute_streaming(&self, content: &str, context: HandlerContext) -> Result<String> {
51        // Streaming implementation would return a stream
52        // For now, just execute normally
53        self.handler.execute(content, context).await
54    }
55
56    /// Execute from file
57    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    /// Batch execute multiple handlers
62    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    // Private methods
77
78    async fn execute_async_internal(&self, content: &str, context: HandlerContext) -> Result<String> {
79        // Async execution with timeout
80        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        // Streaming execution placeholder
92        // In real implementation, this would return a stream of results
93        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
104/// Builder pattern for HandlerExecutor
105pub 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}