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