1use serde::{Deserialize, Serialize};
7use strum::{EnumString, Display};
8
9#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
11pub struct Span {
12 pub start_line: usize,
14 pub end_line: usize,
16}
17
18impl Span {
19 pub fn new(start_line: usize, start_col: usize, end_line: usize, end_col: usize) -> Self {
20 let _ = (start_col, end_col);
22 Self {
23 start_line,
24 end_line,
25 }
26 }
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, EnumString, Display)]
31#[strum(serialize_all = "PascalCase")]
32pub enum SymbolKind {
33 Function,
34 Class,
35 Struct,
36 Enum,
37 Interface,
38 Trait,
39 Constant,
40 Variable,
41 Method,
42 Module,
43 Namespace,
44 Type,
45 Macro,
46 Property,
47 Event,
48 Import,
49 Export,
50 Attribute,
51 #[strum(default)]
55 Unknown(String),
56}
57
58#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
60#[serde(rename_all = "lowercase")]
61pub enum Language {
62 #[default]
63 Rust,
64 Python,
65 JavaScript,
66 TypeScript,
67 Vue,
68 Svelte,
69 Go,
70 Java,
71 PHP,
72 C,
73 Cpp,
74 CSharp,
75 Ruby,
76 Kotlin,
77 Swift,
78 Zig,
79 Unknown,
80}
81
82impl Language {
83 pub fn from_extension(ext: &str) -> Self {
84 match ext {
85 "rs" => Language::Rust,
86 "py" => Language::Python,
87 "js" | "mjs" | "cjs" | "jsx" => Language::JavaScript,
88 "ts" | "mts" | "cts" | "tsx" => Language::TypeScript,
89 "vue" => Language::Vue,
90 "svelte" => Language::Svelte,
91 "go" => Language::Go,
92 "java" => Language::Java,
93 "php" => Language::PHP,
94 "c" | "h" => Language::C,
95 "cpp" | "cc" | "cxx" | "hpp" | "hxx" | "C" | "H" => Language::Cpp,
96 "cs" => Language::CSharp,
97 "rb" | "rake" | "gemspec" => Language::Ruby,
98 "kt" | "kts" => Language::Kotlin,
99 "swift" => Language::Swift,
100 "zig" => Language::Zig,
101 _ => Language::Unknown,
102 }
103 }
104
105 pub fn from_name(name: &str) -> Option<Self> {
110 match name.to_lowercase().as_str() {
111 "rust" | "rs" => Some(Language::Rust),
112 "python" | "py" => Some(Language::Python),
113 "javascript" | "js" => Some(Language::JavaScript),
114 "typescript" | "ts" => Some(Language::TypeScript),
115 "vue" => Some(Language::Vue),
116 "svelte" => Some(Language::Svelte),
117 "go" => Some(Language::Go),
118 "java" => Some(Language::Java),
119 "php" => Some(Language::PHP),
120 "c" => Some(Language::C),
121 "cpp" | "c++" => Some(Language::Cpp),
122 "csharp" | "cs" | "c#" => Some(Language::CSharp),
123 "ruby" | "rb" => Some(Language::Ruby),
124 "kotlin" | "kt" => Some(Language::Kotlin),
125 "zig" => Some(Language::Zig),
126 _ => None,
127 }
128 }
129
130 pub fn supported_names_help() -> &'static str {
132 "rust (rs), python (py), javascript (js), typescript (ts), vue, svelte, \
133 go, java, php, c, cpp (c++), csharp (cs, c#), ruby (rb), kotlin (kt), zig"
134 }
135
136 pub fn is_supported(&self) -> bool {
141 match self {
142 Language::Rust => true,
143 Language::TypeScript => true,
144 Language::JavaScript => true,
145 Language::Vue => true,
146 Language::Svelte => true,
147 Language::Python => true,
148 Language::Go => true,
149 Language::Java => true,
150 Language::PHP => true,
151 Language::C => true,
152 Language::Cpp => true,
153 Language::CSharp => true,
154 Language::Ruby => true,
155 Language::Kotlin => true,
156 Language::Swift => false, Language::Zig => true,
158 Language::Unknown => false,
159 }
160 }
161}
162
163#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
165#[serde(rename_all = "lowercase")]
166pub enum ImportType {
167 Internal,
169 External,
171 Stdlib,
173}
174
175#[derive(Debug, Clone, Serialize, Deserialize)]
178pub struct DependencyInfo {
179 pub path: String,
181 #[serde(skip_serializing_if = "Option::is_none")]
183 pub line: Option<usize>,
184 #[serde(skip_serializing_if = "Option::is_none")]
186 pub symbols: Option<Vec<String>>,
187}
188
189#[derive(Debug, Clone)]
191pub struct Dependency {
192 pub file_id: i64,
194 pub imported_path: String,
196 pub resolved_file_id: Option<i64>,
198 pub import_type: ImportType,
200 pub line_number: usize,
202 pub imported_symbols: Option<Vec<String>>,
204}
205
206#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
213pub struct SymbolRef {
214 pub name: String,
216 pub kind: SymbolKind,
218 pub span: Span,
220}
221
222fn is_unknown_kind(kind: &SymbolKind) -> bool {
224 matches!(kind, SymbolKind::Unknown(_))
225}
226
227#[derive(Debug, Clone, Serialize, Deserialize)]
229pub struct SearchResult {
230 pub path: String,
232 #[serde(skip)]
234 pub lang: Language,
235 #[serde(skip_serializing_if = "is_unknown_kind")]
237 pub kind: SymbolKind,
238 #[serde(skip_serializing_if = "Option::is_none")]
241 pub symbol: Option<String>,
242 pub span: Span,
244 pub preview: String,
246 #[serde(skip_serializing_if = "Option::is_none")]
249 pub dependencies: Option<Vec<DependencyInfo>>,
250}
251
252#[derive(Debug, Clone, Serialize, Deserialize)]
254pub struct MatchResult {
255 #[serde(skip_serializing_if = "is_unknown_kind")]
257 pub kind: SymbolKind,
258 #[serde(skip_serializing_if = "Option::is_none")]
260 pub symbol: Option<String>,
261 pub span: Span,
263 pub preview: String,
265 #[serde(skip_serializing_if = "Vec::is_empty")]
267 pub context_before: Vec<String>,
268 #[serde(skip_serializing_if = "Vec::is_empty")]
270 pub context_after: Vec<String>,
271}
272
273#[derive(Debug, Clone, Serialize, Deserialize)]
275pub struct FileGroupedResult {
276 pub path: String,
278 #[serde(skip_serializing_if = "Option::is_none")]
280 pub dependencies: Option<Vec<DependencyInfo>>,
281 pub matches: Vec<MatchResult>,
283}
284
285impl SearchResult {
286 pub fn new(
287 path: String,
288 lang: Language,
289 kind: SymbolKind,
290 symbol: Option<String>,
291 span: Span,
292 scope: Option<String>,
293 preview: String,
294 ) -> Self {
295 let _ = scope;
297 Self {
298 path,
299 lang,
300 kind,
301 symbol,
302 span,
303 preview,
304 dependencies: None,
305 }
306 }
307}
308
309#[derive(Debug, Clone, Serialize, Deserialize)]
311pub struct IndexConfig {
312 pub languages: Vec<Language>,
314 pub include_patterns: Vec<String>,
316 pub exclude_patterns: Vec<String>,
318 pub follow_symlinks: bool,
320 pub max_file_size: usize,
322 pub parallel_threads: usize,
324 pub query_timeout_secs: u64,
326 pub max_posting_list_entries: usize,
329}
330
331impl Default for IndexConfig {
332 fn default() -> Self {
333 Self {
334 languages: vec![],
335 include_patterns: vec![],
336 exclude_patterns: vec![],
337 follow_symlinks: false,
338 max_file_size: 10 * 1024 * 1024, parallel_threads: 0, query_timeout_secs: 30, max_posting_list_entries: 500_000, }
343 }
344}
345
346#[derive(Debug, Clone, Serialize, Deserialize)]
348pub struct IndexStats {
349 pub total_files: usize,
351 pub index_size_bytes: u64,
353 pub last_updated: String,
355 pub files_by_language: std::collections::HashMap<String, usize>,
357 pub lines_by_language: std::collections::HashMap<String, usize>,
359}
360
361#[derive(Debug, Clone, Serialize, Deserialize)]
363pub struct IndexedFile {
364 pub path: String,
366 pub language: String,
368 pub last_indexed: String,
370}
371
372#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
374#[serde(rename_all = "snake_case")]
375pub enum IndexStatus {
376 Fresh,
378 Stale,
380}
381
382#[derive(Debug, Clone, Serialize, Deserialize)]
384pub struct IndexWarning {
385 pub reason: String,
387 pub action_required: String,
389 #[serde(skip_serializing_if = "Option::is_none")]
391 pub details: Option<IndexWarningDetails>,
392}
393
394#[derive(Debug, Clone, Serialize, Deserialize)]
396pub struct IndexWarningDetails {
397 #[serde(skip_serializing_if = "Option::is_none")]
399 pub current_branch: Option<String>,
400 #[serde(skip_serializing_if = "Option::is_none")]
402 pub indexed_branch: Option<String>,
403 #[serde(skip_serializing_if = "Option::is_none")]
405 pub current_commit: Option<String>,
406 #[serde(skip_serializing_if = "Option::is_none")]
408 pub indexed_commit: Option<String>,
409}
410
411#[derive(Debug, Clone, Serialize, Deserialize)]
413pub struct PaginationInfo {
414 pub total: usize,
416 pub count: usize,
418 pub offset: usize,
420 #[serde(skip_serializing_if = "Option::is_none")]
422 pub limit: Option<usize>,
423 pub has_more: bool,
425}
426
427#[derive(Debug, Clone, Serialize, Deserialize)]
429pub struct QueryResponse {
430 #[serde(skip_serializing_if = "Option::is_none")]
434 pub ai_instruction: Option<String>,
435 pub status: IndexStatus,
437 pub can_trust_results: bool,
439 #[serde(skip_serializing_if = "Option::is_none")]
441 pub warning: Option<IndexWarning>,
442 pub pagination: PaginationInfo,
444 pub results: Vec<FileGroupedResult>,
447}
448
449#[derive(Debug, Clone, Serialize, Deserialize)]
451pub struct CompactionReport {
452 pub files_removed: usize,
454 pub space_saved_bytes: u64,
456 pub duration_ms: u64,
458}
459
460#[cfg(test)]
461mod tests {
462 use super::*;
463
464 #[test]
465 fn test_symbol_ref_json_shape() {
466 let sym = SymbolRef {
467 name: "my_function".to_string(),
468 kind: SymbolKind::Function,
469 span: Span { start_line: 10, end_line: 20 },
470 };
471 let json = serde_json::to_value(&sym).unwrap();
472 assert_eq!(json["name"], "my_function");
473 assert_eq!(json["kind"], "Function");
474 assert_eq!(json["span"]["start_line"], 10);
475 assert_eq!(json["span"]["end_line"], 20);
476 assert!(json.as_array().is_none());
477 }
478
479 #[test]
480 fn test_symbol_ref_roundtrip() {
481 let original = SymbolRef {
482 name: "MyStruct".to_string(),
483 kind: SymbolKind::Struct,
484 span: Span { start_line: 1, end_line: 5 },
485 };
486 let json = serde_json::to_string(&original).unwrap();
487 let decoded: SymbolRef = serde_json::from_str(&json).unwrap();
488 assert_eq!(original, decoded);
489 }
490
491 #[test]
492 fn test_symbol_ref_exact_json() {
493 let sym = SymbolRef {
494 name: "Foo".to_string(),
495 kind: SymbolKind::Class,
496 span: Span { start_line: 3, end_line: 7 },
497 };
498 let json = serde_json::to_string(&sym).unwrap();
499 assert_eq!(
500 json,
501 r#"{"name":"Foo","kind":"Class","span":{"start_line":3,"end_line":7}}"#
502 );
503 }
504}