reformat-core 0.1.6

Core library for text and file reformatting
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! File header management transformer

use regex::Regex;
use std::fs;
use std::path::Path;
use walkdir::WalkDir;

/// Options for header management
#[derive(Debug, Clone)]
pub struct HeaderOptions {
    /// The header text to insert (without comment markers -- those are part of the text)
    pub text: String,
    /// If true, replace {year} in the header text with the current year
    pub update_year: bool,
    /// File extensions to process
    pub file_extensions: Vec<String>,
    /// Process directories recursively
    pub recursive: bool,
    /// Dry run mode (don't modify files)
    pub dry_run: bool,
}

impl Default for HeaderOptions {
    fn default() -> Self {
        HeaderOptions {
            text: String::new(),
            update_year: false,
            file_extensions: vec![
                ".py", ".pyx", ".pxd", ".pxi", ".c", ".h", ".cpp", ".hpp", ".rs", ".go", ".java",
                ".js", ".ts", ".jsx", ".tsx",
            ]
            .iter()
            .map(|s| s.to_string())
            .collect(),
            recursive: true,
            dry_run: false,
        }
    }
}

/// File header manager: insert or update headers at the top of source files
pub struct HeaderManager {
    options: HeaderOptions,
    /// The resolved header text (with year substitution applied)
    resolved_header: String,
    /// Regex to detect if the header (or a year-variant of it) already exists
    header_detector: Option<Regex>,
}

impl HeaderManager {
    /// Creates a new header manager with the given options
    pub fn new(options: HeaderOptions) -> crate::Result<Self> {
        let resolved_header = if options.update_year {
            let year = chrono::Utc::now().format("%Y").to_string();
            options.text.replace("{year}", &year)
        } else {
            options.text.clone()
        };

        // Build a detector regex: escape the header text but replace any 4-digit year
        // with \d{4} so we can find year-variant headers
        let header_detector =
            if !resolved_header.is_empty() {
                let escaped = regex::escape(&resolved_header);
                // Replace any 4-digit year (19xx or 20xx) with a flexible year pattern
                let flexible = Regex::new(r"(?:19|20)\d\{2\}")
                    .unwrap()
                    .replace_all(&escaped, r"\d{4}")
                    .to_string();
                Some(Regex::new(&flexible).map_err(|e| {
                    anyhow::anyhow!("failed to compile header detection regex: {}", e)
                })?)
            } else {
                None
            };

        Ok(HeaderManager {
            options,
            resolved_header,
            header_detector,
        })
    }

    /// Checks if a file should be processed
    fn should_process(&self, path: &Path) -> bool {
        if !path.is_file() {
            return false;
        }

        if path.components().any(|c| {
            c.as_os_str()
                .to_str()
                .map(|s| s.starts_with('.'))
                .unwrap_or(false)
        }) {
            return false;
        }

        let skip_dirs = [
            "build",
            "__pycache__",
            ".git",
            "node_modules",
            "venv",
            ".venv",
            "target",
        ];
        if path.components().any(|c| {
            c.as_os_str()
                .to_str()
                .map(|s| skip_dirs.contains(&s))
                .unwrap_or(false)
        }) {
            return false;
        }

        if let Some(ext) = path.extension() {
            let ext_str = format!(".{}", ext.to_string_lossy());
            self.options.file_extensions.contains(&ext_str)
        } else {
            false
        }
    }

    /// Process a single file. Returns true if the file was modified (or would be in dry-run).
    pub fn process_file(&self, path: &Path) -> crate::Result<bool> {
        if !self.should_process(path) {
            return Ok(false);
        }

        if self.resolved_header.is_empty() {
            return Ok(false);
        }

        let content = fs::read_to_string(path)?;

        // Check if header already exists (possibly with a different year)
        if let Some(ref detector) = self.header_detector {
            if let Some(m) = detector.find(&content) {
                // Header exists -- check if it needs a year update
                let existing = &content[m.start()..m.end()];
                if existing == self.resolved_header {
                    // Exact match, nothing to do
                    return Ok(false);
                }

                // Replace old header with new one (year update)
                let new_content = format!("{}{}", self.resolved_header, &content[m.end()..]);
                // Preserve content before the header (e.g., shebang lines)
                let prefix = &content[..m.start()];
                let full = format!("{}{}", prefix, new_content);

                if self.options.dry_run {
                    println!("Would update header in '{}'", path.display());
                } else {
                    fs::write(path, &full)?;
                    println!("Updated header in '{}'", path.display());
                }
                return Ok(true);
            }
        }

        // Header doesn't exist -- insert it
        // Preserve shebang lines (e.g., #!/usr/bin/env python)
        let (prefix, rest) = if content.starts_with("#!") {
            if let Some(pos) = content.find('\n') {
                (&content[..=pos], &content[pos + 1..])
            } else {
                (content.as_str(), "")
            }
        } else {
            ("", content.as_str())
        };

        let new_content = if prefix.is_empty() {
            format!("{}\n\n{}", self.resolved_header, rest)
        } else {
            format!("{}{}\n\n{}", prefix, self.resolved_header, rest)
        };

        if self.options.dry_run {
            println!("Would insert header in '{}'", path.display());
        } else {
            fs::write(path, &new_content)?;
            println!("Inserted header in '{}'", path.display());
        }

        Ok(true)
    }

    /// Processes a directory or file. Returns (files_changed, operation_description).
    pub fn process(&self, path: &Path) -> crate::Result<(usize, usize)> {
        let mut total_files = 0;
        // We use the second value as "operations" (1 per file touched)
        let mut total_ops = 0;

        if path.is_file() {
            if self.process_file(path)? {
                total_files = 1;
                total_ops = 1;
            }
        } else if path.is_dir() {
            if self.options.recursive {
                for entry in WalkDir::new(path).into_iter().filter_map(|e| e.ok()) {
                    if entry.file_type().is_file() && self.process_file(entry.path())? {
                        total_files += 1;
                        total_ops += 1;
                    }
                }
            } else {
                for entry in fs::read_dir(path)? {
                    let entry = entry?;
                    let entry_path = entry.path();
                    if entry_path.is_file() && self.process_file(&entry_path)? {
                        total_files += 1;
                        total_ops += 1;
                    }
                }
            }
        }

        Ok((total_files, total_ops))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn test_insert_header() {
        let dir = std::env::temp_dir().join("reformat_header_insert");
        fs::create_dir_all(&dir).unwrap();

        let file = dir.join("test.rs");
        fs::write(&file, "fn main() {}\n").unwrap();

        let options = HeaderOptions {
            text: "// Copyright 2025 TestCorp".to_string(),
            ..Default::default()
        };
        let manager = HeaderManager::new(options).unwrap();
        let (files, _) = manager.process(&file).unwrap();

        assert_eq!(files, 1);

        let content = fs::read_to_string(&file).unwrap();
        assert!(content.starts_with("// Copyright 2025 TestCorp\n\n"));
        assert!(content.contains("fn main() {}"));

        fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn test_header_already_present() {
        let dir = std::env::temp_dir().join("reformat_header_exists");
        fs::create_dir_all(&dir).unwrap();

        let file = dir.join("test.rs");
        let original = "// Copyright 2025 TestCorp\n\nfn main() {}\n";
        fs::write(&file, original).unwrap();

        let options = HeaderOptions {
            text: "// Copyright 2025 TestCorp".to_string(),
            ..Default::default()
        };
        let manager = HeaderManager::new(options).unwrap();
        let (files, _) = manager.process(&file).unwrap();

        assert_eq!(files, 0);

        let content = fs::read_to_string(&file).unwrap();
        assert_eq!(content, original);

        fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn test_update_year_in_header() {
        let dir = std::env::temp_dir().join("reformat_header_year");
        fs::create_dir_all(&dir).unwrap();

        let file = dir.join("test.rs");
        fs::write(&file, "// Copyright 2020 TestCorp\n\nfn main() {}\n").unwrap();

        let current_year = chrono::Utc::now().format("%Y").to_string();
        let options = HeaderOptions {
            text: format!("// Copyright {} TestCorp", current_year),
            ..Default::default()
        };
        let manager = HeaderManager::new(options).unwrap();
        let (files, _) = manager.process(&file).unwrap();

        assert_eq!(files, 1);

        let content = fs::read_to_string(&file).unwrap();
        assert!(content.starts_with(&format!("// Copyright {} TestCorp", current_year)));

        fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn test_preserve_shebang() {
        let dir = std::env::temp_dir().join("reformat_header_shebang");
        fs::create_dir_all(&dir).unwrap();

        let file = dir.join("test.py");
        fs::write(&file, "#!/usr/bin/env python\nprint('hello')\n").unwrap();

        let options = HeaderOptions {
            text: "# Copyright 2025 TestCorp".to_string(),
            file_extensions: vec![".py".to_string()],
            ..Default::default()
        };
        let manager = HeaderManager::new(options).unwrap();
        manager.process(&file).unwrap();

        let content = fs::read_to_string(&file).unwrap();
        assert!(content.starts_with("#!/usr/bin/env python\n"));
        assert!(content.contains("# Copyright 2025 TestCorp"));
        assert!(content.contains("print('hello')"));

        fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn test_dry_run() {
        let dir = std::env::temp_dir().join("reformat_header_dry");
        fs::create_dir_all(&dir).unwrap();

        let file = dir.join("test.rs");
        let original = "fn main() {}\n";
        fs::write(&file, original).unwrap();

        let options = HeaderOptions {
            text: "// License Header".to_string(),
            dry_run: true,
            ..Default::default()
        };
        let manager = HeaderManager::new(options).unwrap();
        let (files, _) = manager.process(&file).unwrap();

        assert_eq!(files, 1);
        let content = fs::read_to_string(&file).unwrap();
        assert_eq!(content, original);

        fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn test_empty_header() {
        let dir = std::env::temp_dir().join("reformat_header_empty");
        fs::create_dir_all(&dir).unwrap();

        let file = dir.join("test.rs");
        fs::write(&file, "fn main() {}\n").unwrap();

        let options = HeaderOptions {
            text: String::new(),
            ..Default::default()
        };
        let manager = HeaderManager::new(options).unwrap();
        let (files, _) = manager.process(&file).unwrap();

        assert_eq!(files, 0);

        fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn test_year_template_substitution() {
        let dir = std::env::temp_dir().join("reformat_header_template");
        fs::create_dir_all(&dir).unwrap();

        let file = dir.join("test.rs");
        fs::write(&file, "fn main() {}\n").unwrap();

        let options = HeaderOptions {
            text: "// Copyright {year} TestCorp".to_string(),
            update_year: true,
            ..Default::default()
        };
        let manager = HeaderManager::new(options).unwrap();
        manager.process(&file).unwrap();

        let current_year = chrono::Utc::now().format("%Y").to_string();
        let content = fs::read_to_string(&file).unwrap();
        assert!(content.contains(&format!("Copyright {} TestCorp", current_year)));

        fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn test_recursive_processing() {
        let dir = std::env::temp_dir().join("reformat_header_recursive");
        fs::create_dir_all(&dir).unwrap();

        let sub = dir.join("sub");
        fs::create_dir_all(&sub).unwrap();

        let f1 = dir.join("a.rs");
        let f2 = sub.join("b.rs");
        fs::write(&f1, "fn a() {}\n").unwrap();
        fs::write(&f2, "fn b() {}\n").unwrap();

        let options = HeaderOptions {
            text: "// Header".to_string(),
            ..Default::default()
        };
        let manager = HeaderManager::new(options).unwrap();
        let (files, _) = manager.process(&dir).unwrap();

        assert_eq!(files, 2);

        fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn test_multiline_header() {
        let dir = std::env::temp_dir().join("reformat_header_multiline");
        fs::create_dir_all(&dir).unwrap();

        let file = dir.join("test.rs");
        fs::write(&file, "fn main() {}\n").unwrap();

        let options = HeaderOptions {
            text: "// Copyright 2025 TestCorp\n// Licensed under MIT\n// All rights reserved"
                .to_string(),
            ..Default::default()
        };
        let manager = HeaderManager::new(options).unwrap();
        manager.process(&file).unwrap();

        let content = fs::read_to_string(&file).unwrap();
        assert!(content.starts_with(
            "// Copyright 2025 TestCorp\n// Licensed under MIT\n// All rights reserved\n\n"
        ));

        fs::remove_dir_all(&dir).unwrap();
    }
}