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
|
extern crate transformation_pipeline;
use std::clone::Clone;
use transformation_pipeline::{TransformationStage, StageActions, StageResult, TransformationPipeline};
struct User {
name: String,
}
impl Clone for User {
fn clone(&self) -> Self {
User {
name: self.name.clone(),
}
}
}
struct CapitializeName {}
impl TransformationStage<User> for CapitializeName {
fn run(&self, previous: User) -> StageResult<User> {
let mut name: Vec<char> = previous.name.chars().collect();
name[0] = name[0].to_uppercase().nth(0).unwrap();
Ok(StageActions::Next(User {
name: name.into_iter().collect(),
}))
}
}
/*
I normally don't assume that all individuals are male. This is just a test.
*/
struct ImposeMr {}
impl TransformationStage<User> for ImposeMr {
fn run(&self, previous: User) -> StageResult<User> {
let mut name: String = "Mr. ".to_owned();
name.push_str(&previous.name);
Ok(StageActions::Next(User {
name: name,
}))
}
}
#[test]
fn run_pipeline() {
let pipeline: TransformationPipeline<User> = TransformationPipeline::new(vec![
Box::new(CapitializeName {}),
Box::new(ImposeMr {}),
]);
assert_eq!(pipeline.run(User { name: "john".to_owned() }).unwrap().name, "Mr. John");
assert_eq!(pipeline.run(User { name: "John Doe".to_owned() }).unwrap().name, "Mr. John Doe");
}
|