ai_context_gen/
lib.rs

1//! # AI Context Generator
2//!
3//! A Rust library for generating structured context from code repositories,
4//! especially useful for LLMs and AI agents.
5//!
6//! ## Features
7//!
8//! - 🔍 **Complete Scanning**: Analyzes all `.rs` and `.md` files in the repository
9//! - 🌳 **Abstract Syntax Tree**: Extracts and documents structures, functions, enums and implementations
10//! - 📊 **Token Control**: Respects token limits and prioritizes important content
11//! - 📁 **Project Structure**: Generates file tree visualization
12//! - 📖 **Documentation**: Includes markdown files like README, documentation, etc.
13//! - ⚡ **Performance**: Asynchronous and optimized processing
14//!
15//! ## Usage Example
16//!
17//! ```rust
18//! use ai_context_gen::{Config, ContextGenerator, RepositoryScanner};
19//! use std::path::PathBuf;
20//!
21//! # async fn example() -> anyhow::Result<()> {
22//! let config = Config {
23//!     repo_path: PathBuf::from("."),
24//!     max_tokens: 50000,
25//!     output_file: "repo_context.md".to_string(),
26//!     include_hidden: false,
27//!     include_deps: false,
28//! };
29//!
30//! let scanner = RepositoryScanner::new(config.clone());
31//! let scan_result = scanner.scan().await?;
32//!
33//! let generator = ContextGenerator::new(config);
34//! generator.generate_context(scan_result).await?;
35//! # Ok(())
36//! # }
37//! ```
38
39use std::path::PathBuf;
40
41pub mod config;
42pub mod generator;
43pub mod parser;
44pub mod scanner;
45pub mod token_counter;
46
47// Re-export main structs for easier usage
48pub use config::Config;
49pub use generator::ContextGenerator;
50pub use parser::{EnumInfo, FunctionInfo, ImplInfo, RustAnalysis, RustParser, StructInfo};
51pub use scanner::{FileInfo, FileType, RepositoryScanner, ScanResult};
52pub use token_counter::{ContentPrioritizer, ContentSection, TokenCounter};
53
54/// Default Result type used by the library
55pub type Result<T> = anyhow::Result<T>;
56
57/// Generates repository context with default configuration
58///
59/// This is a convenience function that configures and executes
60/// the entire context generation process.
61///
62/// # Arguments
63///
64/// * `path` - Path to the repository
65/// * `output` - Output file name
66///
67/// # Example
68///
69/// ```rust
70/// use ai_context_gen::generate_context;
71/// use std::path::PathBuf;
72///
73/// # async fn example() -> anyhow::Result<()> {
74/// generate_context(PathBuf::from("."), "context.md".to_string()).await?;
75/// # Ok(())
76/// # }
77/// ```
78pub async fn generate_context(path: PathBuf, output: String) -> Result<()> {
79    let config = Config {
80        repo_path: path,
81        max_tokens: 50000,
82        output_file: output,
83        include_hidden: false,
84        include_deps: false,
85    };
86
87    let scanner = RepositoryScanner::new(config.clone());
88    let scan_result = scanner.scan().await?;
89
90    let generator = ContextGenerator::new(config);
91    generator.generate_context(scan_result).await?;
92
93    Ok(())
94}
95
96/// Generates repository context with custom configuration
97///
98/// # Arguments
99///
100/// * `config` - Custom configuration
101///
102/// # Example
103///
104/// ```rust
105/// use ai_context_gen::{Config, generate_context_with_config};
106/// use std::path::PathBuf;
107///
108/// # async fn example() -> anyhow::Result<()> {
109/// let config = Config {
110///     repo_path: PathBuf::from("./my-project"),
111///     max_tokens: 100000,
112///     output_file: "detailed_context.md".to_string(),
113///     include_hidden: true,
114///     include_deps: true,
115/// };
116///
117/// generate_context_with_config(config).await?;
118/// # Ok(())
119/// # }
120/// ```
121pub async fn generate_context_with_config(config: Config) -> Result<()> {
122    let scanner = RepositoryScanner::new(config.clone());
123    let scan_result = scanner.scan().await?;
124
125    let generator = ContextGenerator::new(config);
126    generator.generate_context(scan_result).await?;
127
128    Ok(())
129}