Multi-phase polymorphic pipeline with traits

Rust Hard traits

Task «Multi-phase polymorphic pipeline with traits»

Implement a system of traits `Stage`, `Pipeline`, and `Transform` that allows building data processing pipelines. The `Stage` trait defines the method `process(&self, input: i32) -> i32`. The `Pipeline` trait defines the method `run(&self, stages: Vec<Box<dyn Stage>>, input: i32) -> i32`, which sequentially applies all stages. The `Transform` trait defines the method `transform(&self, value: i32) -> i32`, which can modify the value according to some rule. Implement the structures `AddStage`, `MultiplyStage`, `NegateStage`, each of which implements `Stage`. Also implement the structure `SimplePipeline`, which implements `Pipeline`. Then write a function that takes a string with commands (`add`, `mul`, `neg`) and numbers, creates the corresponding stages, runs the pipeline with an initial value of 0, and outputs the result. Commands are space-separated: `add 5 mul 2 neg`. Constraints: input value is `i32`, the result is guaranteed to fit into `i32`.

Input Format

A string with commands and numbers separated by spaces. For example: 'add 5 mul 2 neg'.

Output Format

An integer is the result of processing an initial value of 0 through a pipeline of stages.

Examples

Example 1

INPUT
add 5 mul 2 neg
OUTPUT
-10

Example 2

INPUT
mul 3 add 10 neg
OUTPUT
-10

Example 3

INPUT
5 1 2 3 4 5
OUTPUT
0

Example 4

INPUT
add 100 mul 0
OUTPUT
0

Example 5

INPUT
4
OUTPUT
0
main.rs