homeboy 0.70.0

CLI for multi-component deployment and development workflow automation
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
//! Grammar-driven item parsing — extract top-level items with full boundaries.
//!
//! This module builds on the grammar engine (`utils/grammar.rs`) to produce
//! `GrammarItem`s — complete items with start/end lines and source text.
//! It replaces the extension-side `parse_items` command for languages that
//! have a grammar.toml.
//!
//! # Architecture
//!
//! ```text
//! utils/grammar.rs       (patterns, symbols, walk_lines)
//!//! utils/grammar_items.rs (this file: item boundaries, source extraction)
//!//! core/refactor/         (decompose, move — consume GrammarItems)
//! ```

use serde::{Deserialize, Serialize};

use super::grammar::{self, Grammar, Symbol};

// ============================================================================
// Types
// ============================================================================

/// A parsed top-level item with full boundaries and source text.
///
/// This is the core equivalent of `extension::ParsedItem`, produced entirely
/// from grammar patterns without calling extension scripts.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GrammarItem {
    /// Name of the item (function, struct, etc.).
    pub name: String,
    /// What kind of item: function, struct, enum, trait, impl, const, static, type_alias.
    pub kind: String,
    /// Start line (1-indexed, includes doc comments and attributes).
    pub start_line: usize,
    /// End line (1-indexed, inclusive).
    pub end_line: usize,
    /// The extracted source code (including doc comments and attributes).
    pub source: String,
    /// Visibility: "pub", "pub(crate)", "pub(super)", or "" for private.
    #[serde(default)]
    pub visibility: String,
}

// ============================================================================
// Core parse_items
// ============================================================================

/// Parse all top-level items from source content using a grammar.
///
/// This is the core replacement for the extension `parse_items` command.
/// It uses grammar patterns to find declarations, then resolves item
/// boundaries using grammar-aware brace matching that correctly handles
/// strings, comments, and language-specific constructs.
///
/// Items inside `#[cfg(test)] mod tests { ... }` blocks are excluded.
pub fn parse_items(content: &str, grammar: &Grammar) -> Vec<GrammarItem> {
    let lines: Vec<&str> = content.lines().collect();
    if lines.is_empty() {
        return Vec::new();
    }

    // Find the test module range to exclude
    let test_range = find_test_module_range(&lines, grammar);

    // Extract all symbols using the grammar engine
    let symbols = grammar::extract(content, grammar);

    // Map grammar concepts to item kinds
    let item_symbols: Vec<(&Symbol, &str)> = symbols
        .iter()
        .filter_map(|s| {
            let kind = match s.concept.as_str() {
                "function" | "free_function" => "function",
                "struct" => {
                    // The struct pattern matches struct/enum/trait — use the "kind" capture
                    s.get("kind").unwrap_or("struct")
                }
                "impl_block" => "impl",
                "type_alias" => "type_alias",
                "const_static" => s.get("kind").unwrap_or("const"),
                _ => return None,
            };
            Some((s, kind))
        })
        .collect();

    let mut items = Vec::new();

    for (symbol, kind) in &item_symbols {
        let decl_line_idx = symbol.line - 1; // 0-indexed

        // Skip if inside test module
        if let Some((test_start, test_end)) = test_range {
            if decl_line_idx >= test_start && decl_line_idx <= test_end {
                continue;
            }
        }

        // Only process top-level items (depth 0)
        if symbol.depth != 0 {
            continue;
        }

        // Find the start including doc comments and attributes
        let prefix_start = find_prefix_start(&lines, decl_line_idx);

        // Skip if prefix extends into test module
        if let Some((test_start, test_end)) = test_range {
            if prefix_start >= test_start && prefix_start <= test_end {
                continue;
            }
        }

        // Find the end of the item
        let end_line_idx = find_item_end(&lines, decl_line_idx, kind, grammar);

        // Extract the name
        let name = if *kind == "impl" {
            // For impl blocks, try type_name first
            symbol
                .get("type_name")
                .or_else(|| symbol.name())
                .unwrap_or("")
        } else {
            symbol.name().unwrap_or("")
        };

        if name.is_empty() {
            continue;
        }

        // Build the impl name with trait if present
        let full_name = if *kind == "impl" {
            if let Some(trait_name) = symbol.get("trait_name") {
                if !trait_name.is_empty() {
                    format!("{} for {}", trait_name, name)
                } else {
                    name.to_string()
                }
            } else {
                name.to_string()
            }
        } else {
            name.to_string()
        };

        // Extract visibility
        let visibility = symbol
            .visibility()
            .map(|v| v.trim().to_string())
            .unwrap_or_default();

        // Extract source text
        let source = lines[prefix_start..=end_line_idx].join("\n");

        items.push(GrammarItem {
            name: full_name,
            kind: kind.to_string(),
            start_line: prefix_start + 1, // 1-indexed
            end_line: end_line_idx + 1,   // 1-indexed
            source,
            visibility,
        });
    }

    // Sort by start line and deduplicate overlapping items
    items.sort_by_key(|item| item.start_line);
    dedupe_overlapping_items(items)
}

// ============================================================================
// Boundary detection
// ============================================================================

/// Find the start of doc comments and attributes above a declaration.
fn find_prefix_start(lines: &[&str], decl_line: usize) -> usize {
    let mut start = decl_line;

    while start > 0 {
        let prev = lines[start - 1].trim();
        if prev.starts_with("///")
            || prev.starts_with("//!")
            || prev.starts_with("#[")
            || prev.is_empty()
        {
            // Check if empty line is between doc comments (not a gap)
            if prev.is_empty() {
                // Look further back — if there's a doc comment above the blank,
                // include the blank. Otherwise stop.
                if start >= 2 {
                    let above = lines[start - 2].trim();
                    if above.starts_with("///") || above.starts_with("#[") {
                        start -= 1;
                        continue;
                    }
                }
                break;
            }
            start -= 1;
        } else {
            break;
        }
    }

    start
}

/// Find the end line of an item using grammar-aware brace matching.
fn find_item_end(lines: &[&str], decl_line: usize, kind: &str, grammar: &Grammar) -> usize {
    // For const, static, type_alias — scan for semicolon
    if kind == "const" || kind == "static" || kind == "type_alias" {
        for i in decl_line..lines.len() {
            if lines[i].contains(';') {
                return i;
            }
        }
        return decl_line;
    }

    // For struct/enum/trait — check if it's a unit/tuple struct (semicolon before any brace)
    if kind == "struct" || kind == "enum" || kind == "trait" {
        // Scan forward from the declaration line: if we hit `;` before `{`, it's braceless
        for i in decl_line..lines.len() {
            let line = lines[i];
            for ch in line.chars() {
                if ch == '{' {
                    // Has braces — fall through to brace matching below
                    break;
                }
                if ch == ';' {
                    return i;
                }
            }
            if line.contains('{') {
                break;
            }
        }
    }

    // For everything else — find matching brace using grammar-aware scanning
    find_matching_brace(lines, decl_line, grammar)
}

// ============================================================================
// Grammar-aware brace matching
// ============================================================================

/// Grammar-aware brace matching that handles strings, comments, raw strings,
/// and character/lifetime literals correctly.
///
/// This is the core replacement for the extension `find_matching_brace`.
pub(crate) fn find_matching_brace(lines: &[&str], start_line: usize, grammar: &Grammar) -> usize {
    let open = grammar.blocks.open.chars().next().unwrap_or('{');
    let close = grammar.blocks.close.chars().next().unwrap_or('}');
    let escape_char = grammar.strings.escape.chars().next().unwrap_or('\\');
    let quote_chars: Vec<char> = grammar
        .strings
        .quotes
        .iter()
        .filter_map(|q| q.chars().next())
        .collect();

    let mut depth: i32 = 0;
    let mut found_open = false;
    let mut in_block_comment = false;

    for i in start_line..lines.len() {
        let line = lines[i];
        let chars: Vec<char> = line.chars().collect();
        let mut j = 0;

        while j < chars.len() {
            // Inside block comment
            if in_block_comment {
                if j + 1 < chars.len() && chars[j] == '*' && chars[j + 1] == '/' {
                    in_block_comment = false;
                    j += 2;
                } else {
                    j += 1;
                }
                continue;
            }

            // Block comment start
            if j + 1 < chars.len() && chars[j] == '/' && chars[j + 1] == '*' {
                in_block_comment = true;
                j += 2;
                continue;
            }

            // Line comment
            if j + 1 < chars.len() && chars[j] == '/' && chars[j + 1] == '/' {
                break;
            }

            // Raw string literal (r#"..."#, r##"..."##, etc.)
            if chars[j] == 'r' && j + 1 < chars.len() {
                let mut hashes = 0;
                let mut k = j + 1;
                while k < chars.len() && chars[k] == '#' {
                    hashes += 1;
                    k += 1;
                }
                if k < chars.len() && chars[k] == '"' && hashes > 0 {
                    // Found r#"... — skip until matching "###
                    k += 1; // skip opening quote
                    let closing: String = std::iter::once('"')
                        .chain(std::iter::repeat('#').take(hashes))
                        .collect();
                    let closing_chars: Vec<char> = closing.chars().collect();
                    'raw_scan: while k < chars.len() {
                        if k + closing_chars.len() <= chars.len() {
                            let slice: String = chars[k..k + closing_chars.len()].iter().collect();
                            if slice == closing {
                                k += closing_chars.len();
                                break 'raw_scan;
                            }
                        }
                        k += 1;
                    }
                    // If we didn't find closing on this line, skip rest of line
                    if k >= chars.len() {
                        let closing_str: String = closing.chars().collect();
                        let mut found_close = false;
                        for next_i in (i + 1)..lines.len() {
                            if lines[next_i].contains(&closing_str) {
                                found_close = true;
                                break;
                            }
                            if next_i == lines.len() - 1 {
                                return lines.len() - 1;
                            }
                        }
                        if found_close {
                            // Raw string closed on a later line — skip rest of this line
                            break;
                        }
                    }
                    j = k;
                    continue;
                }
            }

            // Regular string literal
            if quote_chars.contains(&chars[j]) {
                let quote = chars[j];
                // In Rust, single quote is for char literals and lifetimes
                if quote == '\'' {
                    if j + 1 < chars.len()
                        && (chars[j + 1].is_alphanumeric() || chars[j + 1] == '_')
                    {
                        j += 1;
                        continue;
                    }
                }
                j += 1;
                while j < chars.len() {
                    if chars[j] == escape_char {
                        j += 2;
                    } else if chars[j] == quote {
                        j += 1;
                        break;
                    } else {
                        j += 1;
                    }
                }
                continue;
            }

            if chars[j] == open {
                depth += 1;
                found_open = true;
            } else if chars[j] == close {
                depth -= 1;
                if found_open && depth == 0 {
                    return i;
                }
            }

            j += 1;
        }
    }

    lines.len() - 1
}

// ============================================================================
// Test module detection
// ============================================================================

/// Find the range of the `#[cfg(test)] mod tests { ... }` block.
/// Returns (start_idx, end_idx) as 0-indexed line numbers, or None.
fn find_test_module_range(lines: &[&str], grammar: &Grammar) -> Option<(usize, usize)> {
    for i in 0..lines.len() {
        if lines[i].contains("#[cfg(test)]") {
            // Look ahead for `mod tests` or `mod test`
            for j in (i + 1)..std::cmp::min(i + 3, lines.len()) {
                let trimmed = lines[j].trim();
                if trimmed.starts_with("mod tests")
                    || trimmed.starts_with("mod test ")
                    || trimmed.starts_with("mod test{")
                {
                    let end = find_matching_brace(lines, j, grammar);
                    return Some((i, end));
                }
            }
        }
    }

    // Also check for `mod tests {` without #[cfg(test)]
    for i in 0..lines.len() {
        let trimmed = lines[i].trim();
        if trimmed.starts_with("mod tests {") || trimmed.starts_with("mod tests{") {
            let end = find_matching_brace(lines, i, grammar);
            return Some((i, end));
        }
    }

    None
}

// ============================================================================
// Helpers
// ============================================================================

/// Remove overlapping items (keep the one that started first / is larger).
fn dedupe_overlapping_items(items: Vec<GrammarItem>) -> Vec<GrammarItem> {
    let mut result: Vec<GrammarItem> = Vec::new();

    for item in items {
        if let Some(last) = result.last() {
            if item.start_line >= last.start_line && item.start_line <= last.end_line {
                if (item.end_line - item.start_line) > (last.end_line - last.start_line) {
                    result.pop();
                    result.push(item);
                }
                continue;
            }
        }
        result.push(item);
    }

    result
}

/// Validate that extracted source has balanced braces.
///
/// Returns true if all braces are balanced. Use this as a pre-write
/// safety check before applying decompose/move operations.
pub fn validate_brace_balance(source: &str, grammar: &Grammar) -> bool {
    let lines: Vec<&str> = source.lines().collect();
    let open = grammar.blocks.open.chars().next().unwrap_or('{');
    let close = grammar.blocks.close.chars().next().unwrap_or('}');
    let escape_char = grammar.strings.escape.chars().next().unwrap_or('\\');
    let mut depth: i32 = 0;
    let mut in_block_comment = false;

    for line in &lines {
        let chars: Vec<char> = line.chars().collect();
        let mut j = 0;
        while j < chars.len() {
            if in_block_comment {
                if j + 1 < chars.len() && chars[j] == '*' && chars[j + 1] == '/' {
                    in_block_comment = false;
                    j += 2;
                } else {
                    j += 1;
                }
                continue;
            }
            if j + 1 < chars.len() && chars[j] == '/' && chars[j + 1] == '*' {
                in_block_comment = true;
                j += 2;
                continue;
            }
            if j + 1 < chars.len() && chars[j] == '/' && chars[j + 1] == '/' {
                break;
            }
            if chars[j] == '"' {
                j += 1;
                while j < chars.len() {
                    if chars[j] == escape_char {
                        j += 2;
                    } else if chars[j] == '"' {
                        j += 1;
                        break;
                    } else {
                        j += 1;
                    }
                }
                continue;
            }
            if chars[j] == open {
                depth += 1;
            } else if chars[j] == close {
                depth -= 1;
            }
            j += 1;
        }
    }

    depth == 0
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    use crate::utils::grammar::{
        BlockSyntax, CommentSyntax, ConceptPattern, Grammar, LanguageMeta, StringSyntax,
    };

    /// Build a full Rust grammar with all item-relevant patterns.
    fn full_rust_grammar() -> Grammar {
        Grammar {
            language: LanguageMeta {
                id: "rust".to_string(),
                extensions: vec!["rs".to_string()],
            },
            comments: CommentSyntax {
                line: vec!["//".to_string()],
                block: vec![("/*".to_string(), "*/".to_string())],
                doc: vec!["///".to_string(), "//!".to_string()],
            },
            strings: StringSyntax {
                quotes: vec!["\"".to_string()],
                escape: "\\".to_string(),
                multiline: vec![],
            },
            blocks: BlockSyntax::default(),
            patterns: {
                let mut p = HashMap::new();
                p.insert(
                    "function".to_string(),
                    ConceptPattern {
                        regex: r"^\s*(pub(?:\(crate\))?\s+)?(?:async\s+)?(?:unsafe\s+)?(?:const\s+)?fn\s+(\w+)\s*\(([^)]*)\)"
                            .to_string(),
                        captures: {
                            let mut c = HashMap::new();
                            c.insert("visibility".to_string(), 1);
                            c.insert("name".to_string(), 2);
                            c.insert("params".to_string(), 3);
                            c
                        },
                        context: "any".to_string(),
                        skip_comments: true,
                        skip_strings: true,
                        require_capture: None,
                    },
                );
                p.insert(
                    "struct".to_string(),
                    ConceptPattern {
                        regex: r"^\s*(pub(?:\(crate\))?\s+)?(struct|enum|trait)\s+(\w+)"
                            .to_string(),
                        captures: {
                            let mut c = HashMap::new();
                            c.insert("visibility".to_string(), 1);
                            c.insert("kind".to_string(), 2);
                            c.insert("name".to_string(), 3);
                            c
                        },
                        context: "top_level".to_string(),
                        skip_comments: true,
                        skip_strings: true,
                        require_capture: None,
                    },
                );
                p.insert(
                    "impl_block".to_string(),
                    ConceptPattern {
                        regex: r"^\s*impl(?:<[^>]*>)?\s+(?:(\w+)\s+for\s+)?(\w+)".to_string(),
                        captures: {
                            let mut c = HashMap::new();
                            c.insert("trait_name".to_string(), 1);
                            c.insert("type_name".to_string(), 2);
                            c
                        },
                        context: "any".to_string(),
                        skip_comments: true,
                        skip_strings: true,
                        require_capture: None,
                    },
                );
                p.insert(
                    "const_static".to_string(),
                    ConceptPattern {
                        regex: r"^\s*(pub(?:\(crate\))?\s+)?(const|static)\s+(\w+)\s*:".to_string(),
                        captures: {
                            let mut c = HashMap::new();
                            c.insert("visibility".to_string(), 1);
                            c.insert("kind".to_string(), 2);
                            c.insert("name".to_string(), 3);
                            c
                        },
                        context: "any".to_string(),
                        skip_comments: true,
                        skip_strings: true,
                        require_capture: None,
                    },
                );
                p.insert(
                    "type_alias".to_string(),
                    ConceptPattern {
                        regex: r"^\s*(pub(?:\(crate\))?\s+)?type\s+(\w+)".to_string(),
                        captures: {
                            let mut c = HashMap::new();
                            c.insert("visibility".to_string(), 1);
                            c.insert("name".to_string(), 2);
                            c
                        },
                        context: "top_level".to_string(),
                        skip_comments: true,
                        skip_strings: true,
                        require_capture: None,
                    },
                );
                p
            },
        }
    }

    #[test]
    fn parse_items_basic() {
        let content = "\
pub fn hello() {
    println!(\"hi\");
}

struct Foo {
    x: i32,
}";
        let grammar = full_rust_grammar();
        let items = parse_items(content, &grammar);

        assert_eq!(items.len(), 2);
        assert_eq!(items[0].name, "hello");
        assert_eq!(items[0].kind, "function");
        assert_eq!(items[0].start_line, 1);
        assert_eq!(items[0].end_line, 3);

        assert_eq!(items[1].name, "Foo");
        assert_eq!(items[1].kind, "struct");
        assert_eq!(items[1].start_line, 5);
        assert_eq!(items[1].end_line, 7);
    }

    #[test]
    fn parse_items_with_doc_comments() {
        let content = "\
/// This function does stuff.
/// It's important.
pub fn documented() {
    todo!()
}";
        let grammar = full_rust_grammar();
        let items = parse_items(content, &grammar);

        assert_eq!(items.len(), 1);
        assert_eq!(items[0].name, "documented");
        assert_eq!(items[0].start_line, 1); // includes doc comments
        assert_eq!(items[0].end_line, 5);
        assert!(items[0].source.starts_with("/// This function"));
    }

    #[test]
    fn parse_items_with_attributes() {
        let content = "\
#[derive(Debug, Clone)]
#[serde(rename_all = \"camelCase\")]
pub struct Config {
    pub name: String,
}";
        let grammar = full_rust_grammar();
        let items = parse_items(content, &grammar);

        assert_eq!(items.len(), 1);
        assert_eq!(items[0].name, "Config");
        assert_eq!(items[0].start_line, 1); // includes attributes
        assert_eq!(items[0].end_line, 5);
    }

    #[test]
    fn parse_items_skips_test_module() {
        let content = "\
pub fn real_fn() {}

#[cfg(test)]
mod tests {
    #[test]
    fn test_something() {
        assert!(true);
    }
}";
        let grammar = full_rust_grammar();
        let items = parse_items(content, &grammar);

        assert_eq!(items.len(), 1);
        assert_eq!(items[0].name, "real_fn");
    }

    #[test]
    fn parse_items_impl_block() {
        let content = "\
pub struct Foo {}

impl Foo {
    pub fn new() -> Self {
        Foo {}
    }
}";
        let grammar = full_rust_grammar();
        let items = parse_items(content, &grammar);

        assert_eq!(items.len(), 2);
        assert_eq!(items[0].name, "Foo");
        assert_eq!(items[0].kind, "struct");
        assert_eq!(items[1].name, "Foo");
        assert_eq!(items[1].kind, "impl");
        assert_eq!(items[1].start_line, 3);
        assert_eq!(items[1].end_line, 7);
    }

    #[test]
    fn parse_items_trait_impl() {
        let content = "\
impl Display for Foo {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(f, \"Foo\")
    }
}";
        let grammar = full_rust_grammar();
        let items = parse_items(content, &grammar);

        assert_eq!(items.len(), 1);
        assert_eq!(items[0].name, "Display for Foo");
        assert_eq!(items[0].kind, "impl");
    }

    #[test]
    fn parse_items_const_and_type_alias() {
        let content = "\
pub const MAX_SIZE: usize = 1024;

pub type Result<T> = std::result::Result<T, Error>;

pub fn process() {}";
        let grammar = full_rust_grammar();
        let items = parse_items(content, &grammar);

        assert_eq!(items.len(), 3);
        assert_eq!(items[0].name, "MAX_SIZE");
        assert_eq!(items[0].kind, "const");
        assert_eq!(items[1].name, "Result");
        assert_eq!(items[1].kind, "type_alias");
        assert_eq!(items[2].name, "process");
        assert_eq!(items[2].kind, "function");
    }

    #[test]
    fn parse_items_braces_in_string() {
        let test_content =
            "pub fn string_test() {\n    let s = \"{ not a brace }\";\n    do_stuff();\n}\n\npub fn after() {}";
        let grammar = full_rust_grammar();
        let items = parse_items(test_content, &grammar);

        assert_eq!(
            items.len(),
            2,
            "Should find 2 functions despite string braces"
        );
        assert_eq!(items[0].name, "string_test");
        assert_eq!(items[0].end_line, 4);
        assert_eq!(items[1].name, "after");
    }

    #[test]
    fn parse_items_enum_variants() {
        let content = "\
pub enum Color {
    Red,
    Green,
    Blue,
}";
        let grammar = full_rust_grammar();
        let items = parse_items(content, &grammar);

        assert_eq!(items.len(), 1);
        assert_eq!(items[0].name, "Color");
        assert_eq!(items[0].kind, "enum");
        assert_eq!(items[0].start_line, 1);
        assert_eq!(items[0].end_line, 5);
    }

    #[test]
    fn parse_items_unit_struct() {
        let content = "\
pub struct Marker;

pub fn after() {}";
        let grammar = full_rust_grammar();
        let items = parse_items(content, &grammar);

        assert_eq!(items.len(), 2);
        assert_eq!(items[0].name, "Marker");
        assert_eq!(items[0].kind, "struct");
        assert_eq!(items[0].end_line, 1);
        assert_eq!(items[1].name, "after");
    }

    #[test]
    fn validate_brace_balance_works() {
        let grammar = full_rust_grammar();
        assert!(validate_brace_balance("fn foo() { bar() }", &grammar));
        assert!(validate_brace_balance(
            "fn foo() {\n    if true {\n        bar()\n    }\n}",
            &grammar
        ));
        assert!(!validate_brace_balance("fn foo() {", &grammar));
        assert!(!validate_brace_balance("fn foo() { { }", &grammar));
    }
}