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
use crate::workflows::definitions::{WorkflowStepDefinition, WorkflowStepType};
use crate::workflows::steps::StepCreationResult;
use std::collections::HashMap;
use thiserror::Error;

/// Represents a type that can generate an instance of a workflow step
pub trait StepGenerator {
    /// Creates a brand new instance of a workflow step based on the supplied definition
    fn generate(&self, definition: WorkflowStepDefinition) -> StepCreationResult;
}

/// The workflow step factory allows consumers to register different workflow step generation
/// instances to use for specific workflow step types.  Consumers can then request the factory
/// to generate workflow steps based on the passed in step definition.
#[derive(Default)]
pub struct WorkflowStepFactory {
    generators: HashMap<WorkflowStepType, Box<dyn StepGenerator + Sync + Send>>,
}

/// Errors that can occur when an attempting to register a generator fails
#[derive(Error, Debug)]
pub enum FactoryRegistrationError {
    #[error(
        "The workflow step factory already has a step generator registered with the type '{0}'"
    )]
    DuplicateName(WorkflowStepType),
}

/// Errors that can occur when an attempt to generate a workflow step fails
#[derive(Error, Debug)]
pub enum FactoryCreateError {
    #[error("No workflow step generator is registered for the type '{0}'")]
    NoRegisteredStep(WorkflowStepType),
}

impl WorkflowStepFactory {
    /// Creates a new workflow step factory, with an empty registration
    pub fn new() -> Self {
        Default::default()
    }

    /// Attempts to register a specific generator instance with the specified
    pub fn register(
        &mut self,
        step_type: WorkflowStepType,
        generator: Box<dyn StepGenerator + Sync + Send>,
    ) -> Result<(), FactoryRegistrationError> {
        if self.generators.contains_key(&step_type) {
            return Err(FactoryRegistrationError::DuplicateName(step_type));
        }

        self.generators.insert(step_type, generator);
        Ok(())
    }

    /// Attempts to create a new instance of a workflow step based on a specified definition
    pub fn create_step(
        &self,
        definition: WorkflowStepDefinition,
    ) -> Result<StepCreationResult, FactoryCreateError> {
        let generator = match self.generators.get(&definition.step_type) {
            Some(generator) => generator,
            None => return Err(FactoryCreateError::NoRegisteredStep(definition.step_type)),
        };

        Ok(generator.generate(definition))
    }
}