codebank 0.4.5

A powerful code documentation generator that creates structured markdown documentation from your codebase. Supports multiple languages including Rust, Python, TypeScript, C, and Go with intelligent parsing and formatting. Features test code filtering, summary generation, and customizable documentation strategies.
Documentation
# CODE BANK IMPLEMENTATION PLAN

## Requirements Analysis
- Core Requirements:
  - [ ] Generate markdown files from source code with proper formatting
  - [ ] Support multiple languages (Rust, Python, TypeScript, C)
  - [ ] Provide different output strategies (full code, public interfaces only)
  - [ ] Create a usable command-line interface
- Technical Constraints:
  - [ ] Use tree-sitter for language parsing
  - [ ] Support extensibility for additional languages
  - [ ] Ensure proper error handling
  - [ ] Follow Rust best practices for code organization

## Component Analysis
- Core Framework:
  - Changes needed: Implement the Bank trait
  - Dependencies: File system access, language parsers
- Language Parsers:
  - Changes needed: Complete implementation for each language
  - Dependencies: tree-sitter grammars
- CLI Interface:
  - Changes needed: Build command-line argument handling
  - Dependencies: clap library

## Design Decisions
- Architecture:
  - [ ] Use trait-based design for extensibility
  - [ ] Maintain language-agnostic intermediate representation
  - [ ] Implement strategy pattern for output generation
- Algorithms:
  - [ ] Efficient directory traversal algorithm
  - [ ] Tree-sitter query optimization for parsing
  - [ ] Markdown generation with proper indentation

## Implementation Strategy
1. Phase 1: Core Implementation
   - [ ] Implement Rust parser (highest priority)
   - [ ] Implement Bank trait for code generation
   - [ ] Build basic CLI interface

2. Phase 2: Additional Languages
   - [ ] Implement Python parser
   - [ ] Implement TypeScript parser
   - [ ] Implement C parser

3. Phase 3: Refinement
   - [ ] Add configuration options
   - [ ] Create examples and documentation
   - [ ] Implement testing

## Testing Strategy
- Unit Tests:
  - [ ] Test language parsers with sample files
  - [ ] Test markdown generation
  - [ ] Test error handling
- Integration Tests:
  - [ ] End-to-end tests for CLI
  - [ ] Multi-language project tests
  - [ ] Edge case handling

## Documentation Plan
- [ ] Complete README with usage examples
- [ ] API documentation for library users
- [ ] Example projects demonstrating features

## Detailed Implementation Guide

### Rust Parser Implementation
1. Tree-sitter Grammar Integration:
   ```rust
   let mut parser = tree_sitter::Parser::new();
   parser.set_language(tree_sitter_rust::language())?;
   ```

2. Query Patterns for Rust Syntax:
   ```rust
   const FUNCTION_QUERY: &str = r#"
   (function_item
     name: (identifier) @function_name
     parameters: (parameters) @params
     return_type: (type_identifier)? @return_type
     body: (block) @body
   ) @function
   "#;
   ```

3. AST Traversal:
   ```rust
   fn traverse_node(&self, node: Node, source: &str) -> Result<Vec<CodeUnit>> {
       // Implementation based on node type
   }
   ```

4. Extract Public Interfaces:
   ```rust
   if node.kind() == "visibility_modifier" && node.utf8_text(source_code.as_bytes())? == "pub" {
       // Handle public item
   }
   ```

### Bank Implementation
1. Directory Traversal:
   ```rust
   use walkdir::WalkDir;

   fn traverse_directory(&self, dir: &Path) -> Result<Vec<FileUnit>> {
       for entry in WalkDir::new(dir).follow_links(true).into_iter().filter_map(|e| e.ok()) {
           if entry.file_type().is_file() {
               // Process file
           }
       }
   }
   ```

2. File Extension Detection:
   ```rust
   fn get_language(&self, path: &Path) -> Option<LanguageType> {
       match path.extension().and_then(OsStr::to_str) {
           Some("rs") => Some(LanguageType::Rust),
           Some("py") => Some(LanguageType::Python),
           // other cases
           _ => None,
       }
   }
   ```

3. Markdown Generation:
   ```rust
   fn generate_markdown(&self, file_units: Vec<FileUnit>, strategy: BankStrategy) -> String {
       let mut output = String::new();
       // Format header
       output.push_str("# Code Bank\n\n");

       for file in file_units {
           // Format file section
           output.push_str(&format!("## {}\n", file.path.display()));

           // Determine language for code block
           let lang = self.get_language_name(&file.path);
           output.push_str(&format!("```{}\n", lang));

           // Add code based on strategy
           match strategy {
               BankStrategy::Default => { /* full code */ },
               BankStrategy::Summary => { /* public interfaces only */ }
               // other cases
           }

           output.push_str("```\n\n");
       }

       output
   }
   ```

### CLI Implementation
1. Command-line Arguments:
   ```rust
   use clap::{Parser, ValueEnum};

   #[derive(Parser, Debug)]
   #[clap(author, version, about)]
   struct Args {
       /// Input directory path
       #[clap(short, long)]
       input: PathBuf,

       /// Output file path
       #[clap(short, long)]
       output: Option<PathBuf>,

       /// Output strategy
       #[clap(short, long, value_enum, default_value_t = OutputStrategy::Default)]
       strategy: OutputStrategy,
   }

   #[derive(Copy, Clone, Debug, ValueEnum)]
   enum OutputStrategy {
       Default,
       NoTests,
       Summary,
   }
   ```

2. Main Implementation:
   ```rust
   fn main() -> Result<()> {
       let args = Args::parse();

       // Convert output strategy
       let strategy = match args.strategy {
           OutputStrategy::Default => BankStrategy::Default,
           OutputStrategy::NoTests => BankStrategy::NoTests,
           OutputStrategy::Summary => BankStrategy::Summary,
       };

       // Create and use bank implementation
       let bank = CodeBank::new();
       let content = bank.generate(&args.input, strategy)?;

       // Output result
       if let Some(output_path) = args.output {
           fs::write(&output_path, content)?;
           println!("Code bank written to {}", output_path.display());
       } else {
           println!("{}", content);
       }

       Ok(())
   }
   ```

## Timeline and Milestones
1. Week 1: Rust parser implementation
2. Week 2: Bank trait implementation and basic CLI
3. Week 3: Additional language parsers
4. Week 4: Refinement, configuration, and documentation

## Creative Phase Components
- Architecture Design: Need to finalize the Bank implementation structure and the parser coordination
- Algorithm Design: Optimize tree-sitter queries for efficient parsing