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> = Lazy::new(|| {
39    Regex::new(r"[A-Za-z_][A-Za-z0-9_]*").expect("IDENT_RE: invalid regex pattern")
40});
41
42pub(super) static COMMON_KEYWORDS: Lazy<HashSet<&'static str>> = Lazy::new(|| {
43    [
44        "if",
45        "else",
46        "for",
47        "while",
48        "return",
49        "break",
50        "continue",
51        "match",
52        "case",
53        "switch",
54        "default",
55        "try",
56        "catch",
57        "throw",
58        "finally",
59        "yield",
60        "await",
61        "async",
62        "new",
63        "in",
64        "of",
65        "do",
66        "fn",
67        "function",
68        "def",
69        "class",
70        "struct",
71        "enum",
72        "trait",
73        "interface",
74        "type",
75        "impl",
76        "let",
77        "var",
78        "const",
79        "static",
80        "public",
81        "private",
82        "protected",
83        "internal",
84        "use",
85        "import",
86        "from",
87        "package",
88        "module",
89        "export",
90        "super",
91        "self",
92        "this",
93        "crate",
94        "pub",
95        "mod",
96    ]
97    .into_iter()
98    .collect()
99});
100
101/// Options for index building
102#[derive(Debug, Clone)]
103pub struct BuildOptions {
104    /// Respect .gitignore files
105    pub respect_gitignore: bool,
106    /// Maximum file size to index (in bytes)
107    pub max_file_size: u64,
108    /// File extensions to include (empty = all supported)
109    pub include_extensions: Vec<String>,
110    /// Directories to exclude
111    pub exclude_dirs: Vec<String>,
112    /// Whether to compute PageRank
113    pub compute_pagerank: bool,
114}
115
116impl Default for BuildOptions {
117    fn default() -> Self {
118        Self {
119            respect_gitignore: true,
120            max_file_size: 10 * 1024 * 1024, // 10 MB
121            include_extensions: vec![],
122            exclude_dirs: vec![
123                "node_modules".into(),
124                ".git".into(),
125                "target".into(),
126                "build".into(),
127                "dist".into(),
128                "__pycache__".into(),
129                ".venv".into(),
130                "venv".into(),
131            ],
132            compute_pagerank: true,
133        }
134    }
135}
136
137/// Intermediate parsed file structure
138pub(super) struct ParsedFile {
139    pub path: String,
140    pub language: Language,
141    pub content_hash: [u8; 32],
142    pub lines: u32,
143    pub tokens: u32,
144    pub symbols: Vec<ParsedSymbol>,
145    pub imports: Vec<Import>,
146}
147
148/// Intermediate parsed symbol
149pub(super) struct ParsedSymbol {
150    pub name: String,
151    pub kind: SymbolKind,
152    pub start_line: u32,
153    pub end_line: u32,
154    pub signature: Option<String>,
155    pub docstring: Option<String>,
156    pub parent: Option<String>,
157    pub visibility: TypesVisibility,
158    pub calls: Vec<String>,
159}