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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
pub mod formatter;
mod lang;
mod units;

use crate::Result;
use std::path::{Path, PathBuf};

pub use formatter::Formatter;
pub use lang::{CppParser, GoParser, PythonParser, RustParser, TypeScriptParser};

/// Represents visibility levels for code elements.
///
/// This enum is used to track the visibility of various code elements
/// such as functions, structs, and modules.
///
/// # Examples
///
/// ```
/// use codebank::Visibility;
///
/// // Public visibility
/// let vis = Visibility::Public;
/// assert!(matches!(vis, Visibility::Public));
///
/// // Private visibility
/// let vis = Visibility::Private;
/// assert!(matches!(vis, Visibility::Private));
///
/// // Crate visibility
/// let vis = Visibility::Crate;
/// assert!(matches!(vis, Visibility::Crate));
///
/// // Restricted visibility
/// let vis = Visibility::Restricted("super::module".to_string());
/// assert!(matches!(vis, Visibility::Restricted(_)));
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum Visibility {
    /// Public visibility (accessible from outside the module)
    #[default]
    Public,

    /// Private visibility (accessible only within the module)
    Private,

    /// Protected visibility (accessible within the module and its descendants)
    Protected,

    /// Crate visibility (accessible within the crate only)
    Crate,

    /// Visibility restricted to a specific path
    Restricted(String),
}

/// The language type supported by the parser.
///
/// # Examples
///
/// ```
/// use codebank::LanguageType;
///
/// // Check Rust files
/// assert!(matches!(LanguageType::Rust, LanguageType::Rust));
///
/// // Check Python files
/// assert!(matches!(LanguageType::Python, LanguageType::Python));
///
/// // Check TypeScript files
/// assert!(matches!(LanguageType::TypeScript, LanguageType::TypeScript));
///
/// // Check C files
/// assert!(matches!(LanguageType::Cpp, LanguageType::Cpp));
///
/// // Check Go files
/// assert!(matches!(LanguageType::Go, LanguageType::Go));
///
/// // Handle unknown types
/// assert!(matches!(LanguageType::Unknown, LanguageType::Unknown));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LanguageType {
    /// Rust language
    Rust,
    /// Python language
    Python,
    /// TypeScript language
    TypeScript,
    /// C/C++ language
    Cpp,
    /// Go language
    Go,
    /// Unknown language (used for unsupported extensions)
    Unknown,
}

/// Trait for language-specific parsers.
///
/// This trait is implemented by parsers for different programming languages
/// to provide consistent parsing behavior.
///
/// # Examples
///
/// ```
/// use codebank::{LanguageParser, FileUnit, Result};
/// use std::path::{Path, PathBuf};
///
/// struct MyParser;
///
/// impl LanguageParser for MyParser {
///     fn parse_file(&mut self, file_path: &Path) -> Result<FileUnit> {
///         // Simple implementation that creates an empty FileUnit
///         Ok(FileUnit::new(file_path.to_path_buf()))
///     }
/// }
///
/// # fn main() -> Result<()> {
/// let mut parser = MyParser;
/// let file_unit = parser.parse_file(Path::new("example.rs"))?;
/// assert_eq!(file_unit.path, PathBuf::from("example.rs"));
/// # Ok(())
/// # }
/// ```
pub trait LanguageParser {
    /// Parse a file into a FileUnit
    fn parse_file(&mut self, file_path: &Path) -> Result<FileUnit>;
}

/// Represents a file in the code.
///
/// This struct contains all the parsed information about a source code file,
/// including its structure, contents, and metadata.
///
/// # Examples
///
/// ```
/// use codebank::{FileUnit, Visibility, FunctionUnit};
/// use std::path::PathBuf;
///
/// // Create a new file unit
/// let mut file = FileUnit::new(PathBuf::from("example.rs"));
///
/// // Add documentation
/// file.doc = Some("Example file documentation".to_string());
///
/// // Add a function
/// let function = FunctionUnit {
///     name: "example_function".to_string(),
///     visibility: Visibility::Public,
///     doc: Some("Function documentation".to_string()),
///     signature: Some("fn example_function()".to_string()),
///     body: Some("{ println!(\"Hello\"); }".to_string()),
///     source: Some("fn example_function() { println!(\"Hello\"); }".to_string()),
///     attributes: vec![],
/// };
/// file.functions.push(function);
///
/// assert_eq!(file.path, PathBuf::from("example.rs"));
/// assert!(file.doc.is_some());
/// assert!(!file.functions.is_empty());
/// ```
#[derive(Debug, Default)]
pub struct FileUnit {
    /// The path to the file
    pub path: PathBuf,

    /// File-level documentation
    pub doc: Option<String>,

    /// The declares in the file, e.g. imports, use statements, mod statements, c includes, python/js imports, etc.
    pub declares: Vec<DeclareStatements>,

    /// The modules contained in the file
    pub modules: Vec<ModuleUnit>,

    /// Top-level functions not in a module
    pub functions: Vec<FunctionUnit>,

    /// Top-level structs not in a module
    pub structs: Vec<StructUnit>,

    /// Top-level traits not in a module
    pub traits: Vec<TraitUnit>,

    /// Top-level implementation blocks
    pub impls: Vec<ImplUnit>,

    /// Source code of the entire file
    pub source: Option<String>,
}

/// Represents declarations in source code.
///
/// This struct is used to store various types of declarations found in source files,
/// such as imports, use statements, and module declarations.
///
/// # Examples
///
/// ```
/// use codebank::{DeclareStatements, DeclareKind};
///
/// // Create an import declaration
/// let import = DeclareStatements {
///     source: "use std::io;".to_string(),
///     kind: DeclareKind::Import,
/// };
/// assert!(matches!(import.kind, DeclareKind::Import));
///
/// // Create a module declaration
/// let module = DeclareStatements {
///     source: "mod example;".to_string(),
///     kind: DeclareKind::Mod,
/// };
/// assert!(matches!(module.kind, DeclareKind::Mod));
/// ```
#[derive(Debug, Default)]
pub struct DeclareStatements {
    /// The source code of the declaration
    pub source: String,
    /// The kind of declaration
    pub kind: DeclareKind,
}

/// The kind of declaration statement.
///
/// # Examples
///
/// ```
/// use codebank::DeclareKind;
///
/// // Import declaration
/// let kind = DeclareKind::Import;
/// assert!(matches!(kind, DeclareKind::Import));
///
/// // Use declaration
/// let kind = DeclareKind::Use;
/// assert!(matches!(kind, DeclareKind::Use));
///
/// // Module declaration
/// let kind = DeclareKind::Mod;
/// assert!(matches!(kind, DeclareKind::Mod));
///
/// // Other declaration types
/// let kind = DeclareKind::Other("macro_rules".to_string());
/// assert!(matches!(kind, DeclareKind::Other(_)));
/// ```
#[derive(Debug, Default, PartialEq)]
pub enum DeclareKind {
    #[default]
    Import,
    Use,
    Mod,
    Other(String),
}

/// Represents a module in the code
#[derive(Debug, Default)]
pub struct ModuleUnit {
    /// The name of the module
    pub name: String,

    /// Attributes applied to the module
    pub attributes: Vec<String>,

    /// The document for the module
    pub doc: Option<String>,

    /// The declares in the module, e.g. imports, use statements, mod statements, c includes, python/js imports, etc.
    pub declares: Vec<DeclareStatements>,

    /// The visibility of the module
    pub visibility: Visibility,

    /// Functions defined in the module
    pub functions: Vec<FunctionUnit>,

    /// Structs defined in the module
    pub structs: Vec<StructUnit>,

    /// Traits defined in the module
    pub traits: Vec<TraitUnit>,

    /// Implementation blocks defined in the module
    pub impls: Vec<ImplUnit>,

    /// Sub-modules defined in the module
    pub submodules: Vec<ModuleUnit>,

    /// Source code of the module declaration
    pub source: Option<String>,
}

/// Represents a function or method in the code
#[derive(Debug, Default, Clone)]
pub struct FunctionUnit {
    /// The name of the function
    pub name: String,

    /// Attributes applied to the function
    pub attributes: Vec<String>,

    /// The visibility of the function
    pub visibility: Visibility,

    /// The documentation for the function
    pub doc: Option<String>,

    /// The function signature (without body)
    pub signature: Option<String>,

    /// The function body
    pub body: Option<String>,

    /// The source code of the function
    pub source: Option<String>,
}

/// Represents a struct or class in the code
#[derive(Debug, Default)]
pub struct StructUnit {
    /// The name of the struct
    pub name: String,

    /// Attributes applied to the struct
    pub attributes: Vec<String>,

    /// The visibility of the struct
    pub visibility: Visibility,

    /// The documentation for the struct
    pub doc: Option<String>,

    /// struct head, e.g. struct Type, class Type, etc.
    pub head: String,

    /// The fields of the struct
    pub fields: Vec<FieldUnit>,

    /// The methods implemented for the struct
    pub methods: Vec<FunctionUnit>,

    /// The source code of the struct
    pub source: Option<String>,
}

/// Represents a field in a struct
#[derive(Debug, Default, Clone)]
pub struct FieldUnit {
    /// The name of the field
    pub name: String,
    /// documentation for the field
    pub doc: Option<String>,
    /// attributes applied to the field
    pub attributes: Vec<String>,
    /// the source code of the field
    pub source: Option<String>,
}

/// Represents a trait or interface in the code
#[derive(Debug, Default, Clone)]
pub struct TraitUnit {
    /// The name of the trait
    pub name: String,

    /// Attributes applied to the struct
    pub attributes: Vec<String>,

    /// The visibility of the trait
    pub visibility: Visibility,

    /// The documentation for the trait
    pub doc: Option<String>,

    /// The methods declared in the trait
    pub methods: Vec<FunctionUnit>,

    /// The source code of the trait
    pub source: Option<String>,
}

/// Represents an implementation block in the code, not all languages need this
#[derive(Debug, Default, Clone)]
pub struct ImplUnit {
    /// Attributes applied to the trait
    pub attributes: Vec<String>,

    /// The documentation for the implementation block
    pub doc: Option<String>,

    /// impl head, e.g. impl Trait for Type or impl Type
    pub head: String,

    /// The methods implemented in this block
    pub methods: Vec<FunctionUnit>,

    /// The source code of the implementation block
    pub source: Option<String>,
}

impl Visibility {
    pub fn as_str(&self, language: LanguageType) -> &str {
        match (self, language) {
            (Visibility::Public, LanguageType::Rust) => "pub",
            (Visibility::Crate, LanguageType::Rust) => "pub(crate)",
            (_, LanguageType::Rust) => "",
            (_, LanguageType::Python) => "",
            (_, LanguageType::TypeScript) => "",
            (_, LanguageType::Cpp) => "",
            (_, LanguageType::Go) => "",
            (_, LanguageType::Unknown) => "",
        }
    }
}

impl LanguageType {
    pub fn as_str(&self) -> &str {
        match self {
            LanguageType::Rust => "rust",
            LanguageType::Python => "python",
            LanguageType::TypeScript => "ts",
            LanguageType::Cpp => "cpp",
            LanguageType::Go => "go",
            LanguageType::Unknown => "unknown",
        }
    }
}