Skip to main content

usage/
usage.rs

1use hypen_parser::{parse_component, print_parse_errors, Argument, Value};
2
3fn main() {
4    println!("=== Hypen Parser Usage Examples ===\n");
5
6    // Example 1: Simple component
7    example_1();
8
9    println!("\n{}\n", "=".repeat(70));
10
11    // Example 2: Extracting arguments
12    example_2();
13
14    println!("\n{}\n", "=".repeat(70));
15
16    // Example 3: Working with applicators
17    example_3();
18
19    println!("\n{}\n", "=".repeat(70));
20
21    // Example 4: Navigating component tree
22    example_4();
23
24    println!("\n{}\n", "=".repeat(70));
25
26    // Example 5: Error handling
27    example_5();
28}
29
30fn example_1() {
31    println!("Example 1: Parsing a simple component\n");
32
33    let input = r#"Text("Hello, World!")"#;
34    println!("Input: {}\n", input);
35
36    match parse_component(input) {
37        Ok(component) => {
38            println!("✓ Component name: {}", component.name);
39            println!("✓ Arguments count: {}", component.arguments.arguments.len());
40
41            if let Some(Argument::Positioned { value, .. }) =
42                component.arguments.arguments.first()
43            {
44                println!("✓ First argument: {:?}", value);
45            }
46        }
47        Err(errors) => {
48            print_parse_errors("input.hypen", input, &errors);
49        }
50    }
51}
52
53fn example_2() {
54    println!("Example 2: Extracting named and positional arguments\n");
55
56    let input = r#"Button(text: "Click Me", enabled: true, color: blue)"#;
57    println!("Input: {}\n", input);
58
59    match parse_component(input) {
60        Ok(component) => {
61            println!("✓ Component: {}", component.name);
62
63            // Get named argument
64            if let Some(text) = component.arguments.get_named("text") {
65                println!("✓ Text argument: {:?}", text);
66            }
67
68            // Get boolean argument
69            if let Some(Value::Boolean(enabled)) = component.arguments.get_named("enabled") {
70                println!("✓ Enabled: {}", enabled);
71            }
72
73            // Iterate all arguments
74            println!("\nAll arguments:");
75            for (i, arg) in component.arguments.arguments.iter().enumerate() {
76                match arg {
77                    Argument::Named { key, value } => {
78                        println!("  [{}] {} = {:?}", i, key, value);
79                    }
80                    Argument::Positioned { position, value } => {
81                        println!("  [{}] pos {} = {:?}", i, position, value);
82                    }
83                }
84            }
85        }
86        Err(errors) => {
87            print_parse_errors("input.hypen", input, &errors);
88        }
89    }
90}
91
92fn example_3() {
93    println!("Example 3: Working with applicators (styling)\n");
94
95    let input = r#"
96        Text("Styled Text")
97            .fontSize(18)
98            .color(blue)
99            .padding(16)
100    "#;
101    println!("Input: {}\n", input);
102
103    match parse_component(input) {
104        Ok(component) => {
105            println!("✓ Component: {}", component.name);
106            println!("✓ Applicators count: {}", component.applicators.len());
107
108            println!("\nApplicators:");
109            for applicator in &component.applicators {
110                print!("  .{}(", applicator.name);
111                for (i, arg) in applicator.arguments.arguments.iter().enumerate() {
112                    if i > 0 {
113                        print!(", ");
114                    }
115                    match arg {
116                        Argument::Named { key, value } => print!("{}: {:?}", key, value),
117                        Argument::Positioned { value, .. } => print!("{:?}", value),
118                    }
119                }
120                println!(")");
121            }
122        }
123        Err(errors) => {
124            print_parse_errors("input.hypen", input, &errors);
125        }
126    }
127}
128
129fn example_4() {
130    println!("Example 4: Navigating the component tree\n");
131
132    let input = r#"
133        Column {
134            Text("Header")
135                .fontSize(24)
136
137            Row {
138                Button("Left")
139                Button("Right")
140            }
141
142            Text("Footer")
143        }
144    "#;
145    println!("Input: {}\n", input);
146
147    match parse_component(input) {
148        Ok(component) => {
149            println!("✓ Root component: {}", component.name);
150            println!("✓ Children count: {}", component.children.len());
151
152            // Walk the tree
153            println!("\nComponent tree:");
154            print_tree(&component, 0);
155
156            // Flatten to list
157            let all_components = component.flatten();
158            println!("\nFlattened tree ({} total components):", all_components.len());
159            for comp in &all_components {
160                println!(
161                    "  - {} (applicators: {})",
162                    comp.name,
163                    comp.applicators.len()
164                );
165            }
166        }
167        Err(errors) => {
168            print_parse_errors("input.hypen", input, &errors);
169        }
170    }
171}
172
173fn print_tree(component: &hypen_parser::ComponentSpecification, depth: usize) {
174    let indent = "  ".repeat(depth);
175    print!("{}{}", indent, component.name);
176
177    if !component.applicators.is_empty() {
178        print!(" [");
179        for (i, app) in component.applicators.iter().enumerate() {
180            if i > 0 {
181                print!(", ");
182            }
183            print!(".{}", app.name);
184        }
185        print!("]");
186    }
187    println!();
188
189    for child in &component.children {
190        print_tree(child, depth + 1);
191    }
192}
193
194fn example_5() {
195    println!("Example 5: Proper error handling\n");
196
197    let inputs = [r#"Text("Valid input")"#,
198        r#"Text("Missing closing paren"#,
199        r#"Column { Text("Unclosed block"#];
200
201    for (i, input) in inputs.iter().enumerate() {
202        println!("Input {}: {}\n", i + 1, input);
203
204        match parse_component(input) {
205            Ok(component) => {
206                println!("✓ Success: parsed component '{}'", component.name);
207            }
208            Err(errors) => {
209                println!("✗ Parse error:");
210                print_parse_errors(&format!("input-{}.hypen", i + 1), input, &errors);
211            }
212        }
213
214        if i < inputs.len() - 1 {
215            println!("\n{}\n", "-".repeat(60));
216        }
217    }
218}