|
| 1 | +use crate::i18n::t; |
| 2 | +use crate::utils::format_log::LogInfo; |
| 3 | +use std::collections::HashMap; |
| 4 | +use std::fs::File; |
| 5 | +use std::io::Write; |
| 6 | +use std::path::Path; |
| 7 | + |
| 8 | +const CATEGORY_ORDER: &[&str] = &["feat", "fix", "docs", "style", "refactor", "test", "chore"]; |
| 9 | + |
| 10 | +fn get_category_label(type_name: &str) -> &str { |
| 11 | + match type_name { |
| 12 | + "feat" => t("commit_category_features"), |
| 13 | + "fix" => t("commit_category_bug_fixes"), |
| 14 | + "docs" => t("commit_category_docs"), |
| 15 | + "style" => t("commit_category_style"), |
| 16 | + "refactor" => t("commit_category_refactor"), |
| 17 | + "test" => t("commit_category_test"), |
| 18 | + "chore" => t("commit_category_chores"), |
| 19 | + _ => type_name, |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +/// Save Markdown report |
| 24 | +pub fn save_report_markdown( |
| 25 | + result: &HashMap<String, HashMap<String, Vec<LogInfo>>>, |
| 26 | + path: &str, |
| 27 | +) -> std::io::Result<()> { |
| 28 | + // Check if result is empty |
| 29 | + if result.is_empty() { |
| 30 | + println!("{}", t("no_report_generated")); |
| 31 | + return Ok(()); |
| 32 | + } |
| 33 | + |
| 34 | + let mut md = String::new(); |
| 35 | + let mut repo_titles = vec![]; |
| 36 | + |
| 37 | + for (repo, type_map) in result { |
| 38 | + if !repo_titles.contains(repo) { |
| 39 | + md.push_str(&format!("## {}\n\n", repo)); |
| 40 | + repo_titles.push(repo.to_string()); |
| 41 | + } |
| 42 | + |
| 43 | + for &cat in CATEGORY_ORDER { |
| 44 | + if let Some(logs) = type_map.get(cat) { |
| 45 | + let label = get_category_label(cat); |
| 46 | + md.push_str(&format!("### {}\n\n", label)); |
| 47 | + for (index, log) in logs.iter().enumerate() { |
| 48 | + md.push_str(&format!("{}. {}\n", index + 1, log.message)); |
| 49 | + } |
| 50 | + md.push('\n'); |
| 51 | + } |
| 52 | + } |
| 53 | + md.push('\n'); |
| 54 | + } |
| 55 | + |
| 56 | + let mut file = File::create(Path::new(path))?; |
| 57 | + file.write_all(md.as_bytes())?; |
| 58 | + |
| 59 | + println!("{}", t("report_saved").replace("{}", path)); |
| 60 | + println!(""); |
| 61 | + |
| 62 | + Ok(()) |
| 63 | +} |
0 commit comments