# Rfgrep API Reference
## Quick Start Guide
### Basic Usage
```rust
use rfgrep::app_simple::RfgrepApp;
use rfgrep::cli::Cli;
let app = RfgrepApp::new()?;
let cli = Cli::parse();
app.run(cli).await?;
```
## Core APIs
### 1. Application Management
#### `RfgrepApp`
```rust
// Create application instance
let app = RfgrepApp::new()?;
// Run with CLI arguments
app.run(cli).await?;
```
### 2. Streaming Search
#### `StreamingSearchPipeline`
```rust
use rfgrep::streaming_search::{StreamingConfig, StreamingSearchPipeline};
// Create configuration
let config = StreamingConfig {
algorithm: SearchAlgorithm::BoyerMoore,
context_lines: 3,
case_sensitive: true,
chunk_size: 8192,
buffer_size: 65536,
..Default::default()
};
// Create pipeline
let pipeline = StreamingSearchPipeline::new(config);
// Search single file
let matches = pipeline.search_file(&path, "pattern").await?;
// Search multiple files in parallel
let files = vec![path1, path2, path3];
let file_refs: Vec<&Path> = files.iter().map(|p| p.as_path()).collect();
let matches = pipeline.search_files_parallel(&file_refs, "pattern", 4).await?;
```
### 3. Plugin System
#### `EnhancedPluginManager`
```rust
use rfgrep::plugin_system::{EnhancedPluginManager, PluginRegistry};
// Create manager
let manager = Arc::new(EnhancedPluginManager::new());
// Create registry and load plugins
let registry = PluginRegistry::new(manager.clone());
registry.load_plugins().await?;
// Search using plugins
let matches = manager.search_file(&path, "pattern").await?;
// Get plugin information
let plugins = manager.list_plugins().await;
let stats = manager.get_plugin_stats().await;
```
#### Custom Plugin Implementation
```rust
use rfgrep::plugin_system::EnhancedSearchPlugin;
struct MyPlugin {
// Plugin state
}
impl EnhancedSearchPlugin for MyPlugin {
fn name(&self) -> &str { "my-plugin" }
fn version(&self) -> &str { "1.0.0" }
fn description(&self) -> &str { "My custom search plugin" }
fn can_handle(&self, file: &Path) -> bool {
file.extension().map_or(false, |ext| ext == "myext")
}
fn search(&self, file: &Path, pattern: &str) -> RfgrepResult<Vec<SearchMatch>> {
// Implementation
Ok(vec![])
}
// ... other required methods
}
```
### 4. TUI Interface
#### `TuiApp`
```rust
use rfgrep::tui::{TuiApp, init_terminal, restore_terminal};
// Initialize terminal
let mut terminal = init_terminal()?;
com (34.36.0.14): icmp_seq=1415 ttl=112 time=343 ms
64 bytes from 14.0.36.34.bc.googleusercontent.com (34.36.0.14): icmp_seq=1416 ttl=112 time=344 ms
64 bytes from 14.0.36.34.bc.googleusercontent.com (34.36.0.14): icmp_seq=1417 ttl=112 time=308 ms
64 bytes from 14.0.36.34.bc.googleusercontent.com (34.36.0.14): icmp_seq=1418 ttl=112 time=334 ms
64 bytes from 14.0.36.34.bc.googleusercontent.com (34.36.0.14): icmp_seq=1419 ttl=112 time=341 ms
64 bytes from 14.0.36.34.bc.googleusercontent.com (34.36.0.14): icmp_seq=1420 ttl=112 time=294 ms
64 bytes from 14.0.36.34.bc.googleusercontent.com (34.36.0.14): icmp_seq=1421 ttl=112 time=312 ms
64 bytes from 14.0.36.34.bc.googleusercontent.com (34.36.0.14): icmp_seq=1422 ttl=112 time=411 ms
64 bytes from 14.0.36.34.bc.googleusercontent.com (34.36.0.14): icmp_seq=1423 ttl=112 time=280 ms
64 bytes from 14.0.36.34.bc.googleusercontent.com (34.36.0.14): icmp_seq=1424 ttl=112 time=457 ms
64 bytes from 14.0.36.34.bc.googleusercontent.com (34.36.0.14): icmp_seq=1425 ttl=112 time=459 ms
64 bytes from 14.0.36.34.bc.googleusercontent.com (34.36.0.14): icmp_seq=1426 ttl=112 time=381 ms
64 bytes from 14.0.36.34.bc.googleusercontent.com (34.36.0.14): icmp_seq=1427 ttl=112 time=360 ms
64 bytes from 14.0.36.34.bc.googleusercontent.com (34.36.0.14): icmp_seq=1428 ttl=112 time=372 ms
64 bytes from 14.0.36.34.bc.googleusercontent.com (34.36.0.14): icmp_seq=1429 ttl=112 time=323 ms
64 bytes from 14.0.36.34.bc.googleusercontent.com (34.36.0.14): icmp_seq=1430 ttl=112 time=381 ms
64 bytes from 14.0.36.34.bc.googleusercontent.com
// Create TUI app
let mut app = TuiApp::new()?;
// Run TUI
app.run(&mut terminal).await?;
// Restore terminal
restore_terminal(&mut terminal)?;
```
### 5. Plugin CLI
#### `PluginCli`
```rust
use rfgrep::plugin_cli::PluginCli;
use rfgrep::cli::PluginCommands;
let plugin_cli = PluginCli::new(manager);
// List plugins
plugin_cli.handle_command(PluginCommands::List).await?;
// Show plugin info
plugin_cli.handle_command(PluginCommands::Info { name: "text".to_string() }).await?;
// Enable plugin
plugin_cli.handle_command(PluginCommands::Enable { name: "text".to_string() }).await?;
```
## Configuration Types
### `StreamingConfig`
```rust
pub struct StreamingConfig {
pub algorithm: SearchAlgorithm, // Search algorithm to use
pub context_lines: usize, // Number of context lines
pub case_sensitive: bool, // Case sensitivity
pub invert_match: bool, // Invert match results
pub max_matches: Option<usize>, // Maximum matches per file
pub timeout_per_file: Option<u64>, // Timeout per file (seconds)
pub chunk_size: usize, // Chunk size for processing
pub buffer_size: usize, // Buffer size for I/O
}
```
### `TuiState`
```rust
pub struct TuiState {
pub pattern: String, // Current search pattern
pub matches: Vec<SearchMatch>, // Search results
pub current_file_index: usize, // Currently selected file
pub current_match_index: usize, // Currently selected match
pub files: Vec<String>, // List of files
pub search_mode: SearchMode, // Text/Word/Regex
pub algorithm: SearchAlgorithm, // Search algorithm
pub case_sensitive: bool, // Case sensitivity
pub context_lines: usize, // Context lines
pub show_help: bool, // Show help panel
pub status_message: String, // Status message
pub search_in_progress: bool, // Search in progress
pub scroll_offset: usize, // Scroll offset
}
```
## Search Algorithms
### Available Algorithms
```rust
pub enum SearchAlgorithm {
Simple, // Simple string search
BoyerMoore, // Boyer-Moore algorithm
Regex, // Regular expressions
Simd, // SIMD-optimized search
}
```
### Algorithm Selection
```rust
// Create algorithm factory
let factory = SearchAlgorithmFactory::new();
// Create specific algorithm
let algorithm = factory.create(SearchAlgorithm::BoyerMoore, "pattern")?;
// Search with algorithm
let matches = algorithm.search(text, "pattern");
```
## Error Handling
### Error Types
```rust
pub enum RfgrepError {
Io(std::io::Error), // I/O errors
Regex(regex::Error), // Regex compilation errors
Other(String), // Other errors
PluginNotFound(String), // Plugin not found
PluginError(String), // Plugin-specific errors
ConfigurationError(String), // Configuration errors
}
```
### Result Type
```rust
pub type Result<T> = std::result::Result<T, RfgrepError>;
```
## Performance Tips
### Streaming Search
- Use appropriate chunk sizes (8KB-64KB)
- Enable parallel processing for multiple files
- Use SIMD algorithm for simple patterns
- Use Boyer-Moore for complex patterns
### Plugin System
- Implement `supports_streaming()` for better performance
- Use appropriate priority values (lower = higher priority)
- Cache plugin instances when possible
### TUI Interface
- Limit the number of displayed matches for better performance
- Use pagination for large result sets
- Implement efficient scrolling for large files
## Common Patterns
### Async Runtime Setup
```rust
use tokio::runtime::Runtime;
let rt = Runtime::new()?;
rt.block_on(async {
// Your async code here
})?;
```
### Plugin Registration
```rust
let plugin = Box::new(MyPlugin::new());
manager.register_plugin("my-plugin", plugin).await?;
```
### Configuration Loading
```rust
let config = StreamingConfig {
algorithm: SearchAlgorithm::BoyerMoore,
context_lines: 3,
case_sensitive: true,
..Default::default()
};
```
### Error Propagation
```rust
fn my_function() -> RfgrepResult<()> {
let result = some_operation()?; // Propagates errors
Ok(())
}
```
## Integration Examples
### CLI Integration
```rust
use rfgrep::cli::{Cli, Commands};
let cli = Cli::parse();
match cli.command {
Commands::Search { pattern, .. } => {
// Handle search command
}
Commands::Plugins { command } => {
// Handle plugin command
}
Commands::Tui { .. } => {
// Handle TUI command
}
}
```
### File Processing
```rust
use rfgrep::walker::walk_dir;
let entries: Vec<_> = walk_dir(&path, true, false).collect();
for entry in entries {
if entry.path().is_file() {
// Process file
}
}
```
### Binary Detection
```rust
use rfgrep::processor::is_binary;
if is_binary(&path) {
// Skip binary files
continue;
}
```