garbage_code_hunter/last_words/
mod.rs1pub mod display;
4pub mod scanner;
5
6use crate::common::OutputFormat;
7use anyhow::Result;
8use std::path::Path;
9
10pub fn run(path: &Path, format: &OutputFormat, with_age: bool, lang: &str) -> Result<String> {
12 let mut words = scanner::scan(path);
13
14 if with_age {
16 for word in &mut words {
17 word.age_days = scanner::try_get_age(&word.file, word.line);
18 }
19 }
20
21 let output = match format {
22 OutputFormat::Terminal => display::format_terminal(&words, lang),
23 OutputFormat::Json => display::format_json(&words),
24 };
25
26 Ok(output)
27}
28
29#[cfg(test)]
30mod tests {
31 use super::*;
32
33 #[test]
34 fn test_run_on_temp_dir() {
35 let dir = std::env::temp_dir().join("gch_lw_test");
36 let _ = std::fs::create_dir_all(&dir);
37 let file = dir.join("test.rs");
38 std::fs::write(&file, "// TODO: test\nfn main() {}\n").unwrap();
39
40 let result = run(&dir, &OutputFormat::Terminal, false, "en-US");
41 assert!(result.is_ok());
42 assert!(result.unwrap().contains("TODO"));
43
44 let _ = std::fs::remove_dir_all(&dir);
45 }
46}