1use serde::{Deserialize, Serialize};
2use std::path::{Path, PathBuf};
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum CkError {
7 #[error("IO error: {0}")]
8 Io(#[from] std::io::Error),
9
10 #[error("Regex error: {0}")]
11 Regex(#[from] regex::Error),
12
13 #[error("Serialization error: {0}")]
14 Serialization(#[from] bincode::Error),
15
16 #[error("JSON error: {0}")]
17 Json(#[from] serde_json::Error),
18
19 #[error("Index error: {0}")]
20 Index(String),
21
22 #[error("Search error: {0}")]
23 Search(String),
24
25 #[error("Embedding error: {0}")]
26 Embedding(String),
27
28 #[error("Other error: {0}")]
29 Other(String),
30}
31
32pub type Result<T> = std::result::Result<T, CkError>;
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
35pub enum Language {
36 Rust,
37 Python,
38 JavaScript,
39 TypeScript,
40 Haskell,
41 Go,
42 Java,
43 C,
44 Cpp,
45 CSharp,
46 Ruby,
47 Php,
48 Swift,
49 Kotlin,
50}
51
52impl Language {
53 pub fn from_extension(ext: &str) -> Option<Self> {
54 match ext {
55 "rs" => Some(Language::Rust),
56 "py" => Some(Language::Python),
57 "js" => Some(Language::JavaScript),
58 "ts" | "tsx" => Some(Language::TypeScript),
59 "hs" | "lhs" => Some(Language::Haskell),
60 "go" => Some(Language::Go),
61 "java" => Some(Language::Java),
62 "c" => Some(Language::C),
63 "cpp" | "cc" | "cxx" | "c++" => Some(Language::Cpp),
64 "h" | "hpp" => Some(Language::Cpp), "cs" => Some(Language::CSharp),
66 "rb" => Some(Language::Ruby),
67 "php" => Some(Language::Php),
68 "swift" => Some(Language::Swift),
69 "kt" | "kts" => Some(Language::Kotlin),
70 _ => None,
71 }
72 }
73
74 pub fn from_path(path: &Path) -> Option<Self> {
75 path.extension()
76 .and_then(|ext| ext.to_str())
77 .and_then(Self::from_extension)
78 }
79}
80
81impl std::fmt::Display for Language {
82 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83 let name = match self {
84 Language::Rust => "rust",
85 Language::Python => "python",
86 Language::JavaScript => "javascript",
87 Language::TypeScript => "typescript",
88 Language::Haskell => "haskell",
89 Language::Go => "go",
90 Language::Java => "java",
91 Language::C => "c",
92 Language::Cpp => "cpp",
93 Language::CSharp => "csharp",
94 Language::Ruby => "ruby",
95 Language::Php => "php",
96 Language::Swift => "swift",
97 Language::Kotlin => "kotlin",
98 };
99 write!(f, "{}", name)
100 }
101}
102
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct Span {
105 pub byte_start: usize,
106 pub byte_end: usize,
107 pub line_start: usize,
108 pub line_end: usize,
109}
110
111#[derive(Debug, Clone, Serialize, Deserialize)]
112pub struct FileMetadata {
113 pub path: PathBuf,
114 pub hash: String,
115 pub last_modified: u64,
116 pub size: u64,
117}
118
119#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct SearchResult {
121 pub file: PathBuf,
122 pub span: Span,
123 pub score: f32,
124 pub preview: String,
125 #[serde(skip_serializing_if = "Option::is_none")]
126 pub lang: Option<Language>,
127 #[serde(skip_serializing_if = "Option::is_none")]
128 pub symbol: Option<String>,
129 #[serde(skip_serializing_if = "Option::is_none")]
130 pub chunk_hash: Option<String>,
131 #[serde(skip_serializing_if = "Option::is_none")]
132 pub index_epoch: Option<u64>,
133}
134
135#[derive(Debug, Clone, Serialize, Deserialize)]
136pub struct JsonSearchResult {
137 pub file: String,
138 pub span: Span,
139 pub lang: Option<Language>,
140 pub symbol: Option<String>,
141 pub score: f32,
142 pub signals: SearchSignals,
143 pub preview: String,
144 pub model: String,
145}
146
147#[derive(Debug, Clone, Serialize, Deserialize)]
148pub struct JsonlSearchResult {
149 pub path: String,
150 pub span: Span,
151 pub language: Option<String>,
152 #[serde(skip_serializing_if = "Option::is_none")]
153 pub snippet: Option<String>,
154 #[serde(skip_serializing_if = "Option::is_none")]
155 pub score: Option<f32>,
156 #[serde(skip_serializing_if = "Option::is_none")]
157 pub chunk_hash: Option<String>,
158 #[serde(skip_serializing_if = "Option::is_none")]
159 pub index_epoch: Option<u64>,
160}
161
162#[derive(Debug, Clone, Serialize, Deserialize)]
163pub struct SearchSignals {
164 pub lex_rank: Option<usize>,
165 pub vec_rank: Option<usize>,
166 pub rrf_score: f32,
167}
168
169#[derive(Debug, Clone, PartialEq)]
170pub enum SearchMode {
171 Regex,
172 Lexical,
173 Semantic,
174 Hybrid,
175}
176
177#[derive(Debug, Clone)]
178pub struct SearchOptions {
179 pub mode: SearchMode,
180 pub query: String,
181 pub path: PathBuf,
182 pub top_k: Option<usize>,
183 pub threshold: Option<f32>,
184 pub case_insensitive: bool,
185 pub whole_word: bool,
186 pub fixed_string: bool,
187 pub line_numbers: bool,
188 pub context_lines: usize,
189 pub before_context_lines: usize,
190 pub after_context_lines: usize,
191 pub recursive: bool,
192 pub json_output: bool,
193 pub jsonl_output: bool,
194 pub no_snippet: bool,
195 pub reindex: bool,
196 pub show_scores: bool,
197 pub show_filenames: bool,
198 pub files_with_matches: bool,
199 pub files_without_matches: bool,
200 pub exclude_patterns: Vec<String>,
201 pub respect_gitignore: bool,
202 pub full_section: bool,
203}
204
205impl JsonlSearchResult {
206 pub fn from_search_result(result: &SearchResult, include_snippet: bool) -> Self {
207 Self {
208 path: result.file.to_string_lossy().to_string(),
209 span: result.span.clone(),
210 language: result.lang.as_ref().map(|l| l.to_string()),
211 snippet: if include_snippet {
212 Some(result.preview.clone())
213 } else {
214 None
215 },
216 score: if result.score >= 0.0 {
217 Some(result.score)
218 } else {
219 None
220 },
221 chunk_hash: result.chunk_hash.clone(),
222 index_epoch: result.index_epoch,
223 }
224 }
225}
226
227impl Default for SearchOptions {
228 fn default() -> Self {
229 Self {
230 mode: SearchMode::Regex,
231 query: String::new(),
232 path: PathBuf::from("."),
233 top_k: None,
234 threshold: None,
235 case_insensitive: false,
236 whole_word: false,
237 fixed_string: false,
238 line_numbers: false,
239 context_lines: 0,
240 before_context_lines: 0,
241 after_context_lines: 0,
242 recursive: true,
243 json_output: false,
244 jsonl_output: false,
245 no_snippet: false,
246 reindex: false,
247 show_scores: false,
248 show_filenames: false,
249 files_with_matches: false,
250 files_without_matches: false,
251 exclude_patterns: get_default_exclude_patterns(),
252 respect_gitignore: true,
253 full_section: false,
254 }
255 }
256}
257
258pub fn get_default_exclude_patterns() -> Vec<String> {
261 vec![
262 ".ck".to_string(),
264 ".fastembed_cache".to_string(),
266 ".cache".to_string(),
267 "__pycache__".to_string(),
268 ".git".to_string(),
270 ".svn".to_string(),
271 ".hg".to_string(),
272 "target".to_string(), "build".to_string(), "dist".to_string(), "node_modules".to_string(), ".gradle".to_string(), ".mvn".to_string(), "bin".to_string(), "obj".to_string(), "venv".to_string(),
283 ".venv".to_string(),
284 "env".to_string(),
285 ".env".to_string(),
286 "virtualenv".to_string(),
287 ".vscode".to_string(),
289 ".idea".to_string(),
290 ".eclipse".to_string(),
291 "tmp".to_string(),
293 "temp".to_string(),
294 ".tmp".to_string(),
295 ]
296}
297
298pub fn get_sidecar_path(repo_root: &Path, file_path: &Path) -> PathBuf {
299 let relative = file_path.strip_prefix(repo_root).unwrap_or(file_path);
300 let mut sidecar = repo_root.join(".ck");
301 sidecar.push(relative);
302 let ext = relative
303 .extension()
304 .map(|e| format!("{}.ck", e.to_string_lossy()))
305 .unwrap_or_else(|| "ck".to_string());
306 sidecar.set_extension(ext);
307 sidecar
308}
309
310pub fn compute_file_hash(path: &Path) -> Result<String> {
311 let data = std::fs::read(path)?;
312 let hash = blake3::hash(&data);
313 Ok(hash.to_hex().to_string())
314}
315
316#[cfg(test)]
317mod tests {
318 use super::*;
319 use std::fs;
320 use tempfile::TempDir;
321
322 #[test]
323 fn test_span_creation() {
324 let span = Span {
325 byte_start: 0,
326 byte_end: 10,
327 line_start: 1,
328 line_end: 2,
329 };
330
331 assert_eq!(span.byte_start, 0);
332 assert_eq!(span.byte_end, 10);
333 assert_eq!(span.line_start, 1);
334 assert_eq!(span.line_end, 2);
335 }
336
337 #[test]
338 fn test_search_options_default() {
339 let options = SearchOptions::default();
340 assert!(matches!(options.mode, SearchMode::Regex));
341 assert_eq!(options.query, "");
342 assert_eq!(options.path, PathBuf::from("."));
343 assert_eq!(options.top_k, None);
344 assert_eq!(options.threshold, None);
345 assert!(!options.case_insensitive);
346 assert!(!options.whole_word);
347 assert!(!options.fixed_string);
348 assert!(!options.line_numbers);
349 assert_eq!(options.context_lines, 0);
350 assert!(options.recursive);
351 assert!(!options.json_output);
352 assert!(!options.reindex);
353 assert!(!options.show_scores);
354 assert!(!options.show_filenames);
355 }
356
357 #[test]
358 fn test_file_metadata_serialization() {
359 let metadata = FileMetadata {
360 path: PathBuf::from("test.txt"),
361 hash: "abc123".to_string(),
362 last_modified: 1234567890,
363 size: 1024,
364 };
365
366 let json = serde_json::to_string(&metadata).unwrap();
367 let deserialized: FileMetadata = serde_json::from_str(&json).unwrap();
368
369 assert_eq!(metadata.path, deserialized.path);
370 assert_eq!(metadata.hash, deserialized.hash);
371 assert_eq!(metadata.last_modified, deserialized.last_modified);
372 assert_eq!(metadata.size, deserialized.size);
373 }
374
375 #[test]
376 fn test_search_result_serialization() {
377 let result = SearchResult {
378 file: PathBuf::from("test.txt"),
379 span: Span {
380 byte_start: 0,
381 byte_end: 10,
382 line_start: 1,
383 line_end: 1,
384 },
385 score: 0.95,
386 preview: "hello world".to_string(),
387 lang: Some(Language::Rust),
388 symbol: Some("main".to_string()),
389 chunk_hash: Some("abc123".to_string()),
390 index_epoch: Some(1699123456),
391 };
392
393 let json = serde_json::to_string(&result).unwrap();
394 let deserialized: SearchResult = serde_json::from_str(&json).unwrap();
395
396 assert_eq!(result.file, deserialized.file);
397 assert_eq!(result.score, deserialized.score);
398 assert_eq!(result.preview, deserialized.preview);
399 assert_eq!(result.lang, deserialized.lang);
400 assert_eq!(result.symbol, deserialized.symbol);
401 assert_eq!(result.chunk_hash, deserialized.chunk_hash);
402 assert_eq!(result.index_epoch, deserialized.index_epoch);
403 }
404
405 #[test]
406 fn test_jsonl_search_result_conversion() {
407 let result = SearchResult {
408 file: PathBuf::from("src/auth.rs"),
409 span: Span {
410 byte_start: 1203,
411 byte_end: 1456,
412 line_start: 42,
413 line_end: 58,
414 },
415 score: 0.89,
416 preview: "function authenticate(user) {...}".to_string(),
417 lang: Some(Language::Rust),
418 symbol: Some("authenticate".to_string()),
419 chunk_hash: Some("abc123def456".to_string()),
420 index_epoch: Some(1699123456),
421 };
422
423 let jsonl_with_snippet = JsonlSearchResult::from_search_result(&result, true);
425 assert_eq!(jsonl_with_snippet.path, "src/auth.rs");
426 assert_eq!(jsonl_with_snippet.span.line_start, 42);
427 assert_eq!(jsonl_with_snippet.language, Some("rust".to_string()));
428 assert_eq!(
429 jsonl_with_snippet.snippet,
430 Some("function authenticate(user) {...}".to_string())
431 );
432 assert_eq!(jsonl_with_snippet.score, Some(0.89));
433 assert_eq!(
434 jsonl_with_snippet.chunk_hash,
435 Some("abc123def456".to_string())
436 );
437 assert_eq!(jsonl_with_snippet.index_epoch, Some(1699123456));
438
439 let jsonl_no_snippet = JsonlSearchResult::from_search_result(&result, false);
441 assert_eq!(jsonl_no_snippet.snippet, None);
442 assert_eq!(jsonl_no_snippet.path, "src/auth.rs");
443 }
444
445 #[test]
446 fn test_get_sidecar_path() {
447 let repo_root = PathBuf::from("/home/user/project");
448 let file_path = PathBuf::from("/home/user/project/src/main.rs");
449
450 let sidecar = get_sidecar_path(&repo_root, &file_path);
451 let expected = PathBuf::from("/home/user/project/.ck/src/main.rs.ck");
452
453 assert_eq!(sidecar, expected);
454 }
455
456 #[test]
457 fn test_get_sidecar_path_no_extension() {
458 let repo_root = PathBuf::from("/project");
459 let file_path = PathBuf::from("/project/README");
460
461 let sidecar = get_sidecar_path(&repo_root, &file_path);
462 let expected = PathBuf::from("/project/.ck/README.ck");
463
464 assert_eq!(sidecar, expected);
465 }
466
467 #[test]
468 fn test_compute_file_hash() {
469 let temp_dir = TempDir::new().unwrap();
470 let file_path = temp_dir.path().join("test.txt");
471
472 fs::write(&file_path, "hello world").unwrap();
473
474 let hash1 = compute_file_hash(&file_path).unwrap();
475 let hash2 = compute_file_hash(&file_path).unwrap();
476
477 assert_eq!(hash1, hash2);
479 assert!(!hash1.is_empty());
480
481 fs::write(&file_path, "hello rust").unwrap();
483 let hash3 = compute_file_hash(&file_path).unwrap();
484 assert_ne!(hash1, hash3);
485 }
486
487 #[test]
488 fn test_compute_file_hash_nonexistent() {
489 let result = compute_file_hash(&PathBuf::from("nonexistent.txt"));
490 assert!(result.is_err());
491 }
492
493 #[test]
494 fn test_json_search_result_serialization() {
495 let signals = SearchSignals {
496 lex_rank: Some(1),
497 vec_rank: Some(2),
498 rrf_score: 0.85,
499 };
500
501 let result = JsonSearchResult {
502 file: "test.txt".to_string(),
503 span: Span {
504 byte_start: 0,
505 byte_end: 5,
506 line_start: 1,
507 line_end: 1,
508 },
509 lang: None, symbol: None,
511 score: 0.95,
512 signals,
513 preview: "hello".to_string(),
514 model: "bge-small".to_string(),
515 };
516
517 let json = serde_json::to_string(&result).unwrap();
518 let deserialized: JsonSearchResult = serde_json::from_str(&json).unwrap();
519
520 assert_eq!(result.file, deserialized.file);
521 assert_eq!(result.score, deserialized.score);
522 assert_eq!(result.signals.rrf_score, deserialized.signals.rrf_score);
523 assert_eq!(result.model, deserialized.model);
524 }
525
526 #[test]
527 fn test_language_from_extension() {
528 assert_eq!(Language::from_extension("rs"), Some(Language::Rust));
529 assert_eq!(Language::from_extension("py"), Some(Language::Python));
530 assert_eq!(Language::from_extension("js"), Some(Language::JavaScript));
531 assert_eq!(Language::from_extension("ts"), Some(Language::TypeScript));
532 assert_eq!(Language::from_extension("tsx"), Some(Language::TypeScript));
533 assert_eq!(Language::from_extension("hs"), Some(Language::Haskell));
534 assert_eq!(Language::from_extension("lhs"), Some(Language::Haskell));
535 assert_eq!(Language::from_extension("go"), Some(Language::Go));
536 assert_eq!(Language::from_extension("java"), Some(Language::Java));
537 assert_eq!(Language::from_extension("c"), Some(Language::C));
538 assert_eq!(Language::from_extension("cpp"), Some(Language::Cpp));
539 assert_eq!(Language::from_extension("cs"), Some(Language::CSharp));
540 assert_eq!(Language::from_extension("rb"), Some(Language::Ruby));
541 assert_eq!(Language::from_extension("php"), Some(Language::Php));
542 assert_eq!(Language::from_extension("swift"), Some(Language::Swift));
543 assert_eq!(Language::from_extension("kt"), Some(Language::Kotlin));
544 assert_eq!(Language::from_extension("kts"), Some(Language::Kotlin));
545 assert_eq!(Language::from_extension("unknown"), None);
546 }
547
548 #[test]
549 fn test_language_from_path() {
550 assert_eq!(
551 Language::from_path(&PathBuf::from("test.rs")),
552 Some(Language::Rust)
553 );
554 assert_eq!(
555 Language::from_path(&PathBuf::from("test.py")),
556 Some(Language::Python)
557 );
558 assert_eq!(
559 Language::from_path(&PathBuf::from("test.js")),
560 Some(Language::JavaScript)
561 );
562 assert_eq!(
563 Language::from_path(&PathBuf::from("test.hs")),
564 Some(Language::Haskell)
565 );
566 assert_eq!(
567 Language::from_path(&PathBuf::from("test.lhs")),
568 Some(Language::Haskell)
569 );
570 assert_eq!(
571 Language::from_path(&PathBuf::from("test.go")),
572 Some(Language::Go)
573 );
574 assert_eq!(Language::from_path(&PathBuf::from("test.unknown")), None); assert_eq!(Language::from_path(&PathBuf::from("noext")), None); }
577
578 #[test]
579 fn test_language_display() {
580 assert_eq!(Language::Rust.to_string(), "rust");
581 assert_eq!(Language::Python.to_string(), "python");
582 assert_eq!(Language::JavaScript.to_string(), "javascript");
583 assert_eq!(Language::TypeScript.to_string(), "typescript");
584 assert_eq!(Language::Go.to_string(), "go");
585 assert_eq!(Language::Java.to_string(), "java");
586 }
587}