Skip to main content

codesearch/search/
engine.rs

1//! Default SearchEngine Implementation
2//!
3//! Provides the default implementation of the SearchEngine trait using the existing search_code logic.
4
5use crate::search::core::search_code as search_code_impl;
6use crate::traits::SearchEngine;
7use crate::types::{SearchOptions, SearchResult};
8use std::path::Path;
9
10/// Default search engine implementation
11///
12/// This wraps the existing search_code function to implement the SearchEngine trait.
13/// It provides the standard parallel search with fuzzy matching, caching, and semantic search.
14///
15/// # Examples
16///
17/// ```
18/// use codesearch::search::DefaultSearchEngine;
19/// use codesearch::traits::SearchEngine;
20/// use codesearch::types::SearchOptions;
21/// use std::path::Path;
22///
23/// let engine = DefaultSearchEngine::new();
24/// let options = SearchOptions::default();
25/// let results = engine.search("fn main", Path::new("src"), &options);
26/// ```
27#[derive(Debug, Clone, Default)]
28pub struct DefaultSearchEngine;
29
30impl DefaultSearchEngine {
31    /// Create a new default search engine
32    pub fn new() -> Self {
33        Self
34    }
35}
36
37impl SearchEngine for DefaultSearchEngine {
38    fn search(
39        &self,
40        query: &str,
41        path: &Path,
42        options: &SearchOptions,
43    ) -> Result<Vec<SearchResult>, Box<dyn std::error::Error>> {
44        search_code_impl(query, path, options)
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51    use std::fs;
52    use tempfile::tempdir;
53
54    #[test]
55    fn test_default_search_engine() {
56        let engine = DefaultSearchEngine::new();
57        let dir = tempdir().unwrap();
58        fs::write(dir.path().join("test.rs"), "fn test() {}").unwrap();
59
60        let options = SearchOptions {
61            extensions: Some(vec!["rs".to_string()]),
62            ignore_case: true,
63            ..Default::default()
64        };
65
66        let result = engine.search("test", dir.path(), &options);
67        assert!(result.is_ok());
68        let results = result.unwrap();
69        assert!(!results.is_empty());
70    }
71
72    #[test]
73    fn test_search_engine_trait_object() {
74        let engine: Box<dyn SearchEngine> = Box::new(DefaultSearchEngine::new());
75        let dir = tempdir().unwrap();
76        fs::write(dir.path().join("test.rs"), "fn main() {}").unwrap();
77
78        let options = SearchOptions::default();
79        let result = engine.search("main", dir.path(), &options);
80        assert!(result.is_ok());
81    }
82}