|
| 1 | +use crate::utils::format_commit::{format_commit, CommitInfo}; |
| 2 | +use chrono::{DateTime, Local, NaiveDateTime, TimeZone}; |
| 3 | + |
| 4 | +pub struct LogInfo { |
| 5 | + pub repo: String, |
| 6 | + pub author: String, |
| 7 | + pub email: String, |
| 8 | + pub commit: String, |
| 9 | + pub type_name: String, |
| 10 | + pub category: String, |
| 11 | + pub message: String, |
| 12 | + pub hash: String, |
| 13 | + pub time: String, |
| 14 | + pub unix: i64, |
| 15 | +} |
| 16 | + |
| 17 | +fn parse_time(time_str: &str) -> (String, i64) { |
| 18 | + // Try RFC2822 first |
| 19 | + if let Ok(dt) = DateTime::parse_from_rfc2822(time_str) { |
| 20 | + let local_dt: DateTime<Local> = dt.with_timezone(&Local); |
| 21 | + ( |
| 22 | + local_dt.format("%Y-%m-%d %H:%M:%S").to_string(), |
| 23 | + local_dt.timestamp_millis(), |
| 24 | + ) |
| 25 | + } |
| 26 | + // Try common format |
| 27 | + else if let Ok(naive_dt) = NaiveDateTime::parse_from_str(time_str, "%Y-%m-%d %H:%M:%S") { |
| 28 | + // Use local timezone |
| 29 | + let local_dt = Local.from_local_datetime(&naive_dt).unwrap(); |
| 30 | + ( |
| 31 | + local_dt.format("%Y-%m-%d %H:%M:%S").to_string(), |
| 32 | + local_dt.timestamp_millis(), |
| 33 | + ) |
| 34 | + } |
| 35 | + // Parse failed |
| 36 | + else { |
| 37 | + (time_str.to_string(), 0) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// Format log |
| 42 | +pub fn format_log(log: &str) -> LogInfo { |
| 43 | + let arr: Vec<&str> = log.split("|||").collect(); |
| 44 | + |
| 45 | + let repo = arr.get(0).unwrap_or(&"").to_string(); |
| 46 | + let author = arr.get(1).unwrap_or(&"").to_string(); |
| 47 | + let email = arr.get(2).unwrap_or(&"").to_string(); |
| 48 | + let commit = arr.get(3).unwrap_or(&"").to_string(); |
| 49 | + let hash = arr.get(4).unwrap_or(&"").replace("'", "#"); |
| 50 | + let time_str = arr.get(5).unwrap_or(&"").to_string(); |
| 51 | + |
| 52 | + let (time, unix) = if let Ok(dt) = DateTime::parse_from_rfc2822(&time_str) { |
| 53 | + let local_dt: DateTime<Local> = dt.with_timezone(&Local); |
| 54 | + ( |
| 55 | + local_dt.format("%Y-%m-%d %H:%M:%S").to_string(), |
| 56 | + local_dt.timestamp_millis(), |
| 57 | + ) |
| 58 | + } else if let Ok(naive_dt) = NaiveDateTime::parse_from_str(&time_str, "%Y-%m-%d %H:%M:%S") { |
| 59 | + let local_dt = Local.from_local_datetime(&naive_dt).unwrap(); |
| 60 | + ( |
| 61 | + local_dt.format("%Y-%m-%d %H:%M:%S").to_string(), |
| 62 | + local_dt.timestamp_millis(), |
| 63 | + ) |
| 64 | + } else { |
| 65 | + (time_str.clone(), 0) |
| 66 | + }; |
| 67 | + |
| 68 | + let CommitInfo { |
| 69 | + type_name, |
| 70 | + category, |
| 71 | + message, |
| 72 | + } = format_commit(&commit); |
| 73 | + |
| 74 | + LogInfo { |
| 75 | + repo, |
| 76 | + author, |
| 77 | + email, |
| 78 | + commit, |
| 79 | + type_name, |
| 80 | + category, |
| 81 | + message, |
| 82 | + hash, |
| 83 | + time, |
| 84 | + unix, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +#[cfg(test)] |
| 89 | +mod tests { |
| 90 | + use super::*; |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn test_format_log_basic() { |
| 94 | + let log = |
| 95 | + "repo1|||Alice|||alice@example.com|||feat: add feature|||'abc123|||2024-06-01 12:00:00"; |
| 96 | + let info = format_log(log); |
| 97 | + assert_eq!(info.repo, "repo1"); |
| 98 | + assert_eq!(info.author, "Alice"); |
| 99 | + assert_eq!(info.email, "alice@example.com"); |
| 100 | + assert_eq!(info.commit, "feat: add feature"); |
| 101 | + assert_eq!(info.hash, "#abc123"); |
| 102 | + assert_eq!(info.time, "2024-06-01 12:00:00"); |
| 103 | + assert_eq!(info.type_name, "feat"); |
| 104 | + assert_eq!(info.message, "add feature"); |
| 105 | + } |
| 106 | +} |
0 commit comments