util_demo/
util_demo.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025.
4 *    3-Prism Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! # Util Demo
10//!
11//! Demonstrates the usage of Pair and Triple utility types.
12//!
13//! # Author
14//!
15//! Haixing Hu
16
17use prism3_core::{Pair, Triple};
18
19fn main() {
20    println!("=== Pair Demo ===\n");
21
22    // Create Pair using new method
23    let pair1 = Pair::new("name", "Alice");
24    println!("pair1 (using new): {}", pair1);
25
26    // Create Pair using struct literal (public fields)
27    let mut pair2 = Pair {
28        first: 1,
29        second: 100,
30    };
31    println!("pair2 (using struct literal): {}", pair2);
32
33    // Direct field access (public fields)
34    pair2.first = 42;
35    pair2.second = 200;
36    println!("pair2 (after modification): {}", pair2);
37
38    // Using methods
39    let pair3 = Pair::new(10, 20);
40    println!("pair3: {}", pair3);
41    println!("  first: {}, second: {}", pair3.first(), pair3.second());
42
43    // Map operations
44    let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
45    println!("pair4 (after mapping): {}", pair4);
46
47    // Swap
48    let pair5 = Pair::new("key", 123);
49    let swapped = pair5.swap();
50    println!("pair5: {}, swapped: {}", pair5, swapped);
51
52    // Convert to tuple
53    let (f, s) = pair5.into_tuple();
54    println!("Converted to tuple: ({}, {})", f, s);
55
56    println!("\n=== Triple Demo ===\n");
57
58    // Create Triple using new method
59    let triple1 = Triple::new("Alice", 30, true);
60    println!("triple1 (using new): {}", triple1);
61
62    // Create Triple using struct literal (public fields)
63    let mut triple2 = Triple {
64        first: 1,
65        second: 2,
66        third: 3,
67    };
68    println!("triple2 (using struct literal): {}", triple2);
69
70    // Direct field access (public fields)
71    triple2.first = 10;
72    triple2.second = 20;
73    triple2.third = 30;
74    println!("triple2 (after modification): {}", triple2);
75
76    // Using methods
77    let triple3 = Triple::new(100, 200, 300);
78    println!("triple3: {}", triple3);
79    println!(
80        "  first: {}, second: {}, third: {}",
81        triple3.first(),
82        triple3.second(),
83        triple3.third()
84    );
85
86    // Map operations
87    let triple4 = triple3
88        .map_first(|x| x / 10)
89        .map_second(|x| x / 10)
90        .map_third(|x| x / 10);
91    println!("triple4 (after mapping): {}", triple4);
92
93    // Convert to tuple
94    let (f, s, t) = triple4.into_tuple();
95    println!("Converted to tuple: ({}, {}, {})", f, s, t);
96
97    println!("\n=== Advanced Usage ===\n");
98
99    // Pair with different types
100    let config = Pair {
101        first: "database_url",
102        second: "postgresql://localhost/mydb",
103    };
104    println!("Config: {}", config);
105
106    // Triple as a record
107    let record = Triple {
108        first: "user_001",
109        second: "Alice",
110        third: 25,
111    };
112    println!(
113        "User Record: ID={}, Name={}, Age={}",
114        record.first, record.second, record.third
115    );
116
117    // Nested structures
118    let nested = Pair {
119        first: Pair::new(1, 2),
120        second: Triple::new(3, 4, 5),
121    };
122    println!(
123        "Nested: pair=({}, {}), triple=({}, {}, {})",
124        nested.first.first,
125        nested.first.second,
126        nested.second.first,
127        nested.second.second,
128        nested.second.third
129    );
130}