use codesearch::errors::{AnalysisError, SearchError};
use codesearch::search::{DefaultSearchEngine, search_code};
use codesearch::traits::SearchEngine;
use codesearch::types::SearchOptions;
use std::path::{Path, PathBuf};
fn example_search_with_error_handling() -> Result<(), SearchError> {
let path = Path::new("src");
let options = SearchOptions::default();
match search_code("test", path, &options) {
Ok(results) => {
println!("Found {} results", results.len());
Ok(())
}
Err(e) => {
eprintln!("Search failed: {e}");
Err(SearchError::InvalidOptions {
message: e.to_string(),
})
}
}
}
fn example_trait_based_search() -> Result<(), SearchError> {
let engine: Box<dyn SearchEngine> = Box::new(DefaultSearchEngine::new());
let options = SearchOptions::default();
match engine.search("fn main", Path::new("src"), &options) {
Ok(results) => {
println!("Found {} matches", results.len());
Ok(())
}
Err(e) => {
eprintln!("Search error: {e}");
Err(SearchError::InvalidOptions {
message: e.to_string(),
})
}
}
}
fn example_specific_errors() {
let err = SearchError::FileNotFound {
path: PathBuf::from("/nonexistent/file.rs"),
};
println!("Error: {err}");
let regex_err = regex::Regex::new("[").unwrap_err();
let err = SearchError::InvalidPattern {
pattern: "[".to_string(),
source: regex_err,
};
println!("Error: {err}");
if let Some(source) = std::error::Error::source(&err) {
println!("Caused by: {source}");
}
let err = AnalysisError::UnsupportedFileType {
extension: "xyz".to_string(),
};
println!("Error: {err}");
}
fn example_with_context() -> Result<(), Box<dyn std::error::Error>> {
let path = Path::new("src");
let options = SearchOptions::default();
search_code("test", path, &options)?;
Ok(())
}
fn example_error_conversion() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let search_err: SearchError = io_err.into();
println!("Converted error: {search_err}");
let regex_err = regex::Regex::new("(").unwrap_err();
let search_err: SearchError = regex_err.into();
println!("Converted error: {search_err}");
}
fn main() {
println!("=== Error Handling Examples ===\n");
println!("Example 1: Search with error handling");
if let Err(e) = example_search_with_error_handling() {
eprintln!("Error: {e}");
}
println!("\nExample 2: Trait-based search");
if let Err(e) = example_trait_based_search() {
eprintln!("Error: {e}");
}
println!("\nExample 3: Specific error types");
example_specific_errors();
println!("\nExample 4: Error context with anyhow");
if let Err(e) = example_with_context() {
eprintln!("Error: {e:?}");
}
println!("\nExample 5: Error conversion");
example_error_conversion();
}