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