| 1 | # Transformation Pipeline |
| 2 | |
| 3 | Middleware-esque API for transforming data. |
| 4 | |
| 5 | ## Features |
| 6 | |
| 7 | - Define a custom data type passed between stages |
| 8 | - Simple pipeline flow exposed to stages: Abort, Finish (early), Skip |
| 9 | - Wrapper functions to run the entire pipeline |
| 10 | |
| 11 | ### Potential Future Features |
| 12 | |
| 13 | - Sub-pipelines (e.g. Routers) |
| 14 | |
| 15 | ## Usage |
| 16 | |
| 17 | ### Installation |
| 18 | |
| 19 | Add `transformation-pipeline` to your `Cargo.toml` file, and add it as an external crate. |
| 20 | |
| 21 | ```rust |
| 22 | extern crate transformation_pipeline; |
| 23 | ``` |
| 24 | |
| 25 | ### Define Data |
| 26 | |
| 27 | Identify or define a datatype that will be passed between stages. |
| 28 | |
| 29 | This can be a built-in, such as `String`, or a custom `struct`. |
| 30 | However, the datatype must have the `Clone` trait defined. |
| 31 | |
| 32 | ```rust |
| 33 | use std::clone::Clone; |
| 34 | |
| 35 | struct User { |
| 36 | name: String, |
| 37 | } |
| 38 | |
| 39 | impl Clone for User { |
| 40 | fn clone(&self) -> Self { |
| 41 | User { |
| 42 | name: self.name.clone(), |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | ``` |
| 47 | |
| 48 | ### Creating Stages |
| 49 | |
| 50 | Create `struct`s for each stage of the pipeline, and implement the `PipelineStage` trait for each stage. |
| 51 | |
| 52 | ```rust |
| 53 | use transformation_pipeline::PipelineStage; |
| 54 | use transformation_pipeline::StageResult; |
| 55 | use transformation_pipeline::StageActions; |
| 56 | |
| 57 | struct MyPipelineStage {} |
| 58 | |
| 59 | impl TransformationStage<User> for MyPipelineStage { |
| 60 | fn run(&self, previous: User) -> StageResult<User> { |
| 61 | let mut name: String = "John ".to_owned(); |
| 62 | name.push_str(&previous.name); |
| 63 | Ok(StageActions::Next(User { |
| 64 | name: name, |
| 65 | })) |
| 66 | } |
| 67 | } |
| 68 | ``` |
| 69 | |
| 70 | See [stage actions](#stage-actions) for more information about the different actions that can be returned. |
| 71 | |
| 72 | ### Create a Pipeline |
| 73 | |
| 74 | Now you can assemble a pipeline out of the created stages. |
| 75 | Each stage must be put in a `Box`, which is a built-in type. |
| 76 | |
| 77 | ```rust |
| 78 | use transformation_pipeline::TransformationPipeline; |
| 79 | let pipeline: TransformationPipeline<User> = TransformationPipeline::new(vec![ |
| 80 | Box::new(MyPipelineStage {}), |
| 81 | // Add other stages here: |
| 82 | // Box::new(MyOtherPipelineStage {}) |
| 83 | ]); |
| 84 | ``` |
| 85 | |
| 86 | ### Using the Pipeline |
| 87 | |
| 88 | Now you can pass data into the pipeline: |
| 89 | |
| 90 | ```rust |
| 91 | let input: User = User { |
| 92 | name: "Doe" |
| 93 | }; |
| 94 | let output = pipeline.run(input).unwrap(); |
| 95 | |
| 96 | assert_eq!(output.name, "John Doe"); |
| 97 | ``` |
| 98 | |
| 99 | ## Documentation |
| 100 | |
| 101 | ### Stage Actions |
| 102 | |
| 103 | Each stage of the pipeline must complete with some "Action". |
| 104 | |
| 105 | #### Next Action |
| 106 | |
| 107 | The standard action is "Next", which passes the given data to the next pipeline stage. |
| 108 | If the stage is the final stage in the pipeline, the given data is returned as the pipeline result. |
| 109 | |
| 110 | ```rust |
| 111 | Ok(StageActions::Next( /* data for next stage */ )) |
| 112 | ``` |
| 113 | |
| 114 | #### Skip Action |
| 115 | |
| 116 | A stage can complete with "Skip", which starts the next pipeline stage as if the current stage never existed. |
| 117 | |
| 118 | This is equivalent to calling: |
| 119 | |
| 120 | ```rust |
| 121 | return Ok(StageActions::Next(previous)); |
| 122 | ``` |
| 123 | |
| 124 | But it can be a little more explicit to what is happening: |
| 125 | |
| 126 | ```rust |
| 127 | if /* action is already completed */ { |
| 128 | return Ok(StageActions::Skip); |
| 129 | } |
| 130 | /* Do action */ |
| 131 | Ok(StageActions::Next( /* ... */ )) |
| 132 | ``` |
| 133 | |
| 134 | #### Finish Action |
| 135 | |
| 136 | A stage can cause the pipeline to immediately complete with the "Finish" action. |
| 137 | This returns the given data as the pipeline result, and does not run any further stages. |
| 138 | |
| 139 | ```rust |
| 140 | Ok(StageActions::Finish(/* final data */ )) |
| 141 | ``` |
| 142 | |
| 143 | #### Jump Action |
| 144 | |
| 145 | A stage can skip subsequent steps in the pipeline with the "Jump" action. |
| 146 | This passes the given data to a stage further down the pipeline, and doesn't run any stages in between. |
| 147 | |
| 148 | ```rust |
| 149 | // SAME AS Next(): |
| 150 | return Ok(StageActions::Skip(0, /* next data */ )); |
| 151 | |
| 152 | // Skips 1 stage: |
| 153 | return Ok(StageActions::Skip(1, /* data to pass to the 2nd stage */ )); |
| 154 | ``` |
| 155 | |
| 156 | #### Abort Action |
| 157 | |
| 158 | A stage can complete with the "Abort" action, causing the entire pipeline to abort with an error. |
| 159 | |
| 160 | ```rust |
| 161 | Ok(StageActions::Abort) |
| 162 | ``` |
| 163 | |
| 164 | ## (Anti-)Purpose/Alternatives |
| 165 | |
| 166 | This package is not designed to: |
| 167 | |
| 168 | - Handle different data types between stages (e.g. successive maps) |
| 169 | - Have multiple functions exposed by pipeline stages (e.g. fancy plugins) |
| 170 | |
| 171 | [cargo-plugin](https://github.com/Geal/cargo-plugin) may be a better alternative for general-purpose plugins. |