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
206fn is_unknown_kind(kind: &SymbolKind) -> bool {
208 matches!(kind, SymbolKind::Unknown(_))
209}
210
211#[derive(Debug, Clone, Serialize, Deserialize)]
213pub struct SearchResult {
214 pub path: String,
216 #[serde(skip)]
218 pub lang: Language,
219 #[serde(skip_serializing_if = "is_unknown_kind")]
221 pub kind: SymbolKind,
222 #[serde(skip_serializing_if = "Option::is_none")]
225 pub symbol: Option<String>,
226 pub span: Span,
228 pub preview: String,
230 #[serde(skip_serializing_if = "Option::is_none")]
233 pub dependencies: Option<Vec<DependencyInfo>>,
234}
235
236#[derive(Debug, Clone, Serialize, Deserialize)]
238pub struct MatchResult {
239 #[serde(skip_serializing_if = "is_unknown_kind")]
241 pub kind: SymbolKind,
242 #[serde(skip_serializing_if = "Option::is_none")]
244 pub symbol: Option<String>,
245 pub span: Span,
247 pub preview: String,
249 #[serde(skip_serializing_if = "Vec::is_empty")]
251 pub context_before: Vec<String>,
252 #[serde(skip_serializing_if = "Vec::is_empty")]
254 pub context_after: Vec<String>,
255}
256
257#[derive(Debug, Clone, Serialize, Deserialize)]
259pub struct FileGroupedResult {
260 pub path: String,
262 #[serde(skip_serializing_if = "Option::is_none")]
264 pub dependencies: Option<Vec<DependencyInfo>>,
265 pub matches: Vec<MatchResult>,
267}
268
269impl SearchResult {
270 pub fn new(
271 path: String,
272 lang: Language,
273 kind: SymbolKind,
274 symbol: Option<String>,
275 span: Span,
276 scope: Option<String>,
277 preview: String,
278 ) -> Self {
279 let _ = scope;
281 Self {
282 path,
283 lang,
284 kind,
285 symbol,
286 span,
287 preview,
288 dependencies: None,
289 }
290 }
291}
292
293#[derive(Debug, Clone, Serialize, Deserialize)]
295pub struct IndexConfig {
296 pub languages: Vec<Language>,
298 pub include_patterns: Vec<String>,
300 pub exclude_patterns: Vec<String>,
302 pub follow_symlinks: bool,
304 pub max_file_size: usize,
306 pub parallel_threads: usize,
308 pub query_timeout_secs: u64,
310}
311
312impl Default for IndexConfig {
313 fn default() -> Self {
314 Self {
315 languages: vec![],
316 include_patterns: vec![],
317 exclude_patterns: vec![],
318 follow_symlinks: false,
319 max_file_size: 10 * 1024 * 1024, parallel_threads: 0, query_timeout_secs: 30, }
323 }
324}
325
326#[derive(Debug, Clone, Serialize, Deserialize)]
328pub struct IndexStats {
329 pub total_files: usize,
331 pub index_size_bytes: u64,
333 pub last_updated: String,
335 pub files_by_language: std::collections::HashMap<String, usize>,
337 pub lines_by_language: std::collections::HashMap<String, usize>,
339}
340
341#[derive(Debug, Clone, Serialize, Deserialize)]
343pub struct IndexedFile {
344 pub path: String,
346 pub language: String,
348 pub last_indexed: String,
350}
351
352#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
354#[serde(rename_all = "snake_case")]
355pub enum IndexStatus {
356 Fresh,
358 Stale,
360}
361
362#[derive(Debug, Clone, Serialize, Deserialize)]
364pub struct IndexWarning {
365 pub reason: String,
367 pub action_required: String,
369 #[serde(skip_serializing_if = "Option::is_none")]
371 pub details: Option<IndexWarningDetails>,
372}
373
374#[derive(Debug, Clone, Serialize, Deserialize)]
376pub struct IndexWarningDetails {
377 #[serde(skip_serializing_if = "Option::is_none")]
379 pub current_branch: Option<String>,
380 #[serde(skip_serializing_if = "Option::is_none")]
382 pub indexed_branch: Option<String>,
383 #[serde(skip_serializing_if = "Option::is_none")]
385 pub current_commit: Option<String>,
386 #[serde(skip_serializing_if = "Option::is_none")]
388 pub indexed_commit: Option<String>,
389}
390
391#[derive(Debug, Clone, Serialize, Deserialize)]
393pub struct PaginationInfo {
394 pub total: usize,
396 pub count: usize,
398 pub offset: usize,
400 #[serde(skip_serializing_if = "Option::is_none")]
402 pub limit: Option<usize>,
403 pub has_more: bool,
405}
406
407#[derive(Debug, Clone, Serialize, Deserialize)]
409pub struct QueryResponse {
410 #[serde(skip_serializing_if = "Option::is_none")]
414 pub ai_instruction: Option<String>,
415 pub status: IndexStatus,
417 pub can_trust_results: bool,
419 #[serde(skip_serializing_if = "Option::is_none")]
421 pub warning: Option<IndexWarning>,
422 pub pagination: PaginationInfo,
424 pub results: Vec<FileGroupedResult>,
427}
428
429#[derive(Debug, Clone, Serialize, Deserialize)]
431pub struct CompactionReport {
432 pub files_removed: usize,
434 pub space_saved_bytes: u64,
436 pub duration_ms: u64,
438}