ai_context_gen/
lib.rs

1//! # AI Context Generator - Rust Library
2//!
3//! A Rust library for generating structured context from code repositories,
4//! specifically designed for LLMs and AI agents. This library provides both
5//! simple convenience functions and advanced APIs for fine-grained control.
6//!
7//! ## When to Use This Library
8//!
9//! - **Integrate context generation into your Rust applications**
10//! - **Build custom analysis workflows**
11//! - **Create automated documentation systems**
12//! - **Develop AI-powered developer tools**
13//!
14//! For standalone command-line usage, consider using the CLI tool instead.
15//!
16//! ## Features
17//!
18//! - 🔍 **Complete Scanning**: Analyzes all `.rs` and `.md` files in repositories
19//! - 🌳 **AST Analysis**: Extracts structures, functions, enums and implementations
20//! - 📊 **Token Control**: Respects limits and prioritizes important content
21//! - 📁 **Project Structure**: Generates file tree visualizations
22//! - 📖 **Documentation**: Includes markdown files and code documentation
23//! - ⚡ **Async Processing**: Non-blocking, high-performance analysis
24//!
25//! ## Quick Start
26//!
27//! ### Simple Usage
28//!
29//! ```rust
30//! use ai_context_gen::generate_context;
31//! use std::path::PathBuf;
32//!
33//! #[tokio::main]
34//! async fn main() -> anyhow::Result<()> {
35//!     // Generate context for current directory
36//!     generate_context(PathBuf::from("."), "context.md".to_string()).await?;
37//!     println!("Context generated successfully!");
38//!     Ok(())
39//! }
40//! ```
41//!
42//! ### Advanced Usage with Configuration
43//!
44//! ```rust,no_run
45//! use ai_context_gen::{Config, ContextGenerator, RepositoryScanner};
46//! use std::path::PathBuf;
47//!
48//! #[tokio::main]
49//! async fn main() -> anyhow::Result<()> {
50//!     // Custom configuration
51//!     let config = Config {
52//!         repo_path: PathBuf::from("./my-project"),
53//!         max_tokens: 100000,
54//!         output_file: "detailed_context.md".to_string(),
55//!         include_hidden: true,
56//!         include_deps: true,
57//!     };
58//!
59//!     // Step-by-step process for more control
60//!     let scanner = RepositoryScanner::new(config.clone());
61//!     let scan_result = scanner.scan().await?;
62//!     
63//!     println!("Found {} files", scan_result.files.len());
64//!     
65//!     let generator = ContextGenerator::new(config);
66//!     generator.generate_context(scan_result).await?;
67//!     
68//!     Ok(())
69//! }
70//! ```
71//!
72//! ### Using the Configuration Function
73//!
74//! ```rust,no_run
75//! use ai_context_gen::{Config, generate_context_with_config};
76//! use std::path::PathBuf;
77//!
78//! #[tokio::main]
79//! async fn main() -> anyhow::Result<()> {
80//!     let config = Config {
81//!         repo_path: PathBuf::from("/path/to/analyze"),
82//!         max_tokens: 75000,
83//!         output_file: "analysis.md".to_string(),
84//!         include_hidden: false,
85//!         include_deps: true,
86//!     };
87//!
88//!     generate_context_with_config(config).await?;
89//!     Ok(())
90//! }
91//! ```
92//!
93//! ## API Overview
94//!
95//! - [`generate_context`]: Simple function for basic use cases
96//! - [`generate_context_with_config`]: Function with custom configuration
97//! - [`Config`]: Configuration structure for all options
98//! - [`RepositoryScanner`]: File scanning and discovery
99//! - [`ContextGenerator`]: Context generation with priorities
100//! - [`RustParser`]: Rust code AST analysis
101//!
102//! ## Integration Patterns
103//!
104//! ### Web Applications
105//!
106//! ```rust,no_run
107//! use ai_context_gen::{Config, generate_context_with_config};
108//! use std::path::PathBuf;
109//!
110//! async fn analyze_repo_endpoint(repo_path: String) -> Result<String, Box<dyn std::error::Error>> {
111//!     let config = Config {
112//!         repo_path: PathBuf::from(repo_path),
113//!         max_tokens: 50000,
114//!         output_file: format!("/tmp/analysis_{}.md", chrono::Utc::now().timestamp()),
115//!         include_hidden: false,
116//!         include_deps: false,
117//!     };
118//!     
119//!     generate_context_with_config(config.clone()).await?;
120//!     Ok(config.output_file)
121//! }
122//! ```
123//!
124//! ### Custom Workflows
125//!
126//! ```rust,no_run
127//! use ai_context_gen::{Config, RepositoryScanner, ContextGenerator};
128//! use std::path::PathBuf;
129//!
130//! async fn custom_analysis_workflow(repo_path: PathBuf) -> anyhow::Result<()> {
131//!     let config = Config {
132//!         repo_path: repo_path.clone(),
133//!         max_tokens: 100000,
134//!         output_file: "temp_analysis.md".to_string(),
135//!         include_hidden: true,
136//!         include_deps: true,
137//!     };
138//!
139//!     // Scan first
140//!     let scanner = RepositoryScanner::new(config.clone());
141//!     let scan_result = scanner.scan().await?;
142//!     
143//!     // Custom filtering or processing here
144//!     println!("Found {} Rust files", scan_result.files.iter()
145//!         .filter(|f| matches!(f.file_type, ai_context_gen::FileType::Rust))
146//!         .count());
147//!     
148//!     // Generate context
149//!     let generator = ContextGenerator::new(config);
150//!     generator.generate_context(scan_result).await?;
151//!     
152//!     Ok(())
153//! }
154//! ```
155
156use std::path::PathBuf;
157
158pub mod config;
159pub mod generator;
160pub mod parser;
161pub mod scanner;
162pub mod token_counter;
163
164// Re-export main structs for easier usage
165pub use config::Config;
166pub use generator::ContextGenerator;
167pub use parser::{EnumInfo, FunctionInfo, ImplInfo, RustAnalysis, RustParser, StructInfo};
168pub use scanner::{FileInfo, FileType, RepositoryScanner, ScanResult};
169pub use token_counter::{ContentPrioritizer, ContentSection, TokenCounter};
170
171/// Default Result type used by the library
172pub type Result<T> = anyhow::Result<T>;
173
174/// Generates repository context with default configuration
175///
176/// This is a convenience function that configures and executes
177/// the entire context generation process.
178///
179/// # Arguments
180///
181/// * `path` - Path to the repository
182/// * `output` - Output file name
183///
184/// # Example
185///
186/// ```rust
187/// use ai_context_gen::generate_context;
188/// use std::path::PathBuf;
189///
190/// # async fn example() -> anyhow::Result<()> {
191/// generate_context(PathBuf::from("."), "context.md".to_string()).await?;
192/// # Ok(())
193/// # }
194/// ```
195pub async fn generate_context(path: PathBuf, output: String) -> Result<()> {
196    let config = Config {
197        repo_path: path,
198        max_tokens: 50000,
199        output_file: output,
200        include_hidden: false,
201        include_deps: false,
202    };
203
204    let scanner = RepositoryScanner::new(config.clone());
205    let scan_result = scanner.scan().await?;
206
207    let generator = ContextGenerator::new(config);
208    generator.generate_context(scan_result).await?;
209
210    Ok(())
211}
212
213/// Generates repository context with custom configuration
214///
215/// # Arguments
216///
217/// * `config` - Custom configuration
218///
219/// # Example
220///
221/// ```rust
222/// use ai_context_gen::{Config, generate_context_with_config};
223/// use std::path::PathBuf;
224///
225/// # async fn example() -> anyhow::Result<()> {
226/// let config = Config {
227///     repo_path: PathBuf::from("./my-project"),
228///     max_tokens: 100000,
229///     output_file: "detailed_context.md".to_string(),
230///     include_hidden: true,
231///     include_deps: true,
232/// };
233///
234/// generate_context_with_config(config).await?;
235/// # Ok(())
236/// # }
237/// ```
238pub async fn generate_context_with_config(config: Config) -> Result<()> {
239    let scanner = RepositoryScanner::new(config.clone());
240    let scan_result = scanner.scan().await?;
241
242    let generator = ContextGenerator::new(config);
243    generator.generate_context(scan_result).await?;
244
245    Ok(())
246}