vectordb-cli 1.4.5

A CLI tool for semantic code search.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use std::io;
use std::path::PathBuf;
use thiserror::Error;
// use syn;
use ort;
// use serde::ser::Error as SerError;
// use serde::de::Error as DeError;

/// Result type for VectorDB operations
pub type Result<T> = std::result::Result<T, VectorDBError>;

/// Errors that can occur in the VectorDB system
#[derive(Error, Debug)]
pub enum VectorDBError {
    #[error("File not found: {0}")]
    FileNotFound(String),

    #[error("Failed to read file {path}: {source}")]
    FileReadError { path: PathBuf, source: io::Error },

    #[error("Failed to write file {path}: {source}")]
    FileWriteError { path: PathBuf, source: io::Error },

    #[error("Failed to create directory {path}: {source}")]
    DirectoryCreationError { path: PathBuf, source: io::Error },

    #[error("Failed to access file metadata for {path}: {source}")]
    MetadataError { path: PathBuf, source: io::Error },

    #[error("Error serializing or deserializing data: {0}")]
    SerializationError(String),

    #[error("Error generating embedding: {0}")]
    EmbeddingError(String),

    #[error("Database error: {0}")]
    DatabaseError(String),

    #[error("AST traversal error: {0}")]
    ASTTraversalError(String),

    #[error("Invalid parameter: {0}")]
    InvalidParameter(String),

    #[error("Invalid path: {0}")]
    InvalidPath(String),

    #[error("Cache error: {0}")]
    CacheError(String),

    #[error("Parser error: {0}")]
    ParserError(String),

    #[error("Unsupported language: {0}")]
    UnsupportedLanguage(String),

    #[error("HNSW index error: {0}")]
    HNSWError(String),

    #[error("HNSW index dimension ({expected}) does not match query/data dimension ({found})")]
    DimensionMismatch { expected: usize, found: usize },

    #[error("IO error: {0}")]
    IOError(#[from] io::Error),

    #[error("Code analysis error: {0}")]
    CodeAnalysisError(String),

    #[error("General error: {0}")]
    GeneralError(String),

    #[error("Directory not found: {0}")]
    DirectoryNotFound(String),

    #[error("Repository error: {0}")]
    RepositoryError(String),

    #[error("Repository not found: {0}")]
    RepositoryNotFound(String),

    #[error("Error deserializing data: {0}")]
    DeserializationError(String),

    #[error("Search error: {0}")]
    SearchError(String),

    #[error("Other error: {0}")]
    Other(String),

    #[error("Configuration error: {0}")]
    ConfigurationError(String),

    #[error("Indexing error: {0}")]
    IndexingError(String),

    #[error("Directory '{0}' is not present in the index")]
    DirectoryNotIndexed(String),

    #[error("Search index not found or not built")]
    IndexNotFound,

    #[error("Operation cancelled by user")]
    OperationCancelled,

    #[error("Mutex lock error: {0}")]
    MutexLockError(String),

    /// Error originating from the Qdrant client
    #[error("Qdrant client error: {0}")]
    QdrantError(#[from] qdrant_client::QdrantError),

    /// Error related to Git operations
    #[error("Git error: {0}")]
    GitError(#[from] git2::Error),

    /// Error when a required feature is not yet implemented
    #[error("Feature not implemented: {0}")]
    NotImplemented(String),

    /// Error for ONNX Runtime
    #[error("ONNX Runtime error: {0}")]
    OrtError(#[from] ort::Error),
}

// Implement conversion from anyhow::Error
impl From<anyhow::Error> for VectorDBError {
    fn from(err: anyhow::Error) -> Self {
        // This is a simplified conversion. In a real application, you might
        // want to inspect the anyhow::Error further (e.g., using downcasting)
        // to map it to more specific VectorDBError variants if possible.
        // For now, mapping to HNSWError based on the existing test expectation.
        VectorDBError::HNSWError(err.to_string())
    }
}

// Add Clone implementation for VectorDBError to support parallel processing
impl Clone for VectorDBError {
    fn clone(&self) -> Self {
        match self {
            Self::FileNotFound(s) => Self::FileNotFound(s.clone()),
            Self::FileReadError { path, source } => Self::FileReadError {
                path: path.clone(),
                source: io::Error::new(source.kind(), source.to_string()),
            },
            Self::FileWriteError { path, source } => Self::FileWriteError {
                path: path.clone(),
                source: io::Error::new(source.kind(), source.to_string()),
            },
            Self::DirectoryCreationError { path, source } => Self::DirectoryCreationError {
                path: path.clone(),
                source: io::Error::new(source.kind(), source.to_string()),
            },
            Self::MetadataError { path, source } => Self::MetadataError {
                path: path.clone(),
                source: io::Error::new(source.kind(), source.to_string()),
            },
            Self::SerializationError(s) => Self::SerializationError(s.clone()),
            Self::EmbeddingError(s) => Self::EmbeddingError(s.clone()),
            Self::DatabaseError(s) => Self::DatabaseError(s.clone()),
            Self::ASTTraversalError(s) => Self::ASTTraversalError(s.clone()),
            Self::InvalidParameter(s) => Self::InvalidParameter(s.clone()),
            Self::InvalidPath(s) => Self::InvalidPath(s.clone()),
            Self::CacheError(s) => Self::CacheError(s.clone()),
            Self::ParserError(s) => Self::ParserError(s.clone()),
            Self::UnsupportedLanguage(s) => Self::UnsupportedLanguage(s.clone()),
            Self::HNSWError(s) => Self::HNSWError(s.clone()),
            Self::IOError(e) => Self::IOError(io::Error::new(e.kind(), e.to_string())),
            Self::CodeAnalysisError(s) => Self::CodeAnalysisError(s.clone()),
            Self::GeneralError(s) => Self::GeneralError(s.clone()),
            Self::DirectoryNotFound(s) => Self::DirectoryNotFound(s.clone()),
            Self::RepositoryError(s) => Self::RepositoryError(s.clone()),
            Self::RepositoryNotFound(s) => Self::RepositoryNotFound(s.clone()),
            Self::DeserializationError(s) => Self::DeserializationError(s.clone()),
            Self::SearchError(s) => Self::SearchError(s.clone()),
            Self::Other(s) => Self::Other(s.clone()),
            Self::ConfigurationError(s) => Self::ConfigurationError(s.clone()),
            Self::DimensionMismatch { expected, found } => Self::DimensionMismatch {
                expected: *expected,
                found: *found,
            },
            Self::IndexingError(s) => Self::IndexingError(s.clone()),
            Self::DirectoryNotIndexed(s) => Self::DirectoryNotIndexed(s.clone()),
            Self::IndexNotFound => Self::IndexNotFound,
            Self::OperationCancelled => Self::OperationCancelled,
            Self::MutexLockError(s) => Self::MutexLockError(s.clone()),
            Self::QdrantError(e) => Self::GeneralError(format!("Cannot clone QdrantError: {}", e)),
            Self::GitError(_) => Self::GeneralError("Cannot clone git2::Error".to_string()),
            Self::NotImplemented(s) => Self::NotImplemented(s.clone()),
            Self::OrtError(_) => Self::GeneralError("Cannot clone ort::Error".to_string()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io;
    use std::path::PathBuf;
    use serde_json; // Ensure serde_json is available in dev-dependencies

    #[test]
    fn test_display_file_not_found() {
        let err = VectorDBError::FileNotFound("missing.txt".to_string());
        assert_eq!(err.to_string(), "File not found: missing.txt");
    }

    #[test]
    fn test_display_file_read_error() {
        let io_err = io::Error::new(io::ErrorKind::PermissionDenied, "cannot read");
        let err = VectorDBError::FileReadError { path: PathBuf::from("secret.txt"), source: io_err };
        assert_eq!(err.to_string(), "Failed to read file secret.txt: cannot read");
    }

    #[test]
    fn test_display_file_write_error() {
        let io_err = io::Error::new(io::ErrorKind::PermissionDenied, "cannot write");
        let err = VectorDBError::FileWriteError { path: PathBuf::from("output.log"), source: io_err };
        assert_eq!(err.to_string(), "Failed to write file output.log: cannot write");
    }

    #[test]
    fn test_display_directory_creation_error() {
        let io_err = io::Error::new(io::ErrorKind::AlreadyExists, "dir exists");
        let err = VectorDBError::DirectoryCreationError{ path: PathBuf::from("my_dir"), source: io_err };
        assert_eq!(err.to_string(), "Failed to create directory my_dir: dir exists");
    }

    #[test]
    fn test_display_metadata_error() {
        let io_err = io::Error::new(io::ErrorKind::NotFound, "no metadata");
        let err = VectorDBError::MetadataError{ path: PathBuf::from("no_file"), source: io_err };
        assert_eq!(err.to_string(), "Failed to access file metadata for no_file: no metadata");
    }

    #[test]
    fn test_display_serialization_error() {
        // Create a dummy value that cannot be serialized easily by default serde_json
        #[derive(Debug)] // Add Debug for easier assertion messages if needed
        struct Unserializable;
        impl serde::Serialize for Unserializable {
             // Correct the return type to match the trait
             fn serialize<S>(&self, _serializer: S) -> std::result::Result<S::Ok, S::Error>
             where S: serde::Serializer {
                 // Use the serializer's error type
                 Err(serde::ser::Error::custom("cannot serialize Unserializable"))
             }
        }
        // This should now correctly generate a serde_json::Error
        let serialization_err = serde_json::to_string(&Unserializable {}).unwrap_err();
        let err = VectorDBError::SerializationError(serialization_err.to_string());
        // Check the Display output using contains, as the exact serde_json error might vary slightly
        assert!(err.to_string().contains("cannot serialize Unserializable"));
        // Also check the error type prefix is correct
        assert!(err.to_string().starts_with("Error serializing or deserializing data:"));
    }

    #[test]
    fn test_display_deserialization_error() {
        let err = VectorDBError::DeserializationError("Failed to deserialize JSON data".to_string());
        assert_eq!(err.to_string(), "Error deserializing data: Failed to deserialize JSON data");
    }

    #[test]
    fn test_display_embedding_error() {
        let err = VectorDBError::EmbeddingError("Embedding generation failed".to_string());
        assert_eq!(err.to_string(), "Error generating embedding: Embedding generation failed");
    }

    #[test]
    fn test_display_database_error() {
        let err = VectorDBError::DatabaseError("DB connection failed".to_string());
        assert_eq!(err.to_string(), "Database error: DB connection failed");
    }

    #[test]
    fn test_display_ast_traversal_error() {
        let err = VectorDBError::ASTTraversalError("Could not traverse node".to_string());
        assert_eq!(err.to_string(), "AST traversal error: Could not traverse node");
    }

    #[test]
    fn test_display_invalid_parameter() {
        let err = VectorDBError::InvalidParameter("Negative count provided".to_string());
        assert_eq!(err.to_string(), "Invalid parameter: Negative count provided");
    }

    #[test]
    fn test_display_invalid_path() {
        let err = VectorDBError::InvalidPath("Path contains invalid chars".to_string());
        assert_eq!(err.to_string(), "Invalid path: Path contains invalid chars");
    }

    #[test]
    fn test_display_cache_error() {
        let err = VectorDBError::CacheError("Cache miss or invalidation".to_string());
        assert_eq!(err.to_string(), "Cache error: Cache miss or invalidation");
    }

    #[test]
    fn test_display_parser_error() {
        let err = VectorDBError::ParserError("Syntax error in code".to_string());
        assert_eq!(err.to_string(), "Parser error: Syntax error in code");
    }

    #[test]
    fn test_display_unsupported_language() {
        let err = VectorDBError::UnsupportedLanguage("Language 'Brainfuck' not supported".to_string());
        assert_eq!(err.to_string(), "Unsupported language: Language 'Brainfuck' not supported");
    }

    #[test]
    fn test_display_hnsw_error() {
        // Often wraps other errors, e.g., ONNX errors might end up here via From<anyhow::Error>
        let err = VectorDBError::HNSWError("HNSW search failed internally".to_string());
        assert_eq!(err.to_string(), "HNSW index error: HNSW search failed internally");
    }

    #[test]
    fn test_display_dimension_mismatch() {
        let err = VectorDBError::DimensionMismatch { expected: 768, found: 384 };
        assert_eq!(err.to_string(), "HNSW index dimension (768) does not match query/data dimension (384)");
    }

    #[test]
    fn test_display_io_error() {
        let io_err = io::Error::new(io::ErrorKind::TimedOut, "connection timed out");
        // Uses From<io::Error>
        let err = VectorDBError::from(io_err); // Or VectorDBError::IOError(io_err)
        assert_eq!(err.to_string(), "IO error: connection timed out");
    }

    #[test]
    fn test_display_code_analysis_error() {
        let err = VectorDBError::CodeAnalysisError("Failed to analyze symbols".to_string());
        assert_eq!(err.to_string(), "Code analysis error: Failed to analyze symbols");
    }

    #[test]
    fn test_display_general_error() {
        let err = VectorDBError::GeneralError("An unexpected issue occurred".to_string());
        assert_eq!(err.to_string(), "General error: An unexpected issue occurred");
    }

    #[test]
    fn test_display_directory_not_found() {
        let err = VectorDBError::DirectoryNotFound("/non/existent/path".to_string());
        assert_eq!(err.to_string(), "Directory not found: /non/existent/path");
    }

    #[test]
    fn test_display_repository_error() {
        let err = VectorDBError::RepositoryError("Git operation failed".to_string());
        assert_eq!(err.to_string(), "Repository error: Git operation failed");
    }

    #[test]
    fn test_display_repository_not_found() {
        let err = VectorDBError::RepositoryNotFound("Repo at path not found".to_string());
        assert_eq!(err.to_string(), "Repository not found: Repo at path not found");
    }

    #[test]
    fn test_display_search_error() {
        let err = VectorDBError::SearchError("Search query was invalid".to_string());
        assert_eq!(err.to_string(), "Search error: Search query was invalid");
    }

    #[test]
    fn test_display_other_error() {
        let err = VectorDBError::Other("Some other specific error".to_string());
        assert_eq!(err.to_string(), "Other error: Some other specific error");
    }

    #[test]
    fn test_display_configuration_error() {
        let err = VectorDBError::ConfigurationError("Missing API key".to_string());
        assert_eq!(err.to_string(), "Configuration error: Missing API key");
    }

    #[test]
    fn test_display_indexing_error() {
        let err = VectorDBError::IndexingError("Failed to add document to index".to_string());
        assert_eq!(err.to_string(), "Indexing error: Failed to add document to index");
    }

    #[test]
    fn test_display_directory_not_indexed() {
        let err = VectorDBError::DirectoryNotIndexed("src/utils".to_string());
        assert_eq!(err.to_string(), "Directory 'src/utils' is not present in the index");
    }

    #[test]
    fn test_display_index_not_found() {
        let err = VectorDBError::IndexNotFound;
        assert_eq!(err.to_string(), "Search index not found or not built");
    }

    #[test]
    fn test_display_operation_cancelled() {
        let err = VectorDBError::OperationCancelled;
        assert_eq!(err.to_string(), "Operation cancelled by user");
    }

    #[test]
    fn test_display_mutex_lock_error() {
        let err = VectorDBError::MutexLockError("Failed to acquire lock".to_string());
        assert_eq!(err.to_string(), "Mutex lock error: Failed to acquire lock");
    }

    // Example test for Clone - testing one variant is usually enough
    #[test]
    fn test_error_cloning() {
        let original = VectorDBError::FileNotFound("clone_test.txt".to_string());
        let cloned = original.clone();
        assert_eq!(original.to_string(), cloned.to_string());
        // Ensure it's a deep clone if necessary (though for String it doesn't matter much)
        if let VectorDBError::FileNotFound(s1) = original {
            if let VectorDBError::FileNotFound(s2) = cloned {
                assert_eq!(s1, s2);
                assert_ne!(s1.as_ptr(), s2.as_ptr()); // Check they aren't the same string instance in memory
            } else {
                panic!("Cloned error is not FileNotFound");
            }
        } else {
            panic!("Original error is not FileNotFound");
        }
    }

    // Example test for From<anyhow::Error>
    #[test]
    fn test_from_anyhow_error() {
        let anyhow_err = anyhow::anyhow!("Something failed in another library");
        let vectordb_err = VectorDBError::from(anyhow_err);
        // Check if it maps to HNSWError as defined in the From impl
        assert!(matches!(vectordb_err, VectorDBError::HNSWError(_)));
        assert!(vectordb_err.to_string().contains("HNSW index error: Something failed in another library"));
    }
}