Expand description
§DepBank
DepBank is a Rust library for generating AI-friendly code banks from your project’s dependencies. It helps AI tools better understand your code by creating comprehensive documentation of the dependencies you’re using.
§Key features
- Finding all Cargo.toml files in a project hierarchy
- Extracting dependency information from Cargo.toml files
- Resolving exact dependency versions using Cargo.lock
- Generating code banks for dependencies
- Calculating token counts for generated documentation
§Core concepts
- Dependency discovery: Finds dependencies in your Cargo.toml files
- Version resolution: Maps version requirements to exact versions using Cargo.lock
- Registry path resolution: Locates dependencies in the local Cargo registry
- Code bank generation: Creates documentation for each dependency
- Token calculation: Measures LLM token usage for generated documentation
§Usage examples
§Generate code banks for dependencies
use anyhow::Result;
use depbank::{
extract_dependency_info, find_cargo_lock, find_cargo_toml_files,
generate_all_code_banks, is_dependency_available, resolve_dependency_versions,
resolve_registry_path,
};
use std::path::{Path, PathBuf};
fn generate_docs(project_path: &Path, output_dir: &Path) -> Result<()> {
// Find Cargo.toml files
let cargo_toml_files = find_cargo_toml_files(project_path)?;
// Extract dependency information
let first_cargo_toml = &cargo_toml_files[0];
let dependency_info = extract_dependency_info(first_cargo_toml)?;
// Resolve exact versions from Cargo.lock
let cargo_lock_path = find_cargo_lock(project_path)?;
let resolved_versions = resolve_dependency_versions(cargo_lock_path, &dependency_info)?;
// Resolve registry path
let registry_path = resolve_registry_path()?;
// Generate code banks
let code_bank_files = generate_all_code_banks(&resolved_versions, ®istry_path, output_dir)?;
println!("Generated {} code bank files", code_bank_files.len());
Ok(())
}
§Calculate tokens for files
use depbank::{calculate_directory_tokens, calculate_file_tokens};
use std::path::Path;
fn count_tokens(path: &Path) -> anyhow::Result<()> {
if path.is_file() {
// Count tokens for a single file
let token_count = calculate_file_tokens(path)?;
println!("{}: {} tokens", path.display(), token_count);
} else if path.is_dir() {
// Count tokens for a directory
let file_stats = calculate_directory_tokens(path, Some("md"))?;
println!("Total files: {}", file_stats.len());
// Calculate total tokens
let total_tokens: usize = file_stats.values().map(|stat| stat.token_count).sum();
println!("Total tokens: {}", total_tokens);
}
Ok(())
}
Structs§
- Dependency
- A dependency with its name and version
- Dependency
Collection - A collection of dependencies with helper methods
- File
Stats - Represents file statistics including token count.
Functions§
- calculate_
directory_ tokens - Calculates tokens for all files in a directory.
- calculate_
file_ tokens - Calculates tokens for a file.
- calculate_
tokens - Calculates the number of tokens in a text.
- collect_
dependencies - Collects all dependencies from found Cargo.toml files into a HashSet.
- construct_
dependency_ path - Constructs the full path to a dependency’s source code.
- extract_
dependency_ info - Extracts dependency information from a single Cargo.toml file.
- find_
cargo_ lock - Finds the Cargo.lock file in the workspace.
- find_
cargo_ toml_ files - Recursively finds all Cargo.toml files in the given directory.
- generate_
all_ code_ banks - Generates code banks for all available dependencies.
- generate_
code_ bank - Generates code bank for a dependency.
- is_
dependency_ available - Checks if a dependency is available locally in the cargo registry.
- is_
dependency_ available_ by_ parts - Overloaded version of is_dependency_available that takes separate name and version strings.
- resolve_
dependency_ paths - Resolves the paths for all dependencies.
- resolve_
dependency_ versions - Resolves exact dependency versions from Cargo.lock file.
- resolve_
registry_ path - Resolves the path to the Cargo registry directory.