reratui-macro 0.2.0

Procedural macros for component definition, RSX syntax, and hook validation in Reratui
Documentation
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
use syn::{
    Result,
    parse::{Parse, ParseStream},
};

// Import submodules for better organization
mod ast;
mod parsers;
mod token_analyzer;
mod validators;

// Re-export public types
pub use ast::*;
pub use parsers::*;
pub use validators::*;

/// Main RSX Parser - orchestrates the parsing process
/// Following SOLID principles with dependency injection and clean interfaces
pub struct RsxMainParser {
    node_parser: NodeParser,
    validator: CompositeValidator,
}

impl RsxMainParser {
    pub fn new() -> Self {
        Self {
            node_parser: ParserFactory::create_node_parser(),
            validator: ValidatorFactory::create_validator(),
        }
    }

    /// Create a parser with React-like validation (PascalCase elements, camelCase props)
    pub fn new_react_like() -> Self {
        Self {
            node_parser: ParserFactory::create_node_parser(),
            validator: ValidatorFactory::create_validator(),
        }
    }

    pub fn parse_with_validation(&self, input: ParseStream) -> Result<Node> {
        let node = match self.node_parser.parse(input) {
            Ok(node) => node,
            Err(err) => {
                // Convert to a more user-friendly error if possible
                let error_msg = err.to_string();
                if error_msg.contains("Expected attribute") {
                    return Err(crate::rsx::error::RsxError::SyntaxError {
                        message: "Invalid attribute syntax".to_string(),
                        span: input.span(),
                        suggestion: Some(
                            "Attributes should be in the format: name=\"value\" or name={expr}"
                                .to_string(),
                        ),
                    }
                    .to_syn_error());
                }
                return Err(err);
            }
        };

        // Always validate the parsed node
        if let Err(err) = self.validator.validate_all(&node) {
            // Try to provide more context for validation errors
            let error_msg = err.to_string();
            if error_msg.contains("naming convention") {
                return Err(crate::rsx::error::RsxError::ValidationError {
                    message: "Component naming convention violation".to_string(),
                    span: node.span(),
                    details: Some(
                        "React-like components should use PascalCase names (e.g., MyComponent)"
                            .to_string(),
                    ),
                }
                .to_syn_error());
            }
            return Err(err);
        }

        Ok(node)
    }
}

impl Default for RsxMainParser {
    fn default() -> Self {
        Self::new()
    }
}

impl Parse for Node {
    fn parse(input: ParseStream) -> Result<Self> {
        // Use permissive validation by default for backward compatibility
        let parser = RsxMainParser::new();
        parser.parse_with_validation(input)
    }
}

impl Parse for Element {
    fn parse(input: ParseStream) -> Result<Self> {
        let parser = ParserFactory::create_element_parser();
        parser.parse(input)
    }
}

impl Parse for ConditionalNode {
    fn parse(input: ParseStream) -> Result<Self> {
        let parser = ParserFactory::create_conditional_parser();
        parser.parse(input)
    }
}

impl Parse for ForLoopNode {
    fn parse(input: ParseStream) -> Result<Self> {
        let parser = ForLoopParser::new();
        parser.parse(input)
    }
}

impl Parse for FragmentNode {
    fn parse(input: ParseStream) -> Result<Self> {
        let parser = FragmentParser::new();
        parser.parse(input)
    }
}

/// Utility functions for parsing with different validation modes
impl RsxMainParser {
    /// Parse RSX with React-like validation (PascalCase components, camelCase props)
    ///
    /// # Example
    /// ```ignore
    /// // This would pass validation
    /// let valid_rsx = quote! { <MyComponent className="test" /> };
    ///
    /// // This would fail validation (lowercase component name)
    /// let invalid_rsx = quote! { <myComponent className="test" /> };
    /// ```
    pub fn parse_react_like_tokens(tokens: proc_macro2::TokenStream) -> Result<Node> {
        let parser = Self::new_react_like();
        let parsed: Node = syn::parse2(tokens)?;
        parser.validator.validate_all(&parsed)?;
        Ok(parsed)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use quote::quote;
    use syn::parse2;

    #[test]
    fn test_react_like_validation_failure() {
        // Test that React-like validation fails for improper naming
        let rsx = quote! { <myComponent className="test" /> };
        let result = RsxMainParser::parse_react_like_tokens(rsx);
        assert!(
            result.is_err(),
            "React-like validation should fail for camelCase component"
        );
    }

    #[test]
    fn test_validation_visitor() {
        // Test the validation visitor pattern
        let rsx = quote! { <MyComponent class_name="test"><ChildComponent /></MyComponent> };
        let node: Node = parse2(rsx).expect("Should parse successfully");

        let mut visitor = ValidationVisitor::new()
            .with_element_validator(
                ElementValidator::new().with_naming_convention(NamingConvention::PascalCase),
            )
            .with_property_validator(
                PropertyValidator::new().with_naming_convention(NamingConvention::SnakeCase),
            );

        let result = node.accept(&mut visitor);
        assert!(
            result.is_ok(),
            "Validation visitor should pass for well-formed RSX"
        );
        assert!(!visitor.has_errors(), "Visitor should have no errors");
    }

    #[test]
    fn test_element_factory_validation() {
        // Test that element factory validates during creation
        use syn::{Path, parse_quote};

        let name: Path = parse_quote!(MyComponent);
        let attributes = vec![];
        let children = vec![];
        let span = proc_macro2::Span::call_site();

        let result = ElementFactory::create(name, attributes, children, span);
        assert!(
            result.is_ok(),
            "Element factory should create valid elements"
        );
    }

    #[test]
    fn test_prop_factory_validation() {
        // Test that prop factory validates during creation
        use syn::{Expr, Ident, parse_quote};

        let key: Ident = parse_quote!(className);
        let value: Expr = parse_quote!("test");
        let span = proc_macro2::Span::call_site();

        let result = PropFactory::create(key, value, span);
        assert!(result.is_ok(), "Prop factory should create valid props");
    }

    #[test]
    fn test_conditional_factory_validation() {
        // Test that conditional factory validates during creation
        use syn::{Expr, parse_quote};

        let condition: Expr = parse_quote!(true);
        let then_branch = Box::new(Node::Expression(parse_quote!(42)));
        let span = proc_macro2::Span::call_site();

        let result = ConditionalFactory::create_logical_and(condition, then_branch, span);
        assert!(
            result.is_ok(),
            "Conditional factory should create valid conditionals"
        );
    }

    #[test]
    fn test_string_literal_as_direct_child() {
        // Test that string literals can be used as direct children
        let rsx = quote! { <Span cyan bold>"Navigation:"</Span> };
        let result = parse2::<Node>(rsx);
        assert!(
            result.is_ok(),
            "String literals should be parseable as direct children"
        );

        if let Ok(Node::Element(element)) = result {
            assert_eq!(element.children.len(), 1, "Should have one child");
            if let Node::Expression(expr) = &element.children[0] {
                if let syn::Expr::Lit(syn::ExprLit {
                    lit: syn::Lit::Str(lit_str),
                    ..
                }) = expr
                {
                    assert_eq!(
                        lit_str.value(),
                        "Navigation:",
                        "String content should match"
                    );
                } else {
                    panic!("Child should be a string literal expression");
                }
            } else {
                panic!("Child should be an expression node");
            }
        }
    }

    #[test]
    fn test_mixed_children_with_string_literals() {
        // Test that components can have both string literals and other children
        let rsx = quote! {
            <Line>
                <Span cyan>"Label: "</Span>
                <Span white bold>"Value"</Span>
            </Line>
        };
        let result = parse2::<Node>(rsx);
        assert!(
            result.is_ok(),
            "Mixed children with string literals should parse successfully"
        );
    }

    #[test]
    fn test_attributes_with_string_literal_children() {
        // Test that attributes work correctly when string literal children are present
        let rsx =
            quote! { <Span style={Style::default().fg(Color::Red)} bold>"Error message"</Span> };
        let result = parse2::<Node>(rsx);
        assert!(
            result.is_ok(),
            "Attributes should work with string literal children"
        );

        if let Ok(Node::Element(element)) = result {
            // Should have both style and bold attributes
            assert!(
                element.attributes.len() >= 2,
                "Should have style and bold attributes"
            );
            // Should have one string literal child
            assert_eq!(element.children.len(), 1, "Should have one child");
        }
    }

    #[test]
    fn test_empty_fragment() {
        // Test that empty fragments parse correctly
        let rsx = quote! { <></> };
        let result = parse2::<Node>(rsx);
        assert!(result.is_ok(), "Empty fragments should parse successfully");

        if let Ok(Node::Fragment(fragment)) = result {
            assert_eq!(
                fragment.children.len(),
                0,
                "Empty fragment should have no children"
            );
        } else {
            panic!("Should parse as a fragment node");
        }
    }

    #[test]
    fn test_fragment_with_children() {
        // Test that fragments with children parse correctly
        let rsx = quote! {
            <>
                <Span cyan>"Label: "</Span>
                <Span white bold>"Value"</Span>
            </>
        };
        let result = parse2::<Node>(rsx);
        assert!(
            result.is_ok(),
            "Fragments with children should parse successfully"
        );

        if let Ok(Node::Fragment(fragment)) = result {
            assert_eq!(
                fragment.children.len(),
                2,
                "Fragment should have two children"
            );
            // Both children should be elements
            assert!(matches!(fragment.children[0], Node::Element(_)));
            assert!(matches!(fragment.children[1], Node::Element(_)));
        } else {
            panic!("Should parse as a fragment node");
        }
    }

    #[test]
    fn test_fragment_with_string_literals() {
        // Test that fragments can contain string literals directly
        let rsx = quote! {
            <>
                "First text"
                "Second text"
            </>
        };
        let result = parse2::<Node>(rsx);
        assert!(
            result.is_ok(),
            "Fragments with string literals should parse successfully"
        );

        if let Ok(Node::Fragment(fragment)) = result {
            assert_eq!(
                fragment.children.len(),
                2,
                "Fragment should have two children"
            );
            // Both children should be expressions (string literals)
            assert!(matches!(fragment.children[0], Node::Expression(_)));
            assert!(matches!(fragment.children[1], Node::Expression(_)));
        }
    }

    #[test]
    fn test_nested_fragments() {
        // Test that fragments can be nested
        let rsx = quote! {
            <>
                <Span>"Outer"</Span>
                <>
                    <Span>"Inner 1"</Span>
                    <Span>"Inner 2"</Span>
                </>
            </>
        };
        let result = parse2::<Node>(rsx);
        assert!(result.is_ok(), "Nested fragments should parse successfully");

        if let Ok(Node::Fragment(fragment)) = result {
            assert_eq!(
                fragment.children.len(),
                2,
                "Outer fragment should have two children"
            );
            assert!(matches!(fragment.children[0], Node::Element(_)));
            assert!(matches!(fragment.children[1], Node::Fragment(_)));

            // Check inner fragment
            if let Node::Fragment(inner_fragment) = &fragment.children[1] {
                assert_eq!(
                    inner_fragment.children.len(),
                    2,
                    "Inner fragment should have two children"
                );
            }
        }
    }

    #[test]
    fn test_fragment_with_conditional() {
        // Test that fragments work with conditional rendering
        let rsx = quote! {
            <>
                {if true { <Span>"Shown"</Span> } else { <Span>"Hidden"</Span> }}
                <Span>"Always shown"</Span>
            </>
        };
        let result = parse2::<Node>(rsx);
        assert!(
            result.is_ok(),
            "Fragments with conditionals should parse successfully"
        );

        if let Ok(Node::Fragment(fragment)) = result {
            assert_eq!(
                fragment.children.len(),
                2,
                "Fragment should have two children"
            );
            assert!(matches!(fragment.children[0], Node::Conditional(_)));
            assert!(matches!(fragment.children[1], Node::Element(_)));
        }
    }

    #[test]
    fn test_for_loop_with_preparation_statements() {
        // Test that for-loops can handle preparation statements before JSX elements
        let rsx = quote! {
            {for (index, file) in props.files.iter().enumerate() {
                let is_selected = index == props.selected_index;
                let status_icon = match file.status {
                    FileStatus::Modified => "📝",
                    FileStatus::Added => "",
                    _ => "",
                };

                <Line>
                    <Span>{status_icon}</Span>
                    <Span>{file.path.clone()}</Span>
                </Line>
            }}
        };
        let result = parse2::<Node>(rsx);
        assert!(
            result.is_ok(),
            "For-loops with preparation statements should parse successfully"
        );

        if let Ok(Node::ForLoop(for_loop)) = result {
            assert_eq!(
                for_loop.preparation_stmts.len(),
                2,
                "Should have two preparation statements"
            );
            assert!(
                matches!(for_loop.body.as_ref(), Node::Element(_)),
                "Body should be an element"
            );
        }
    }

    #[test]
    fn test_for_loop_simple_syntax() {
        // Test simple for-loop without preparation statements
        let rsx = quote! {
            {for item in items {
                <Span>{item}</Span>
            }}
        };
        let result = parse2::<Node>(rsx);
        assert!(result.is_ok(), "Simple for-loops should parse successfully");

        if let Ok(Node::ForLoop(for_loop)) = result {
            assert_eq!(
                for_loop.preparation_stmts.len(),
                0,
                "Should have no preparation statements"
            );
            assert!(
                matches!(for_loop.body.as_ref(), Node::Element(_)),
                "Body should be an element"
            );
        }
    }
}