1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
pub mod simple_http_executor;
use crate::workflows::definitions::WorkflowDefinition;
use futures::future::BoxFuture;
use std::collections::HashMap;
use thiserror::Error;
pub struct ReactorExecutionResult {
pub stream_is_valid: bool,
pub workflows_returned: Vec<WorkflowDefinition>,
}
pub trait ReactorExecutor {
fn get_workflow(&self, stream_name: String) -> BoxFuture<'static, ReactorExecutionResult>;
}
pub trait ReactorExecutorGenerator {
fn generate(
&self,
parameters: &HashMap<String, Option<String>>,
) -> Result<Box<dyn ReactorExecutor>, Box<dyn std::error::Error + Sync + Send>>;
}
#[derive(Default)]
pub struct ReactorExecutorFactory {
generators: HashMap<String, Box<dyn ReactorExecutorGenerator>>,
}
#[derive(Error, Debug)]
pub enum RegistrationError {
#[error("A reactor executor generator is already registered with the name '{0}'")]
DuplicateName(String),
}
#[derive(Error, Debug)]
pub enum GenerationError {
#[error("No generators have been registered for the executor name '{0}'")]
NoRegisteredGenerator(String),
}
impl ReactorExecutionResult {
pub fn invalid() -> Self {
ReactorExecutionResult {
stream_is_valid: false,
workflows_returned: Vec::new(),
}
}
pub fn valid(workflows: Vec<WorkflowDefinition>) -> Self {
ReactorExecutionResult {
stream_is_valid: true,
workflows_returned: workflows,
}
}
}
impl ReactorExecutorFactory {
pub fn new() -> Self {
Default::default()
}
pub fn register(
&mut self,
name: String,
generator: Box<dyn ReactorExecutorGenerator>,
) -> Result<(), RegistrationError> {
if self.generators.contains_key(&name) {
return Err(RegistrationError::DuplicateName(name));
}
self.generators.insert(name, generator);
Ok(())
}
pub fn get_generator(
&self,
name: &str,
) -> Result<&dyn ReactorExecutorGenerator, GenerationError> {
match self.generators.get(name) {
Some(generator) => Ok(generator.as_ref()),
None => Err(GenerationError::NoRegisteredGenerator(name.to_string())),
}
}
}