context_builder/config.rs
1use serde::Deserialize;
2use std::fs;
3use std::path::Path;
4
5/// Global configuration loaded from `.context-builder.toml`.
6///
7/// Any field left as `None` means "use the CLI default / do not override".
8/// Command-line arguments always take precedence over values provided here.
9///
10/// Example `.context-builder.toml`:
11/// ```toml
12/// output = "context.md"
13/// output_folder = "docs"
14/// timestamped_output = true
15/// auto_diff = true
16/// diff_only = true # Emit only change summary + modified file diffs (no full file bodies)
17/// filter = ["rs", "toml"]
18/// ignore = ["target", ".git"]
19/// line_numbers = false
20/// diff_context_lines = 5
21/// ```
22#[derive(Deserialize, Debug, Default)]
23pub struct Config {
24 /// Output file name (or base name when `timestamped_output = true`)
25 pub output: Option<String>,
26
27 /// File extensions to include (no leading dot, e.g. `rs`, `toml`)
28 pub filter: Option<Vec<String>>,
29
30 /// File / directory names to ignore (exact name matches)
31 pub ignore: Option<Vec<String>>,
32
33 /// Add line numbers to code blocks
34 pub line_numbers: Option<bool>,
35
36 /// Preview only the file tree (no file output)
37 pub preview: Option<bool>,
38
39 /// Token counting mode
40 pub token_count: Option<bool>,
41
42 /// Optional folder to place the generated output file(s) in
43 pub output_folder: Option<String>,
44
45 /// If true, append a UTC timestamp to the output file name (before extension)
46 pub timestamped_output: Option<bool>,
47
48 /// Assume "yes" for overwrite / processing confirmations
49 pub yes: Option<bool>,
50
51 /// Enable automatic diff generation (requires `timestamped_output = true`)
52 pub auto_diff: Option<bool>,
53
54 /// Override number of unified diff context lines (falls back to env or default = 3)
55 pub diff_context_lines: Option<usize>,
56
57 /// When true, emit ONLY:
58 /// - Header + file tree
59 /// - Change Summary
60 /// - Per-file diffs for modified files
61 ///
62 /// Excludes full file contents section entirely. Added files appear only in the
63 /// change summary (and are marked Added) but their full content is omitted.
64 pub diff_only: Option<bool>,
65}
66
67/// Load configuration from `.context-builder.toml` in the current working directory.
68/// Returns `None` if the file does not exist or cannot be parsed.
69pub fn load_config() -> Option<Config> {
70 let config_path = Path::new(".context-builder.toml");
71 if config_path.exists() {
72 let content = fs::read_to_string(config_path).ok()?;
73 toml::from_str(&content).ok()
74 } else {
75 None
76 }
77}