transformer_once_demo/
transformer_once_demo.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025.
4 *    3-Prism Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9
10use prism3_function::{BoxTransformerOnce, TransformerOnce};
11
12fn main() {
13    println!("=== TransformerOnce Demo - One-Time Transformation ===\n");
14
15    // ====================================================================
16    // Part 1: BoxTransformerOnce - One-time use
17    // ====================================================================
18    println!("--- BoxTransformerOnce ---");
19    let parse = BoxTransformerOnce::new(|s: String| s.parse::<i32>().unwrap_or(0));
20    println!(
21        "parse.transform(\"42\".to_string()) = {}",
22        parse.transform("42".to_string())
23    );
24    // parse is consumed and cannot be used again
25
26    // Composition
27    let add_one = BoxTransformerOnce::new(|x: i32| x + 1);
28    let double = BoxTransformerOnce::new(|x: i32| x * 2);
29    let to_string = BoxTransformerOnce::new(|x: i32| x.to_string());
30    let pipeline = add_one.and_then(double).and_then(to_string);
31    println!("pipeline(5) = {} (expected: \"12\")", pipeline.transform(5));
32    println!();
33
34    // ====================================================================
35    // Part 2: Practical Examples
36    // ====================================================================
37    println!("=== Practical Examples ===\n");
38
39    // Example 1: String transformation pipeline
40    println!("--- String Transformation Pipeline ---");
41    let to_upper = BoxTransformerOnce::new(|s: String| s.to_uppercase());
42    println!(
43        "to_upper(\"hello\") = {}",
44        to_upper.transform("hello".to_string())
45    );
46    println!();
47
48    // Example 2: Type conversion with ownership transfer
49    println!("--- Type Conversion ---");
50    let into_bytes = BoxTransformerOnce::new(|s: String| s.into_bytes());
51    let bytes = into_bytes.transform("hello".to_string());
52    println!("into_bytes(\"hello\") = {:?}", bytes);
53    println!();
54
55    // Example 3: Complex pipeline
56    println!("--- Complex Pipeline ---");
57    let parser = BoxTransformerOnce::new(|s: String| s.parse::<i32>().unwrap_or(0));
58    let doubler = BoxTransformerOnce::new(|x: i32| x * 2);
59    let formatter = BoxTransformerOnce::new(|x: i32| format!("Result: {}", x));
60    let parse_and_process = parser.and_then(doubler).and_then(formatter);
61
62    println!(
63        "parse_and_process(\"21\") = {}",
64        parse_and_process.transform("21".to_string())
65    );
66    println!();
67
68    // ====================================================================
69    // Part 3: Trait Usage
70    // ====================================================================
71    println!("=== Trait Usage ===\n");
72
73    fn apply_transformer_once<F: TransformerOnce<String, usize>>(f: F, x: String) -> usize {
74        f.transform(x)
75    }
76
77    let length = BoxTransformerOnce::new(|s: String| s.len());
78    println!(
79        "Via trait once: {}",
80        apply_transformer_once(length, "hello".to_string())
81    );
82
83    println!("\n=== Demo Complete ===");
84}