|
| 1 | +mod config; |
| 2 | +mod i18n; |
| 3 | +mod utils; |
| 4 | + |
| 5 | +use std::collections::HashMap; |
| 6 | + |
| 7 | +use config::{init_config, print_config}; |
| 8 | +use i18n::t; |
| 9 | +use utils::{ |
| 10 | + filter_logs::filter_logs, |
| 11 | + format_log::{format_log, LogInfo}, |
| 12 | + get_repo_logs::get_repo_logs, |
| 13 | + get_repo_name::get_repo_name, |
| 14 | + keypress::exit_on_keypress, |
| 15 | + save_report::save_report_markdown, |
| 16 | +}; |
| 17 | + |
| 18 | +fn run() -> Result<(), Box<dyn std::error::Error>> { |
| 19 | + // Initialize global configuration. |
| 20 | + // After initialization, the config is stored in a global OnceLock singleton, |
| 21 | + // and can be accessed from other modules via `config::store::global()`, |
| 22 | + // such as in the i18n module. |
| 23 | + let config = init_config()?; |
| 24 | + |
| 25 | + print_config(); |
| 26 | + |
| 27 | + let mut result: HashMap<String, HashMap<String, Vec<LogInfo>>> = HashMap::new(); |
| 28 | + |
| 29 | + for repo_dir in &config.repos { |
| 30 | + // Get repo name |
| 31 | + let repo_name = |
| 32 | + get_repo_name(repo_dir, &config.format).unwrap_or_else(|| "undefined".to_string()); |
| 33 | + |
| 34 | + let logs = get_repo_logs(repo_dir)?; |
| 35 | + |
| 36 | + // Append repoName to the beginning of each log line |
| 37 | + let logs_with_repo: Vec<String> = logs |
| 38 | + .into_iter() |
| 39 | + .map(|log| format!("{}|||{}", repo_name, log)) |
| 40 | + .collect(); |
| 41 | + |
| 42 | + // Filter logs according to the rules in the configuration file |
| 43 | + let filtered_logs = match filter_logs( |
| 44 | + &logs_with_repo, |
| 45 | + &config.authors, |
| 46 | + &config.includes, |
| 47 | + &config.excludes, |
| 48 | + ) { |
| 49 | + Ok(l) => l, |
| 50 | + Err(e) => { |
| 51 | + eprintln!( |
| 52 | + "{}", |
| 53 | + t("err_filter_logs_failed").replace("{}", &e.to_string()) |
| 54 | + ); |
| 55 | + continue; |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + // Formatting and aggregating logs |
| 60 | + for log in filtered_logs { |
| 61 | + let log_info = format_log(&log); |
| 62 | + let type_name = log_info.type_name.clone(); |
| 63 | + result |
| 64 | + .entry(repo_name.clone()) |
| 65 | + .or_default() |
| 66 | + .entry(type_name) |
| 67 | + .or_default() |
| 68 | + .push(log_info); |
| 69 | + } |
| 70 | + |
| 71 | + // save_report_markdown(&result, "report.txt").expect(t("report_gen_error")); |
| 72 | + save_report_markdown(&result, "report.txt")?; |
| 73 | + |
| 74 | + exit_on_keypress(Some(t("press_to_exit"))); |
| 75 | + } |
| 76 | + |
| 77 | + Ok(()) |
| 78 | +} |
| 79 | + |
| 80 | +fn main() { |
| 81 | + // Print the current working directory |
| 82 | + // When the program crashes, it can help locate the cause |
| 83 | + println!(""); |
| 84 | + println!( |
| 85 | + "Current working directory: \n{}", |
| 86 | + &std::env::current_dir().unwrap().display() |
| 87 | + ); |
| 88 | + |
| 89 | + // Using `?` to propagate errors to `main` causes Rust's default error output |
| 90 | + // (via the `std::process::Termination` trait) to use the Debug format (`{:?}`) |
| 91 | + // for printing error types. As a result, only the enum variant name is shown |
| 92 | + // (e.g., `FileNotFound`), not the user-friendly message from the Display trait. |
| 93 | + // |
| 94 | + // Wrapping the main logic in a separate `run` function and handling errors |
| 95 | + // explicitly with `eprintln!` ensures that the Display output is used. |
| 96 | + if let Err(e) = run() { |
| 97 | + eprintln!("{}", e); // Use Display format to output the error |
| 98 | + std::process::exit(1); |
| 99 | + } |
| 100 | +} |
0 commit comments