|
2 | 2 | use crate::configuration::Code2PromptConfig;
|
3 | 3 | use crate::filter::{build_globset, should_include_file};
|
4 | 4 | use crate::sort::{sort_files, sort_tree, FileSortMethod};
|
| 5 | +use crate::tokenizer::count_tokens; |
5 | 6 | use crate::util::strip_utf8_bom;
|
6 | 7 | use anyhow::Result;
|
7 | 8 | use ignore::WalkBuilder;
|
8 | 9 | use log::debug;
|
| 10 | +use serde::{Deserialize, Serialize}; |
9 | 11 | use serde_json::json;
|
10 | 12 | use std::fs;
|
11 | 13 | use std::path::Path;
|
12 | 14 | use termtree::Tree;
|
13 | 15 |
|
| 16 | +#[derive(Debug, Clone, Copy, Serialize, Deserialize)] |
| 17 | +pub struct EntryMetadata { |
| 18 | + pub is_dir: bool, |
| 19 | + pub is_symlink: bool, |
| 20 | +} |
| 21 | + |
| 22 | +impl From<&std::fs::Metadata> for EntryMetadata { |
| 23 | + fn from(meta: &std::fs::Metadata) -> Self { |
| 24 | + Self { |
| 25 | + is_dir: meta.is_dir(), |
| 26 | + is_symlink: meta.is_symlink(), |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
14 | 31 | /// Traverses the directory and returns the string representation of the tree and the vector of JSON file representations.
|
15 | 32 | ///
|
16 | 33 | /// This function uses the provided configuration to determine which files to include, how to format them,
|
@@ -77,56 +94,72 @@ pub fn traverse_directory(config: &Code2PromptConfig) -> Result<(String, Vec<ser
|
77 | 94 |
|
78 | 95 | // ~~~ Processing File ~~~
|
79 | 96 | if path.is_file() && file_match {
|
80 |
| - if let Ok(code_bytes) = fs::read(path) { |
81 |
| - let clean_bytes = strip_utf8_bom(&code_bytes); |
82 |
| - let code = String::from_utf8_lossy(&clean_bytes); |
83 |
| - |
84 |
| - let code_block = wrap_code_block( |
85 |
| - &code, |
86 |
| - path.extension().and_then(|ext| ext.to_str()).unwrap_or(""), |
87 |
| - config.line_numbers, |
88 |
| - config.no_codeblock, |
89 |
| - ); |
90 |
| - |
91 |
| - if !code.trim().is_empty() && !code.contains(char::REPLACEMENT_CHARACTER) { |
92 |
| - // ~~~ Filepath ~~~ |
93 |
| - let file_path = if config.absolute_path { |
94 |
| - path.display().to_string() |
95 |
| - } else { |
96 |
| - format!("{}/{}", parent_directory, relative_path.display()) |
97 |
| - }; |
98 |
| - |
99 |
| - // ~~~ File JSON Representation ~~~ |
100 |
| - let mut file_entry = serde_json::Map::new(); |
101 |
| - file_entry.insert("path".to_string(), json!(file_path)); |
102 |
| - file_entry.insert( |
103 |
| - "extension".to_string(), |
104 |
| - json!(path.extension().and_then(|ext| ext.to_str()).unwrap_or("")), |
| 97 | + if let Ok(metadata) = entry.metadata() { |
| 98 | + if let Ok(code_bytes) = fs::read(path) { |
| 99 | + let clean_bytes = strip_utf8_bom(&code_bytes); |
| 100 | + let code = String::from_utf8_lossy(&clean_bytes); |
| 101 | + |
| 102 | + let code_block = wrap_code_block( |
| 103 | + &code, |
| 104 | + path.extension().and_then(|ext| ext.to_str()).unwrap_or(""), |
| 105 | + config.line_numbers, |
| 106 | + config.no_codeblock, |
105 | 107 | );
|
106 |
| - file_entry.insert("code".to_string(), json!(code_block)); |
107 |
| - |
108 |
| - // If date sorting is requested, record the file modification time. |
109 |
| - if let Some(method) = config.sort_method { |
110 |
| - if method == FileSortMethod::DateAsc |
111 |
| - || method == FileSortMethod::DateDesc |
112 |
| - { |
113 |
| - let mod_time = fs::metadata(path) |
114 |
| - .and_then(|m| m.modified()) |
115 |
| - .and_then(|mtime| { |
116 |
| - Ok(mtime.duration_since(std::time::SystemTime::UNIX_EPOCH)) |
117 |
| - }) |
118 |
| - .map(|d| d.unwrap().as_secs()) |
119 |
| - .unwrap_or(0); |
120 |
| - file_entry.insert("mod_time".to_string(), json!(mod_time)); |
| 108 | + |
| 109 | + if !code.trim().is_empty() && !code.contains(char::REPLACEMENT_CHARACTER) { |
| 110 | + // ~~~ Filepath ~~~ |
| 111 | + let file_path = if config.absolute_path { |
| 112 | + path.to_string_lossy().to_string() |
| 113 | + } else { |
| 114 | + relative_path.to_string_lossy().to_string() |
| 115 | + }; |
| 116 | + |
| 117 | + // ~~~ File JSON Representation ~~~ |
| 118 | + let mut file_entry = serde_json::Map::new(); |
| 119 | + file_entry.insert("path".to_string(), json!(file_path)); |
| 120 | + file_entry.insert( |
| 121 | + "extension".to_string(), |
| 122 | + json!(path.extension().and_then(|ext| ext.to_str()).unwrap_or("")), |
| 123 | + ); |
| 124 | + file_entry.insert("code".to_string(), json!(code_block)); |
| 125 | + |
| 126 | + // Store metadata |
| 127 | + let entry_meta = EntryMetadata::from(&metadata); |
| 128 | + file_entry |
| 129 | + .insert("metadata".to_string(), serde_json::to_value(entry_meta)?); |
| 130 | + |
| 131 | + // Add token count for the file only if token map is enabled |
| 132 | + if config.token_map_enabled { |
| 133 | + let token_count = count_tokens(&code, &config.encoding); |
| 134 | + file_entry.insert("token_count".to_string(), json!(token_count)); |
121 | 135 | }
|
| 136 | + |
| 137 | + // If date sorting is requested, record the file modification time. |
| 138 | + if let Some(method) = config.sort_method { |
| 139 | + if method == FileSortMethod::DateAsc |
| 140 | + || method == FileSortMethod::DateDesc |
| 141 | + { |
| 142 | + let mod_time = metadata |
| 143 | + .modified() |
| 144 | + .ok() |
| 145 | + .and_then(|mtime| { |
| 146 | + mtime |
| 147 | + .duration_since(std::time::SystemTime::UNIX_EPOCH) |
| 148 | + .ok() |
| 149 | + }) |
| 150 | + .map(|d| d.as_secs()) |
| 151 | + .unwrap_or(0); |
| 152 | + file_entry.insert("mod_time".to_string(), json!(mod_time)); |
| 153 | + } |
| 154 | + } |
| 155 | + files.push(serde_json::Value::Object(file_entry)); |
| 156 | + debug!(target: "included_files", "Included file: {}", file_path); |
| 157 | + } else { |
| 158 | + debug!("Excluded file (empty or invalid UTF-8): {}", path.display()); |
122 | 159 | }
|
123 |
| - files.push(serde_json::Value::Object(file_entry)); |
124 |
| - debug!(target: "included_files", "Included file: {}", file_path); |
125 | 160 | } else {
|
126 |
| - debug!("Excluded file (empty or invalid UTF-8): {}", path.display()); |
| 161 | + debug!("Failed to read file: {}", path.display()); |
127 | 162 | }
|
128 |
| - } else { |
129 |
| - debug!("Failed to read file: {}", path.display()); |
130 | 163 | }
|
131 | 164 | }
|
132 | 165 | }
|
|
0 commit comments