Transformation Pipeline
Middleware-esque API for transforming data.
Features
- Define a custom data type passed between stages
- Simple pipeline flow exposed to stages: Abort, Finish (early), Skip
- Wrapper functions to run the entire pipeline
Potential Future Features
- Sub-pipelines (e.g. Routers)
Usage
Installation
Add transformation-pipeline
to your Cargo.toml
file, and add it as an external crate.
extern crate transformation_pipeline;
Define Data
Identify or define a datatype that will be passed between stages.
This can be a built-in, such as String
, or a custom struct
.
However, the datatype must have the Clone
trait defined.
use Clone;
Creating Stages
Create struct
s for each stage of the pipeline, and implement the PipelineStage
trait for each stage.
use PipelineStage;
use StageResult;
use StageActions;
See stage actions for more information about the different actions that can be returned.
Create a Pipeline
Now you can assemble a pipeline out of the created stages.
Each stage must be put in a Box
, which is a built-in type.
use TransformationPipeline;
let pipeline: = new;
Using the Pipeline
Now you can pass data into the pipeline:
let input: User = User ;
let output = pipeline.run.unwrap;
assert_eq!;
Documentation
Stage Actions
Each stage of the pipeline must complete with some "Action".
Next Action
The standard action is "Next", which passes the given data to the next pipeline stage. If the stage is the final stage in the pipeline, the given data is returned as the pipeline result.
Ok
Skip Action
A stage can complete with "Skip", which starts the next pipeline stage as if the current stage never existed.
This is equivalent to calling:
return Ok;
But it can be a little more explicit to what is happening:
if /* action is already completed */
/* Do action */
Ok
Finish Action
A stage can cause the pipeline to immediately complete with the "Finish" action. This returns the given data as the pipeline result, and does not run any further stages.
Ok
Jump Action
A stage can skip subsequent steps in the pipeline with the "Jump" action. This passes the given data to a stage further down the pipeline, and doesn't run any stages in between.
// SAME AS Next():
return Ok;
// Skips 1 stage:
return Ok;
Abort Action
A stage can complete with the "Abort" action, causing the entire pipeline to abort with an error.
Ok
(Anti-)Purpose/Alternatives
This package is not designed to:
- Handle different data types between stages (e.g. successive maps)
- Have multiple functions exposed by pipeline stages (e.g. fancy plugins)
cargo-plugin may be a better alternative for general-purpose plugins.