| 1 | extern crate transformation_pipeline; |
| 2 | |
| 3 | use std::clone::Clone; |
| 4 | use transformation_pipeline::{TransformationStage, StageActions, StageResult, TransformationPipeline}; |
| 5 | |
| 6 | struct User { |
| 7 | |
| 8 | name: String, |
| 9 | |
| 10 | } |
| 11 | |
| 12 | impl Clone for User { |
| 13 | |
| 14 | fn clone(&self) -> Self { |
| 15 | User { |
| 16 | name: self.name.clone(), |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | } |
| 21 | |
| 22 | struct CapitializeName {} |
| 23 | |
| 24 | impl TransformationStage<User> for CapitializeName { |
| 25 | |
| 26 | fn run(&self, previous: User) -> StageResult<User> { |
| 27 | |
| 28 | let mut name: Vec<char> = previous.name.chars().collect(); |
| 29 | name[0] = name[0].to_uppercase().nth(0).unwrap(); |
| 30 | |
| 31 | Ok(StageActions::Next(User { |
| 32 | name: name.into_iter().collect(), |
| 33 | })) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | I normally don't assume that all individuals are male. This is just a test. |
| 39 | */ |
| 40 | struct ImposeMr {} |
| 41 | |
| 42 | impl TransformationStage<User> for ImposeMr { |
| 43 | |
| 44 | fn run(&self, previous: User) -> StageResult<User> { |
| 45 | |
| 46 | let mut name: String = "Mr. ".to_owned(); |
| 47 | name.push_str(&previous.name); |
| 48 | |
| 49 | Ok(StageActions::Next(User { |
| 50 | name: name, |
| 51 | })) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn run_pipeline() { |
| 57 | let pipeline: TransformationPipeline<User> = TransformationPipeline::new(vec![ |
| 58 | Box::new(CapitializeName {}), |
| 59 | Box::new(ImposeMr {}), |
| 60 | ]); |
| 61 | |
| 62 | assert_eq!(pipeline.run(User { name: "john".to_owned() }).unwrap().name, "Mr. John"); |
| 63 | assert_eq!(pipeline.run(User { name: "John Doe".to_owned() }).unwrap().name, "Mr. John Doe"); |
| 64 | } |