infiniloom_engine/index/builder/
types.rs

1//! Types for index building.
2//!
3//! Contains error types, build options, and intermediate structures.
4
5use crate::parser::Parser;
6use crate::types::Visibility as TypesVisibility;
7use crate::SymbolKind;
8use once_cell::sync::Lazy;
9use regex::Regex;
10use std::cell::RefCell;
11use std::collections::HashSet;
12use std::path::PathBuf;
13use thiserror::Error;
14
15use super::super::types::{Import, Language};
16
17// Thread-local parser storage to avoid re-initialization
18thread_local! {
19    pub(super) static THREAD_PARSER: RefCell<Parser> = RefCell::new(Parser::new());
20}
21
22/// Errors that can occur during index building
23#[derive(Error, Debug)]
24pub enum BuildError {
25    #[error("IO error: {0}")]
26    Io(#[from] std::io::Error),
27
28    #[error("Parse error in {file}: {message}")]
29    Parse { file: String, message: String },
30
31    #[error("Repository not found: {0}")]
32    RepoNotFound(PathBuf),
33
34    #[error("Git error: {0}")]
35    Git(String),
36}
37
38pub(super) static IDENT_RE: Lazy<Regex> =
39    Lazy::new(|| Regex::new(r"[A-Za-z_][A-Za-z0-9_]*").expect("IDENT_RE: invalid regex pattern"));
40
41pub(super) static COMMON_KEYWORDS: Lazy<HashSet<&'static str>> = Lazy::new(|| {
42    [
43        "if",
44        "else",
45        "for",
46        "while",
47        "return",
48        "break",
49        "continue",
50        "match",
51        "case",
52        "switch",
53        "default",
54        "try",
55        "catch",
56        "throw",
57        "finally",
58        "yield",
59        "await",
60        "async",
61        "new",
62        "in",
63        "of",
64        "do",
65        "fn",
66        "function",
67        "def",
68        "class",
69        "struct",
70        "enum",
71        "trait",
72        "interface",
73        "type",
74        "impl",
75        "let",
76        "var",
77        "const",
78        "static",
79        "public",
80        "private",
81        "protected",
82        "internal",
83        "use",
84        "import",
85        "from",
86        "package",
87        "module",
88        "export",
89        "super",
90        "self",
91        "this",
92        "crate",
93        "pub",
94        "mod",
95    ]
96    .into_iter()
97    .collect()
98});
99
100/// Options for index building
101#[derive(Debug, Clone)]
102pub struct BuildOptions {
103    /// Respect .gitignore files
104    pub respect_gitignore: bool,
105    /// Maximum file size to index (in bytes)
106    pub max_file_size: u64,
107    /// File extensions to include (empty = all supported)
108    pub include_extensions: Vec<String>,
109    /// Directories to exclude
110    pub exclude_dirs: Vec<String>,
111    /// Whether to compute PageRank
112    pub compute_pagerank: bool,
113}
114
115impl Default for BuildOptions {
116    fn default() -> Self {
117        Self {
118            respect_gitignore: true,
119            max_file_size: 10 * 1024 * 1024, // 10 MB
120            include_extensions: vec![],
121            exclude_dirs: vec![
122                "node_modules".into(),
123                ".git".into(),
124                "target".into(),
125                "build".into(),
126                "dist".into(),
127                "__pycache__".into(),
128                ".venv".into(),
129                "venv".into(),
130            ],
131            compute_pagerank: true,
132        }
133    }
134}
135
136/// Intermediate parsed file structure
137pub(super) struct ParsedFile {
138    pub path: String,
139    pub language: Language,
140    pub content_hash: [u8; 32],
141    pub lines: u32,
142    pub tokens: u32,
143    pub symbols: Vec<ParsedSymbol>,
144    pub imports: Vec<Import>,
145}
146
147/// Intermediate parsed symbol
148pub(super) struct ParsedSymbol {
149    pub name: String,
150    pub kind: SymbolKind,
151    pub start_line: u32,
152    pub end_line: u32,
153    pub signature: Option<String>,
154    pub docstring: Option<String>,
155    pub parent: Option<String>,
156    pub visibility: TypesVisibility,
157    pub calls: Vec<String>,
158}